/* Copyright (c) 2010 Mans Rullgard 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 #include #include #include #include #include #include #include #define PAGE_SIZE 4096 #define EMIF1_BASE 0x4c000000 #define EMIF2_BASE 0x4d000000 #define EMIF_PERF_CNT_1 0x80 #define EMIF_PERF_CNT_2 0x84 #define EMIF_PERF_CNT_CFG 0x88 #define EMIF_PERF_CNT_TIM 0x90 struct emif_perf { int code; const char *name; }; static const struct emif_perf emif_perf_tab[] = { { 0, "access" }, { 1, "activate" }, { 2, "read" }, { 3, "write" }, { 4, "fifo_cmd" }, { 5, "fifo_write" }, { 6, "fifo_read" }, { 7, "fifo_ret" }, { 8, "prio" }, { 9, "cmd_pend" }, { 10, "data" }, }; static void *emif1, *emif2; static int cfg1 = 9; static int cfg2 = 10; struct emif_stats { uint32_t cycles; uint32_t cnt1; uint32_t cnt2; }; static struct emif_stats emif1_start, emif1_end; static struct emif_stats emif2_start, emif2_end; static void *emif_init(int fd, unsigned base) { void *mem = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, base); volatile uint32_t *emif = mem; if (mem == MAP_FAILED) return NULL; if (*emif != 0x40441403 && *emif != 0x40440c05) { munmap(mem, PAGE_SIZE); return NULL; } emif[EMIF_PERF_CNT_CFG>>2] = cfg2 << 16 | cfg1; return mem; } static void emif_read(volatile uint32_t *emif, struct emif_stats *st) { st->cycles = emif[EMIF_PERF_CNT_TIM>>2]; st->cnt1 = emif[EMIF_PERF_CNT_1>>2]; st->cnt2 = emif[EMIF_PERF_CNT_2>>2]; } static void emif_print(const char *tag, struct emif_stats *st1, struct emif_stats *st2) { uint32_t cycles = st2->cycles - st1->cycles; uint32_t cnt1 = st2->cnt1 - st1->cnt1; uint32_t cnt2 = st2->cnt2 - st1->cnt2; printf("%s %s %2llu%% %s %2llu%%", tag, emif_perf_tab[cfg1].name, 100ull*cnt1/cycles, emif_perf_tab[cfg2].name, 100ull*cnt2/cycles); } static int perf_init(void) { int fd = open("/dev/mem", O_RDWR); int err = 0; if (fd == -1) return -1; emif1 = emif_init(fd, EMIF1_BASE); emif2 = emif_init(fd, EMIF2_BASE); if (!emif1 || !emif2) err = -1; close(fd); return err; } static void perf_start(void) { if (emif1) { emif_read(emif1, &emif1_start); emif_read(emif2, &emif2_start); } } static void perf_stop(void) { if (emif1) { emif_read(emif1, &emif1_end); emif_read(emif2, &emif2_end); } } static void perf_print(void) { if (emif1) { emif_print("EMIF1", &emif1_start, &emif1_end); printf("\t"); emif_print("EMIF2", &emif2_start, &emif2_end); printf("\n"); } } static void perf_close(void) { if (emif1) munmap(emif1, PAGE_SIZE); if (emif2) munmap(emif2, PAGE_SIZE); } static int get_cfg(const char *name, int def) { char *end; int n = strtol(name, &end, 0); int i; if (!*end) return n; for (i = 0; i < sizeof(emif_perf_tab)/sizeof(emif_perf_tab[0]); i++) if (!strcmp(name, emif_perf_tab[i].name)) return emif_perf_tab[i].code; return def; } int main(int argc, char **argv) { int delay = 1; if (argc > 1) delay = strtol(argv[1], NULL, 0); if (argc > 2) cfg1 = get_cfg(argv[2], 9); if (argc > 3) cfg2 = get_cfg(argv[3], 10); if (delay <= 0) delay = 1; if (perf_init()) return 1; for (;;) { perf_start(); sleep(delay); perf_stop(); perf_print(); } perf_close(); return 0; }