diff mbox

[v3,09/12] ppc: odp_cpu_cycles() for ppc

Message ID 20160726025625.7343-10-brian.brooks@linaro.org
State New
Headers show

Commit Message

Brian Brooks July 26, 2016, 2:56 a.m. UTC
Signed-off-by: Brian Brooks <brian.brooks@linaro.org>

---
 platform/linux-generic/arch/powerpc/odp_cpu_arch.c | 26 ++++++++--------------
 1 file changed, 9 insertions(+), 17 deletions(-)

-- 
2.9.0

Comments

Maxim Uvarov July 26, 2016, 7:47 a.m. UTC | #1
On 07/26/16 05:56, Brian Brooks wrote:
> Signed-off-by: Brian Brooks <brian.brooks@linaro.org>

> ---

>   platform/linux-generic/arch/powerpc/odp_cpu_arch.c | 26 ++++++++--------------

>   1 file changed, 9 insertions(+), 17 deletions(-)

>

> diff --git a/platform/linux-generic/arch/powerpc/odp_cpu_arch.c b/platform/linux-generic/arch/powerpc/odp_cpu_arch.c

> index 109dd93..346c170 100644

> --- a/platform/linux-generic/arch/powerpc/odp_cpu_arch.c

> +++ b/platform/linux-generic/arch/powerpc/odp_cpu_arch.c

> @@ -14,25 +14,17 @@

>   #include <odp/api/system_info.h>

>   #include <odp_debug_internal.h>

>   

> -#define GIGA 1000000000

> -

>   uint64_t odp_cpu_cycles(void)

>   {

> -	struct timespec time;

> -	uint64_t sec, ns, hz, cycles;

> -	int ret;

> -

> -	ret = clock_gettime(CLOCK_MONOTONIC_RAW, &time);

> -

> -	if (ret != 0)

> -		ODP_ABORT("clock_gettime failed\n");

> -

> -	hz  = odp_cpu_hz_max();

> -	sec = (uint64_t)time.tv_sec;

> -	ns  = (uint64_t)time.tv_nsec;

> +#if defined(__powerpc__) || defined(__ppc__)

> +	uint64_t tbl, tbu0, tbu1;

>   

> -	cycles  = sec * hz;

> -	cycles += (ns * hz) / GIGA;

> +	do {

> +		__asm__ volatile("mftbu %0" : "=r"(tbu0));

> +		__asm__ volatile("mftb  %0" : "=r"(tbl));

> +		__asm__ volatile("mftbu %0" : "=r"(tbu1));

> +	} while (tbu0 != tbu1);

>   

> -	return cycles;

> +	return (tbu0 << 32) | tbl;

> +#endif

return for non ifdef case is missing. Or there has to be #else 
ODP_STATIC_ASSERT("add ifdef for new case")

Maxim.

>   }
Brian Brooks July 26, 2016, 5:06 p.m. UTC | #2
On 07/26 10:47:44, Maxim Uvarov wrote:
> On 07/26/16 05:56, Brian Brooks wrote:

> > Signed-off-by: Brian Brooks <brian.brooks@linaro.org>

> > ---

> >   platform/linux-generic/arch/powerpc/odp_cpu_arch.c | 26 ++++++++--------------

> >   1 file changed, 9 insertions(+), 17 deletions(-)

> > 

> > diff --git a/platform/linux-generic/arch/powerpc/odp_cpu_arch.c b/platform/linux-generic/arch/powerpc/odp_cpu_arch.c

> > index 109dd93..346c170 100644

> > --- a/platform/linux-generic/arch/powerpc/odp_cpu_arch.c

> > +++ b/platform/linux-generic/arch/powerpc/odp_cpu_arch.c

> > @@ -14,25 +14,17 @@

> >   #include <odp/api/system_info.h>

> >   #include <odp_debug_internal.h>

> > -#define GIGA 1000000000

> > -

> >   uint64_t odp_cpu_cycles(void)

> >   {

> > -	struct timespec time;

> > -	uint64_t sec, ns, hz, cycles;

> > -	int ret;

> > -

> > -	ret = clock_gettime(CLOCK_MONOTONIC_RAW, &time);

> > -

> > -	if (ret != 0)

> > -		ODP_ABORT("clock_gettime failed\n");

> > -

> > -	hz  = odp_cpu_hz_max();

> > -	sec = (uint64_t)time.tv_sec;

> > -	ns  = (uint64_t)time.tv_nsec;

> > +#if defined(__powerpc__) || defined(__ppc__)

> > +	uint64_t tbl, tbu0, tbu1;

> > -	cycles  = sec * hz;

> > -	cycles += (ns * hz) / GIGA;

> > +	do {

> > +		__asm__ volatile("mftbu %0" : "=r"(tbu0));

> > +		__asm__ volatile("mftb  %0" : "=r"(tbl));

> > +		__asm__ volatile("mftbu %0" : "=r"(tbu1));

> > +	} while (tbu0 != tbu1);

> > -	return cycles;

> > +	return (tbu0 << 32) | tbl;

> > +#endif

> return for non ifdef case is missing. Or there has to be #else

> ODP_STATIC_ASSERT("add ifdef for new case")

> 

> Maxim.


Can we agree:

  int foo()
  {
  #if defined(__target__)
      return 42;
  #else
  #error Add support for your target in this file.
  #endif
  }

is preferred over static assertion or returning a zero value?
Maxim Uvarov July 27, 2016, 12:11 p.m. UTC | #3
On 07/26/16 20:06, Brian Brooks wrote:
> On 07/26 10:47:44, Maxim Uvarov wrote:

>> On 07/26/16 05:56, Brian Brooks wrote:

>>> Signed-off-by: Brian Brooks <brian.brooks@linaro.org>

>>> ---

>>>    platform/linux-generic/arch/powerpc/odp_cpu_arch.c | 26 ++++++++--------------

>>>    1 file changed, 9 insertions(+), 17 deletions(-)

>>>

>>> diff --git a/platform/linux-generic/arch/powerpc/odp_cpu_arch.c b/platform/linux-generic/arch/powerpc/odp_cpu_arch.c

>>> index 109dd93..346c170 100644

>>> --- a/platform/linux-generic/arch/powerpc/odp_cpu_arch.c

>>> +++ b/platform/linux-generic/arch/powerpc/odp_cpu_arch.c

>>> @@ -14,25 +14,17 @@

>>>    #include <odp/api/system_info.h>

>>>    #include <odp_debug_internal.h>

>>> -#define GIGA 1000000000

>>> -

>>>    uint64_t odp_cpu_cycles(void)

>>>    {

>>> -	struct timespec time;

>>> -	uint64_t sec, ns, hz, cycles;

>>> -	int ret;

>>> -

>>> -	ret = clock_gettime(CLOCK_MONOTONIC_RAW, &time);

>>> -

>>> -	if (ret != 0)

>>> -		ODP_ABORT("clock_gettime failed\n");

>>> -

>>> -	hz  = odp_cpu_hz_max();

>>> -	sec = (uint64_t)time.tv_sec;

>>> -	ns  = (uint64_t)time.tv_nsec;

>>> +#if defined(__powerpc__) || defined(__ppc__)

>>> +	uint64_t tbl, tbu0, tbu1;

>>> -	cycles  = sec * hz;

>>> -	cycles += (ns * hz) / GIGA;

>>> +	do {

>>> +		__asm__ volatile("mftbu %0" : "=r"(tbu0));

>>> +		__asm__ volatile("mftb  %0" : "=r"(tbl));

>>> +		__asm__ volatile("mftbu %0" : "=r"(tbu1));

>>> +	} while (tbu0 != tbu1);

>>> -	return cycles;

>>> +	return (tbu0 << 32) | tbl;

>>> +#endif

>> return for non ifdef case is missing. Or there has to be #else

>> ODP_STATIC_ASSERT("add ifdef for new case")

>>

>> Maxim.

> Can we agree:

>

>    int foo()

>    {

>    #if defined(__target__)

>        return 42;

>    #else

>    #error Add support for your target in this file.

>    #endif

>    }

>

> is preferred over static assertion or returning a zero value?

yes, I think that is ok.

Maxim.
diff mbox

Patch

diff --git a/platform/linux-generic/arch/powerpc/odp_cpu_arch.c b/platform/linux-generic/arch/powerpc/odp_cpu_arch.c
index 109dd93..346c170 100644
--- a/platform/linux-generic/arch/powerpc/odp_cpu_arch.c
+++ b/platform/linux-generic/arch/powerpc/odp_cpu_arch.c
@@ -14,25 +14,17 @@ 
 #include <odp/api/system_info.h>
 #include <odp_debug_internal.h>
 
-#define GIGA 1000000000
-
 uint64_t odp_cpu_cycles(void)
 {
-	struct timespec time;
-	uint64_t sec, ns, hz, cycles;
-	int ret;
-
-	ret = clock_gettime(CLOCK_MONOTONIC_RAW, &time);
-
-	if (ret != 0)
-		ODP_ABORT("clock_gettime failed\n");
-
-	hz  = odp_cpu_hz_max();
-	sec = (uint64_t)time.tv_sec;
-	ns  = (uint64_t)time.tv_nsec;
+#if defined(__powerpc__) || defined(__ppc__)
+	uint64_t tbl, tbu0, tbu1;
 
-	cycles  = sec * hz;
-	cycles += (ns * hz) / GIGA;
+	do {
+		__asm__ volatile("mftbu %0" : "=r"(tbu0));
+		__asm__ volatile("mftb  %0" : "=r"(tbl));
+		__asm__ volatile("mftbu %0" : "=r"(tbu1));
+	} while (tbu0 != tbu1);
 
-	return cycles;
+	return (tbu0 << 32) | tbl;
+#endif
 }