Hi Aravind,

On Tue, Aug 16, 2016 at 4:33 AM, machiry aravind <machiry_msidc@hotmail.com> wrote:

Hi all,


I want to measure clock frequency in secure world. I am using the function: read_cntfrq (https://github.com/OP-TEE/optee_os/blob/master/core/arch/arm/include/arm32.h#L529) to get it.


On Hikey,  The value I get is: 1.2MHz,


Right. It is set by ARM Trusted Firmware, here: https://github.com/linaro-swg/arm-trusted-firmware/blob/hikey_gendrv/plat/hikey/aarch64/hikey_common.c#L154-L157

is it scaled down or this is the actual frequency?

Looks like it is the actual frequency. I wrote a small test in non-secure world to check this. I'm reading CNTVCT_EL0 because CNTPCT_EL0 is not accessible to user space (access is disabled by the Linux kernel). But it's the same thing essentially.

#include <inttypes.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>

uint32_t read_cntfrq()
{
uint32_t val;

asm volatile("mrs %0, cntfrq_el0" : "=r" (val));
return val;
}

uint64_t read_cntvct()
{
uint64_t val;
asm volatile("mrs %0, cntvct_el0" : "=r" (val));
return val;
}

double ts_to_sec(struct timespec *ts)
{
return (ts->tv_sec + ((double)ts->tv_nsec)/1000000000);
}

double ts_diff(struct timespec *ta, struct timespec *tb)
{
return ts_to_sec(tb) - ts_to_sec(ta);
}

int main(int argc, char *argv[])
{
uint64_t a,b;
struct timespec ta, tb;

printf("cntfrq=%d\n", read_cntfrq());
a = read_cntvct();
clock_gettime(CLOCK_MONOTONIC, &ta);
printf("ctnvct=%ld\n", a);
sleep(20);
b = read_cntvct();
clock_gettime(CLOCK_MONOTONIC, &tb);
printf("cntvct=%ld\ndelta=%ld (%g/sec)\n", b, b-a,
      ((double)(b-a))/ts_diff(&ta, &tb));
return 0;
}

root@HiKey:/ cntfrq 
cntfrq=1200000
ctnvct=67224235
cntvct=91224367
delta=24000132 (1.2e+06/sec)
root@HiKey:/


Now I'm wondering if the conter frequency is exactly 1.2 MHz, or if it's a multiple of the main CPU clock which is slightly less than 1.2 GHz (which is likely IMO):

NOTICE:  Booting Trusted Firmware
NOTICE:  BL1: v1.1(release):9d3b0ec
NOTICE:  BL1: Built : 16:57:57, Aug 12 2016
NOTICE:  syspll frequency:1190494208Hz

(this ARM-TF commit sets the syspll frequency to 1.19 GHz: https://github.com/linaro-swg/arm-trusted-firmware/commit/f88eff3cb154). You may have to make more precise measurements.

HTH,
 -- 
Jerome


Thank You,

Aravind


_______________________________________________
Tee-dev mailing list
Tee-dev@lists.linaro.org
https://lists.linaro.org/mailman/listinfo/tee-dev