| 1 |
/* |
| 2 |
* The MIT License (MIT) |
| 3 |
* |
| 4 |
* Copyright (c) 2016 Philippe Proulx <[email protected]> |
| 5 |
* |
| 6 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 |
* of this software and associated documentation files (the "Software"), to deal |
| 8 |
* in the Software without restriction, including without limitation the rights |
| 9 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 |
* copies of the Software, and to permit persons to whom the Software is |
| 11 |
* furnished to do so, subject to the following conditions: |
| 12 |
* |
| 13 |
* The above copyright notice and this permission notice shall be included in |
| 14 |
* all copies or substantial portions of the Software. |
| 15 |
* |
| 16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 |
* THE SOFTWARE. |
| 23 |
*/ |
| 24 |
|
| 25 |
#include <stdint.h> |
| 26 |
#include <stdio.h> |
| 27 |
#include <barectf.h> |
| 28 |
|
| 29 |
#include "barectf-platform-qemu-arm-uart.h" |
| 30 |
|
| 31 |
#define BUF_SIZE 4096 |
| 32 |
#define TIMER_CTRL_32BIT (1 << 1) |
| 33 |
#define TIMER_CTRL_ENABLE (1 << 7) |
| 34 |
|
| 35 |
volatile uint32_t * const uart0 = (uint32_t *) 0x101f1000; |
| 36 |
volatile uint32_t * const uart1 = (uint32_t *) 0x101f2000; |
| 37 |
volatile uint32_t * const timer0_ctrl = (uint32_t *) 0x101e2008; |
| 38 |
volatile uint32_t * const timer0_value = (uint32_t *) 0x101e2004; |
| 39 |
|
| 40 |
static struct barectf_default_ctx barectf_ctx; |
| 41 |
|
| 42 |
static uint8_t buf[BUF_SIZE]; |
| 43 |
|
| 44 |
static uint64_t get_clock(void* data) |
| 45 |
{ |
| 46 |
return (uint64_t) -*timer0_value; |
| 47 |
} |
| 48 |
|
| 49 |
static void flush_packet(void) |
| 50 |
{ |
| 51 |
size_t i; |
| 52 |
|
| 53 |
/* flush packet to UART 1 */ |
| 54 |
for (i = 0; i < BUF_SIZE; ++i) { |
| 55 |
*uart1 = (uint32_t) buf[i]; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
static int is_backend_full(void *data) |
| 60 |
{ |
| 61 |
return 0; |
| 62 |
} |
| 63 |
|
| 64 |
static void open_packet(void *data) |
| 65 |
{ |
| 66 |
barectf_default_open_packet(&barectf_ctx); |
| 67 |
} |
| 68 |
|
| 69 |
static void close_packet(void *data) |
| 70 |
{ |
| 71 |
/* close packet now */ |
| 72 |
barectf_default_close_packet(&barectf_ctx); |
| 73 |
|
| 74 |
/* flush current packet */ |
| 75 |
flush_packet(); |
| 76 |
} |
| 77 |
|
| 78 |
void barectf_platform_qemu_arm_uart_init(void) |
| 79 |
{ |
| 80 |
struct barectf_platform_callbacks cbs = { |
| 81 |
.default_clock_get_value = get_clock, |
| 82 |
.is_backend_full = is_backend_full, |
| 83 |
.open_packet = open_packet, |
| 84 |
.close_packet = close_packet, |
| 85 |
}; |
| 86 |
|
| 87 |
/* enable/start timer (clock source) */ |
| 88 |
*timer0_ctrl |= TIMER_CTRL_32BIT | TIMER_CTRL_ENABLE; |
| 89 |
|
| 90 |
/* initialize barectf context */ |
| 91 |
barectf_init(&barectf_ctx, buf, BUF_SIZE, cbs, NULL); |
| 92 |
|
| 93 |
/* open first packet */ |
| 94 |
open_packet(NULL); |
| 95 |
|
| 96 |
/* indicate that tracing is starting */ |
| 97 |
puts("tracing: starting"); |
| 98 |
} |
| 99 |
|
| 100 |
void barectf_platform_qemu_arm_uart_fini(void) |
| 101 |
{ |
| 102 |
/* close last packet if it contains at least one event */ |
| 103 |
if (barectf_packet_is_open(&barectf_ctx) && |
| 104 |
!barectf_packet_is_empty(&barectf_ctx)) { |
| 105 |
close_packet(NULL); |
| 106 |
} |
| 107 |
|
| 108 |
/* indicate that tracing is done */ |
| 109 |
puts("tracing: done"); |
| 110 |
} |
| 111 |
|
| 112 |
struct barectf_default_ctx *barectf_platform_qemu_arm_uart_get_barectf_ctx() |
| 113 |
{ |
| 114 |
return &barectf_ctx; |
| 115 |
} |
| 116 |
|
| 117 |
#define STDOUT 1 |
| 118 |
#define STDERR 2 |
| 119 |
|
| 120 |
/* custom write "syscall" for newlib's stdio: write to UART 0 */ |
| 121 |
int _write(int file, char *ptr, int len) { |
| 122 |
if (file == STDOUT || file == STDERR) { |
| 123 |
int i; |
| 124 |
|
| 125 |
for (i = 0; i < len; i++) { |
| 126 |
*uart0 = (uint32_t) ptr[i]; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
return 0; |
| 131 |
} |