Re: [PATCH RTEMS v2 5/6] libfreebsd: Port OFW to RTEMS

2020-06-14 Thread Niteesh G. S.
On Sun, Jun 14, 2020 at 5:48 PM Christian Mauderer 
wrote:

>
>
> On 14/06/2020 13:37, Vijay Kumar Banerjee wrote:
> > On Sat, Jun 13, 2020 at 11:44 PM Niteesh G. S. 
> wrote:
> >>
> >>
> >>
> >> On Sat, Jun 13, 2020 at 6:30 PM Vijay Kumar Banerjee 
> wrote:
> >>>
> >>> On Thu, Jun 11, 2020 at 5:53 PM G S Niteesh Babu 
> wrote:
> 
>  The following files have been ported to RTEMS
>  1) openfirm.h
>  2) openfirm.c
>  3) ofw_fdt.c
>  ---
>   cpukit/libfreebsd/dev/ofw/ofw_fdt.c  | 146
> ++-
>   cpukit/libfreebsd/dev/ofw/openfirm.c |  58 ++-
>   cpukit/libfreebsd/dev/ofw/openfirm.h |  17 
>   3 files changed, 219 insertions(+), 2 deletions(-)
> 
>  diff --git a/cpukit/libfreebsd/dev/ofw/ofw_fdt.c
> b/cpukit/libfreebsd/dev/ofw/ofw_fdt.c
>  index e4f72e8142..aba170d67a 100644
>  --- a/cpukit/libfreebsd/dev/ofw/ofw_fdt.c
>  +++ b/cpukit/libfreebsd/dev/ofw/ofw_fdt.c
>  @@ -30,9 +30,12 @@
>    */
> 
>   #include 
>  +#ifndef __rtems__
>   __FBSDID("$FreeBSD$");
>  +#endif /* __rtems__ */
> 
>   #include 
>  +#ifndef __rtems__
>   #include 
>   #include 
>   #include 
>  @@ -45,9 +48,20 @@ __FBSDID("$FreeBSD$");
>   #include 
>   #include 
>   #include 
>  +#endif /* __rtems__ */
> 
>   #include "ofw_if.h"
> 
>  +#ifdef __rtems__
>  +#include 
>  +#include 
>  +#include 
>  +#include 
>  +#include 
>  +#include "openfirm.h"
>  +#endif /* __rtems__ */
>  +
>  +#ifndef __rtems__
>   #ifdef DEBUG
>   #define debugf(fmt, args...) do { printf("%s(): ", __func__);  \
>   printf(fmt,##args); } while (0)
>  @@ -63,6 +77,7 @@ __FBSDID("$FreeBSD$");
>   #define FDT_MARVELL
>   #endif
>   #endif
>  +#endif /* __rtems__ */
> 
>   static int ofw_fdt_init(ofw_t, void *);
>   static phandle_t ofw_fdt_peer(ofw_t, phandle_t);
>  @@ -78,6 +93,7 @@ static ssize_t ofw_fdt_canon(ofw_t, const char *,
> char *, size_t);
>   static phandle_t ofw_fdt_finddevice(ofw_t, const char *);
>   static ssize_t ofw_fdt_instance_to_path(ofw_t, ihandle_t, char *,
> size_t);
>   static ssize_t ofw_fdt_package_to_path(ofw_t, phandle_t, char *,
> size_t);
>  +#ifndef __rtems__
>   static int ofw_fdt_interpret(ofw_t, const char *, int, cell_t *);
> 
>   static ofw_method_t ofw_fdt_methods[] = {
>  @@ -104,9 +120,11 @@ static ofw_def_t ofw_fdt = {
>  0
>   };
>   OFW_DEF(ofw_fdt);
>  +#endif /* __rtems__ */
> 
>   static void *fdtp = NULL;
> 
>  +#ifndef __rtems__
>   static int
>   sysctl_handle_dtb(SYSCTL_HANDLER_ARGS)
>   {
>  @@ -127,6 +145,27 @@ sysctl_register_fdt_oid(void *arg)
>  sysctl_handle_dtb, "", "Device Tree Blob");
>   }
>   SYSINIT(dtb_oid, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_fdt_oid,
> NULL);
>  +#else /* __rtems__ */
>  +const void* bsp_fdt_get(void);
> >>>
> >>> Why is it declared here? The function declaration is in fdt.h which is
> >>> included in the libfdt.h
> >>
> >>
> >> We cannot include a BSP headers from cpukit.
> >>
> > The definition of the function is in bsps/shared/start so we're
> > already using code from the bsps/ here. I wonder if this raises the
> > question of whether cpukit is the right location for this driver or if
> > we should use the bsps codes in this driver as a special case?
>
> I wondered about that too. That's why Niteesh moved it in an earlier
> patch set. I think Sebastian said that it would be better in cpukit.
>
> But I'm not that happy with the current solution either. That code
> wouldn't even link without the bsp_fdt_get function. Is there an
> alternative that we could use that would link?
>

Hello Christian,


Sebastian already suggested creating an API to register a device
tree. The main goal of this was to allow unit testing. But then he
mentioned about using wrapper functions to test.
https://lists.rtems.org/pipermail/devel/2020-June/060025.html

I would like to get this patch to a state where this can be pushed
without any changes. The upcoming patches are also libfreebsd
related. If we could finalize this patch we can avoid some
repetitive discussions and will also make it easier for me to create
the next patches.

These are the following points that need to be finalized

1) Where to place the header files? Should they be placed under
cpukit/include/libfreebsd or can we add libfreebsd to the include path?
The latter one will allow us to have same header structure as FreeBSD.

2) How to handle BSP dependencies?

Thanks,
Niteesh.

> >>>
>  +static void
>  +ofw_init(void)
>  +{
>  +   int rv;
>  +   const void *fdt;
>  +
>  +   fdt = bsp_fdt_get();
>  +   rv = ofw_fdt_init(NULL, fdt);
>  +
>  +   /* Fatal if invalid FDT is provided */
>  + 

Re: [PATCH RTEMS v2 5/6] libfreebsd: Port OFW to RTEMS

2020-06-14 Thread Christian Mauderer



On 14/06/2020 13:37, Vijay Kumar Banerjee wrote:
> On Sat, Jun 13, 2020 at 11:44 PM Niteesh G. S.  wrote:
>>
>>
>>
>> On Sat, Jun 13, 2020 at 6:30 PM Vijay Kumar Banerjee  wrote:
>>>
>>> On Thu, Jun 11, 2020 at 5:53 PM G S Niteesh Babu  
>>> wrote:

 The following files have been ported to RTEMS
 1) openfirm.h
 2) openfirm.c
 3) ofw_fdt.c
 ---
  cpukit/libfreebsd/dev/ofw/ofw_fdt.c  | 146 ++-
  cpukit/libfreebsd/dev/ofw/openfirm.c |  58 ++-
  cpukit/libfreebsd/dev/ofw/openfirm.h |  17 
  3 files changed, 219 insertions(+), 2 deletions(-)

 diff --git a/cpukit/libfreebsd/dev/ofw/ofw_fdt.c 
 b/cpukit/libfreebsd/dev/ofw/ofw_fdt.c
 index e4f72e8142..aba170d67a 100644
 --- a/cpukit/libfreebsd/dev/ofw/ofw_fdt.c
 +++ b/cpukit/libfreebsd/dev/ofw/ofw_fdt.c
 @@ -30,9 +30,12 @@
   */

  #include 
 +#ifndef __rtems__
  __FBSDID("$FreeBSD$");
 +#endif /* __rtems__ */

  #include 
 +#ifndef __rtems__
  #include 
  #include 
  #include 
 @@ -45,9 +48,20 @@ __FBSDID("$FreeBSD$");
  #include 
  #include 
  #include 
 +#endif /* __rtems__ */

  #include "ofw_if.h"

 +#ifdef __rtems__
 +#include 
 +#include 
 +#include 
 +#include 
 +#include 
 +#include "openfirm.h"
 +#endif /* __rtems__ */
 +
 +#ifndef __rtems__
  #ifdef DEBUG
  #define debugf(fmt, args...) do { printf("%s(): ", __func__);  \
  printf(fmt,##args); } while (0)
 @@ -63,6 +77,7 @@ __FBSDID("$FreeBSD$");
  #define FDT_MARVELL
  #endif
  #endif
 +#endif /* __rtems__ */

  static int ofw_fdt_init(ofw_t, void *);
  static phandle_t ofw_fdt_peer(ofw_t, phandle_t);
 @@ -78,6 +93,7 @@ static ssize_t ofw_fdt_canon(ofw_t, const char *, char 
 *, size_t);
  static phandle_t ofw_fdt_finddevice(ofw_t, const char *);
  static ssize_t ofw_fdt_instance_to_path(ofw_t, ihandle_t, char *, size_t);
  static ssize_t ofw_fdt_package_to_path(ofw_t, phandle_t, char *, size_t);
 +#ifndef __rtems__
  static int ofw_fdt_interpret(ofw_t, const char *, int, cell_t *);

  static ofw_method_t ofw_fdt_methods[] = {
 @@ -104,9 +120,11 @@ static ofw_def_t ofw_fdt = {
 0
  };
  OFW_DEF(ofw_fdt);
 +#endif /* __rtems__ */

  static void *fdtp = NULL;

 +#ifndef __rtems__
  static int
  sysctl_handle_dtb(SYSCTL_HANDLER_ARGS)
  {
 @@ -127,6 +145,27 @@ sysctl_register_fdt_oid(void *arg)
 sysctl_handle_dtb, "", "Device Tree Blob");
  }
  SYSINIT(dtb_oid, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_fdt_oid, 
 NULL);
 +#else /* __rtems__ */
 +const void* bsp_fdt_get(void);
>>>
>>> Why is it declared here? The function declaration is in fdt.h which is
>>> included in the libfdt.h
>>
>>
>> We cannot include a BSP headers from cpukit.
>>
> The definition of the function is in bsps/shared/start so we're
> already using code from the bsps/ here. I wonder if this raises the
> question of whether cpukit is the right location for this driver or if
> we should use the bsps codes in this driver as a special case?

I wondered about that too. That's why Niteesh moved it in an earlier
patch set. I think Sebastian said that it would be better in cpukit.

But I'm not that happy with the current solution either. That code
wouldn't even link without the bsp_fdt_get function. Is there an
alternative that we could use that would link?

>>>
 +static void
 +ofw_init(void)
 +{
 +   int rv;
 +   const void *fdt;
 +
 +   fdt = bsp_fdt_get();
 +   rv = ofw_fdt_init(NULL, fdt);
 +
 +   /* Fatal if invalid FDT is provided */
 +   if (rv != 0)
 +   bsp_fatal(LIBFREEBSD_FATAL_INVALID_FDT);
 +}
 +RTEMS_SYSINIT_ITEM(
 +   ofw_init,
 +   RTEMS_SYSINIT_BSP_START,
 +   RTEMS_SYSINIT_ORDER_FIRST
 +);
 +#endif /* __rtems__ */

  static int
  ofw_fdt_init(ofw_t ofw, void *data)
 @@ -297,7 +336,11 @@ ofw_fdt_getprop(ofw_t ofw, phandle_t package, const 
 char *propname, void *buf,
 if (prop == NULL)
 return (-1);

 +#ifndef __rtems__
 bcopy(prop, buf, min(len, buflen));
 +#else /* __rtems__ */
 +   memcpy(buf, prop, MIN(len, buflen));
 +#endif /* __rtems__ */

 return (len);
  }
 @@ -407,6 +450,7 @@ ofw_fdt_package_to_path(ofw_t ofw, phandle_t package, 
 char *buf, size_t len)
 return (-1);
  }

 +#ifndef __rtems__
  #if defined(FDT_MARVELL)
  static int
  ofw_fdt_fixup(ofw_t ofw)
 @@ -476,4 +520,104 @@ ofw_fdt_interpret(ofw_t ofw, const char *cmd, int 
 nret, cell_t *retvals)
  #else
 return (0);
  #endif
 -}
 \ No newline at end of 

Re: [PATCH RTEMS v2 5/6] libfreebsd: Port OFW to RTEMS

2020-06-14 Thread Vijay Kumar Banerjee
On Sat, Jun 13, 2020 at 11:44 PM Niteesh G. S.  wrote:
>
>
>
> On Sat, Jun 13, 2020 at 6:30 PM Vijay Kumar Banerjee  wrote:
>>
>> On Thu, Jun 11, 2020 at 5:53 PM G S Niteesh Babu  
>> wrote:
>> >
>> > The following files have been ported to RTEMS
>> > 1) openfirm.h
>> > 2) openfirm.c
>> > 3) ofw_fdt.c
>> > ---
>> >  cpukit/libfreebsd/dev/ofw/ofw_fdt.c  | 146 ++-
>> >  cpukit/libfreebsd/dev/ofw/openfirm.c |  58 ++-
>> >  cpukit/libfreebsd/dev/ofw/openfirm.h |  17 
>> >  3 files changed, 219 insertions(+), 2 deletions(-)
>> >
>> > diff --git a/cpukit/libfreebsd/dev/ofw/ofw_fdt.c 
>> > b/cpukit/libfreebsd/dev/ofw/ofw_fdt.c
>> > index e4f72e8142..aba170d67a 100644
>> > --- a/cpukit/libfreebsd/dev/ofw/ofw_fdt.c
>> > +++ b/cpukit/libfreebsd/dev/ofw/ofw_fdt.c
>> > @@ -30,9 +30,12 @@
>> >   */
>> >
>> >  #include 
>> > +#ifndef __rtems__
>> >  __FBSDID("$FreeBSD$");
>> > +#endif /* __rtems__ */
>> >
>> >  #include 
>> > +#ifndef __rtems__
>> >  #include 
>> >  #include 
>> >  #include 
>> > @@ -45,9 +48,20 @@ __FBSDID("$FreeBSD$");
>> >  #include 
>> >  #include 
>> >  #include 
>> > +#endif /* __rtems__ */
>> >
>> >  #include "ofw_if.h"
>> >
>> > +#ifdef __rtems__
>> > +#include 
>> > +#include 
>> > +#include 
>> > +#include 
>> > +#include 
>> > +#include "openfirm.h"
>> > +#endif /* __rtems__ */
>> > +
>> > +#ifndef __rtems__
>> >  #ifdef DEBUG
>> >  #define debugf(fmt, args...) do { printf("%s(): ", __func__);  \
>> >  printf(fmt,##args); } while (0)
>> > @@ -63,6 +77,7 @@ __FBSDID("$FreeBSD$");
>> >  #define FDT_MARVELL
>> >  #endif
>> >  #endif
>> > +#endif /* __rtems__ */
>> >
>> >  static int ofw_fdt_init(ofw_t, void *);
>> >  static phandle_t ofw_fdt_peer(ofw_t, phandle_t);
>> > @@ -78,6 +93,7 @@ static ssize_t ofw_fdt_canon(ofw_t, const char *, char 
>> > *, size_t);
>> >  static phandle_t ofw_fdt_finddevice(ofw_t, const char *);
>> >  static ssize_t ofw_fdt_instance_to_path(ofw_t, ihandle_t, char *, size_t);
>> >  static ssize_t ofw_fdt_package_to_path(ofw_t, phandle_t, char *, size_t);
>> > +#ifndef __rtems__
>> >  static int ofw_fdt_interpret(ofw_t, const char *, int, cell_t *);
>> >
>> >  static ofw_method_t ofw_fdt_methods[] = {
>> > @@ -104,9 +120,11 @@ static ofw_def_t ofw_fdt = {
>> > 0
>> >  };
>> >  OFW_DEF(ofw_fdt);
>> > +#endif /* __rtems__ */
>> >
>> >  static void *fdtp = NULL;
>> >
>> > +#ifndef __rtems__
>> >  static int
>> >  sysctl_handle_dtb(SYSCTL_HANDLER_ARGS)
>> >  {
>> > @@ -127,6 +145,27 @@ sysctl_register_fdt_oid(void *arg)
>> > sysctl_handle_dtb, "", "Device Tree Blob");
>> >  }
>> >  SYSINIT(dtb_oid, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_fdt_oid, 
>> > NULL);
>> > +#else /* __rtems__ */
>> > +const void* bsp_fdt_get(void);
>>
>> Why is it declared here? The function declaration is in fdt.h which is
>> included in the libfdt.h
>
>
> We cannot include a BSP headers from cpukit.
>
The definition of the function is in bsps/shared/start so we're
already using code from the bsps/ here. I wonder if this raises the
question of whether cpukit is the right location for this driver or if
we should use the bsps codes in this driver as a special case?
>>
>> > +static void
>> > +ofw_init(void)
>> > +{
>> > +   int rv;
>> > +   const void *fdt;
>> > +
>> > +   fdt = bsp_fdt_get();
>> > +   rv = ofw_fdt_init(NULL, fdt);
>> > +
>> > +   /* Fatal if invalid FDT is provided */
>> > +   if (rv != 0)
>> > +   bsp_fatal(LIBFREEBSD_FATAL_INVALID_FDT);
>> > +}
>> > +RTEMS_SYSINIT_ITEM(
>> > +   ofw_init,
>> > +   RTEMS_SYSINIT_BSP_START,
>> > +   RTEMS_SYSINIT_ORDER_FIRST
>> > +);
>> > +#endif /* __rtems__ */
>> >
>> >  static int
>> >  ofw_fdt_init(ofw_t ofw, void *data)
>> > @@ -297,7 +336,11 @@ ofw_fdt_getprop(ofw_t ofw, phandle_t package, const 
>> > char *propname, void *buf,
>> > if (prop == NULL)
>> > return (-1);
>> >
>> > +#ifndef __rtems__
>> > bcopy(prop, buf, min(len, buflen));
>> > +#else /* __rtems__ */
>> > +   memcpy(buf, prop, MIN(len, buflen));
>> > +#endif /* __rtems__ */
>> >
>> > return (len);
>> >  }
>> > @@ -407,6 +450,7 @@ ofw_fdt_package_to_path(ofw_t ofw, phandle_t package, 
>> > char *buf, size_t len)
>> > return (-1);
>> >  }
>> >
>> > +#ifndef __rtems__
>> >  #if defined(FDT_MARVELL)
>> >  static int
>> >  ofw_fdt_fixup(ofw_t ofw)
>> > @@ -476,4 +520,104 @@ ofw_fdt_interpret(ofw_t ofw, const char *cmd, int 
>> > nret, cell_t *retvals)
>> >  #else
>> > return (0);
>> >  #endif
>> > -}
>> > \ No newline at end of file
>> > +}
>> > +#endif /* __rtems__ */
>> > +
>> > +#ifdef __rtems__
>> > +int
>> > +OFW_INIT(ofw_t ofw_obj, void *cookie)
>> > +{
>> > +
>> > +   return (ofw_fdt_init(ofw_obj, cookie));
>> > +}
>> > +
>> > +phandle_t
>> > +OFW_PEER(ofw_t ofw_obj, phandle_t node)
>> > +{
>> > +
>> > +   return (ofw_fdt_peer(ofw_obj, node));
>> > +}
>> > +
>> > +phandle_t
>> > +OFW_CHILD(ofw_t 

Re: [PATCH RTEMS v2 5/6] libfreebsd: Port OFW to RTEMS

2020-06-13 Thread Niteesh G. S.
On Sat, Jun 13, 2020 at 6:30 PM Vijay Kumar Banerjee 
wrote:

> On Thu, Jun 11, 2020 at 5:53 PM G S Niteesh Babu 
> wrote:
> >
> > The following files have been ported to RTEMS
> > 1) openfirm.h
> > 2) openfirm.c
> > 3) ofw_fdt.c
> > ---
> >  cpukit/libfreebsd/dev/ofw/ofw_fdt.c  | 146 ++-
> >  cpukit/libfreebsd/dev/ofw/openfirm.c |  58 ++-
> >  cpukit/libfreebsd/dev/ofw/openfirm.h |  17 
> >  3 files changed, 219 insertions(+), 2 deletions(-)
> >
> > diff --git a/cpukit/libfreebsd/dev/ofw/ofw_fdt.c
> b/cpukit/libfreebsd/dev/ofw/ofw_fdt.c
> > index e4f72e8142..aba170d67a 100644
> > --- a/cpukit/libfreebsd/dev/ofw/ofw_fdt.c
> > +++ b/cpukit/libfreebsd/dev/ofw/ofw_fdt.c
> > @@ -30,9 +30,12 @@
> >   */
> >
> >  #include 
> > +#ifndef __rtems__
> >  __FBSDID("$FreeBSD$");
> > +#endif /* __rtems__ */
> >
> >  #include 
> > +#ifndef __rtems__
> >  #include 
> >  #include 
> >  #include 
> > @@ -45,9 +48,20 @@ __FBSDID("$FreeBSD$");
> >  #include 
> >  #include 
> >  #include 
> > +#endif /* __rtems__ */
> >
> >  #include "ofw_if.h"
> >
> > +#ifdef __rtems__
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include "openfirm.h"
> > +#endif /* __rtems__ */
> > +
> > +#ifndef __rtems__
> >  #ifdef DEBUG
> >  #define debugf(fmt, args...) do { printf("%s(): ", __func__);  \
> >  printf(fmt,##args); } while (0)
> > @@ -63,6 +77,7 @@ __FBSDID("$FreeBSD$");
> >  #define FDT_MARVELL
> >  #endif
> >  #endif
> > +#endif /* __rtems__ */
> >
> >  static int ofw_fdt_init(ofw_t, void *);
> >  static phandle_t ofw_fdt_peer(ofw_t, phandle_t);
> > @@ -78,6 +93,7 @@ static ssize_t ofw_fdt_canon(ofw_t, const char *, char
> *, size_t);
> >  static phandle_t ofw_fdt_finddevice(ofw_t, const char *);
> >  static ssize_t ofw_fdt_instance_to_path(ofw_t, ihandle_t, char *,
> size_t);
> >  static ssize_t ofw_fdt_package_to_path(ofw_t, phandle_t, char *,
> size_t);
> > +#ifndef __rtems__
> >  static int ofw_fdt_interpret(ofw_t, const char *, int, cell_t *);
> >
> >  static ofw_method_t ofw_fdt_methods[] = {
> > @@ -104,9 +120,11 @@ static ofw_def_t ofw_fdt = {
> > 0
> >  };
> >  OFW_DEF(ofw_fdt);
> > +#endif /* __rtems__ */
> >
> >  static void *fdtp = NULL;
> >
> > +#ifndef __rtems__
> >  static int
> >  sysctl_handle_dtb(SYSCTL_HANDLER_ARGS)
> >  {
> > @@ -127,6 +145,27 @@ sysctl_register_fdt_oid(void *arg)
> > sysctl_handle_dtb, "", "Device Tree Blob");
> >  }
> >  SYSINIT(dtb_oid, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_fdt_oid,
> NULL);
> > +#else /* __rtems__ */
> > +const void* bsp_fdt_get(void);
>
> Why is it declared here? The function declaration is in fdt.h which is
> included in the libfdt.h
>

We cannot include a BSP headers from cpukit.


> > +static void
> > +ofw_init(void)
> > +{
> > +   int rv;
> > +   const void *fdt;
> > +
> > +   fdt = bsp_fdt_get();
> > +   rv = ofw_fdt_init(NULL, fdt);
> > +
> > +   /* Fatal if invalid FDT is provided */
> > +   if (rv != 0)
> > +   bsp_fatal(LIBFREEBSD_FATAL_INVALID_FDT);
> > +}
> > +RTEMS_SYSINIT_ITEM(
> > +   ofw_init,
> > +   RTEMS_SYSINIT_BSP_START,
> > +   RTEMS_SYSINIT_ORDER_FIRST
> > +);
> > +#endif /* __rtems__ */
> >
> >  static int
> >  ofw_fdt_init(ofw_t ofw, void *data)
> > @@ -297,7 +336,11 @@ ofw_fdt_getprop(ofw_t ofw, phandle_t package, const
> char *propname, void *buf,
> > if (prop == NULL)
> > return (-1);
> >
> > +#ifndef __rtems__
> > bcopy(prop, buf, min(len, buflen));
> > +#else /* __rtems__ */
> > +   memcpy(buf, prop, MIN(len, buflen));
> > +#endif /* __rtems__ */
> >
> > return (len);
> >  }
> > @@ -407,6 +450,7 @@ ofw_fdt_package_to_path(ofw_t ofw, phandle_t
> package, char *buf, size_t len)
> > return (-1);
> >  }
> >
> > +#ifndef __rtems__
> >  #if defined(FDT_MARVELL)
> >  static int
> >  ofw_fdt_fixup(ofw_t ofw)
> > @@ -476,4 +520,104 @@ ofw_fdt_interpret(ofw_t ofw, const char *cmd, int
> nret, cell_t *retvals)
> >  #else
> > return (0);
> >  #endif
> > -}
> > \ No newline at end of file
> > +}
> > +#endif /* __rtems__ */
> > +
> > +#ifdef __rtems__
> > +int
> > +OFW_INIT(ofw_t ofw_obj, void *cookie)
> > +{
> > +
> > +   return (ofw_fdt_init(ofw_obj, cookie));
> > +}
> > +
> > +phandle_t
> > +OFW_PEER(ofw_t ofw_obj, phandle_t node)
> > +{
> > +
> > +   return (ofw_fdt_peer(ofw_obj, node));
> > +}
> > +
> > +phandle_t
> > +OFW_CHILD(ofw_t ofw_obj, phandle_t node)
> > +{
> > +
> > +   return (ofw_fdt_child(ofw_obj, node));
> > +}
> > +
> > +phandle_t
> > +OFW_PARENT(ofw_t ofw_obj, phandle_t node)
> > +{
> > +
> > +   return (ofw_fdt_parent(ofw_obj, node));
> > +}
> > +
> > +phandle_t
> > +OFW_INSTANCE_TO_PACKAGE(ofw_t ofw_obj, ihandle_t instance)
> > +{
> > +
> > +   return (ofw_fdt_instance_to_package(ofw_obj, instance));
> > +}
> > +
> > +ssize_t
> > +OFW_GETPROPLEN(ofw_t ofw_obj, phandle_t package, const char *propname)
> > +{
> > +
> > 

Re: [PATCH RTEMS v2 5/6] libfreebsd: Port OFW to RTEMS

2020-06-13 Thread Vijay Kumar Banerjee
On Thu, Jun 11, 2020 at 5:53 PM G S Niteesh Babu  wrote:
>
> The following files have been ported to RTEMS
> 1) openfirm.h
> 2) openfirm.c
> 3) ofw_fdt.c
> ---
>  cpukit/libfreebsd/dev/ofw/ofw_fdt.c  | 146 ++-
>  cpukit/libfreebsd/dev/ofw/openfirm.c |  58 ++-
>  cpukit/libfreebsd/dev/ofw/openfirm.h |  17 
>  3 files changed, 219 insertions(+), 2 deletions(-)
>
> diff --git a/cpukit/libfreebsd/dev/ofw/ofw_fdt.c 
> b/cpukit/libfreebsd/dev/ofw/ofw_fdt.c
> index e4f72e8142..aba170d67a 100644
> --- a/cpukit/libfreebsd/dev/ofw/ofw_fdt.c
> +++ b/cpukit/libfreebsd/dev/ofw/ofw_fdt.c
> @@ -30,9 +30,12 @@
>   */
>
>  #include 
> +#ifndef __rtems__
>  __FBSDID("$FreeBSD$");
> +#endif /* __rtems__ */
>
>  #include 
> +#ifndef __rtems__
>  #include 
>  #include 
>  #include 
> @@ -45,9 +48,20 @@ __FBSDID("$FreeBSD$");
>  #include 
>  #include 
>  #include 
> +#endif /* __rtems__ */
>
>  #include "ofw_if.h"
>
> +#ifdef __rtems__
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include "openfirm.h"
> +#endif /* __rtems__ */
> +
> +#ifndef __rtems__
>  #ifdef DEBUG
>  #define debugf(fmt, args...) do { printf("%s(): ", __func__);  \
>  printf(fmt,##args); } while (0)
> @@ -63,6 +77,7 @@ __FBSDID("$FreeBSD$");
>  #define FDT_MARVELL
>  #endif
>  #endif
> +#endif /* __rtems__ */
>
>  static int ofw_fdt_init(ofw_t, void *);
>  static phandle_t ofw_fdt_peer(ofw_t, phandle_t);
> @@ -78,6 +93,7 @@ static ssize_t ofw_fdt_canon(ofw_t, const char *, char *, 
> size_t);
>  static phandle_t ofw_fdt_finddevice(ofw_t, const char *);
>  static ssize_t ofw_fdt_instance_to_path(ofw_t, ihandle_t, char *, size_t);
>  static ssize_t ofw_fdt_package_to_path(ofw_t, phandle_t, char *, size_t);
> +#ifndef __rtems__
>  static int ofw_fdt_interpret(ofw_t, const char *, int, cell_t *);
>
>  static ofw_method_t ofw_fdt_methods[] = {
> @@ -104,9 +120,11 @@ static ofw_def_t ofw_fdt = {
> 0
>  };
>  OFW_DEF(ofw_fdt);
> +#endif /* __rtems__ */
>
>  static void *fdtp = NULL;
>
> +#ifndef __rtems__
>  static int
>  sysctl_handle_dtb(SYSCTL_HANDLER_ARGS)
>  {
> @@ -127,6 +145,27 @@ sysctl_register_fdt_oid(void *arg)
> sysctl_handle_dtb, "", "Device Tree Blob");
>  }
>  SYSINIT(dtb_oid, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_fdt_oid, NULL);
> +#else /* __rtems__ */
> +const void* bsp_fdt_get(void);

Why is it declared here? The function declaration is in fdt.h which is
included in the libfdt.h
> +static void
> +ofw_init(void)
> +{
> +   int rv;
> +   const void *fdt;
> +
> +   fdt = bsp_fdt_get();
> +   rv = ofw_fdt_init(NULL, fdt);
> +
> +   /* Fatal if invalid FDT is provided */
> +   if (rv != 0)
> +   bsp_fatal(LIBFREEBSD_FATAL_INVALID_FDT);
> +}
> +RTEMS_SYSINIT_ITEM(
> +   ofw_init,
> +   RTEMS_SYSINIT_BSP_START,
> +   RTEMS_SYSINIT_ORDER_FIRST
> +);
> +#endif /* __rtems__ */
>
>  static int
>  ofw_fdt_init(ofw_t ofw, void *data)
> @@ -297,7 +336,11 @@ ofw_fdt_getprop(ofw_t ofw, phandle_t package, const char 
> *propname, void *buf,
> if (prop == NULL)
> return (-1);
>
> +#ifndef __rtems__
> bcopy(prop, buf, min(len, buflen));
> +#else /* __rtems__ */
> +   memcpy(buf, prop, MIN(len, buflen));
> +#endif /* __rtems__ */
>
> return (len);
>  }
> @@ -407,6 +450,7 @@ ofw_fdt_package_to_path(ofw_t ofw, phandle_t package, 
> char *buf, size_t len)
> return (-1);
>  }
>
> +#ifndef __rtems__
>  #if defined(FDT_MARVELL)
>  static int
>  ofw_fdt_fixup(ofw_t ofw)
> @@ -476,4 +520,104 @@ ofw_fdt_interpret(ofw_t ofw, const char *cmd, int nret, 
> cell_t *retvals)
>  #else
> return (0);
>  #endif
> -}
> \ No newline at end of file
> +}
> +#endif /* __rtems__ */
> +
> +#ifdef __rtems__
> +int
> +OFW_INIT(ofw_t ofw_obj, void *cookie)
> +{
> +
> +   return (ofw_fdt_init(ofw_obj, cookie));
> +}
> +
> +phandle_t
> +OFW_PEER(ofw_t ofw_obj, phandle_t node)
> +{
> +
> +   return (ofw_fdt_peer(ofw_obj, node));
> +}
> +
> +phandle_t
> +OFW_CHILD(ofw_t ofw_obj, phandle_t node)
> +{
> +
> +   return (ofw_fdt_child(ofw_obj, node));
> +}
> +
> +phandle_t
> +OFW_PARENT(ofw_t ofw_obj, phandle_t node)
> +{
> +
> +   return (ofw_fdt_parent(ofw_obj, node));
> +}
> +
> +phandle_t
> +OFW_INSTANCE_TO_PACKAGE(ofw_t ofw_obj, ihandle_t instance)
> +{
> +
> +   return (ofw_fdt_instance_to_package(ofw_obj, instance));
> +}
> +
> +ssize_t
> +OFW_GETPROPLEN(ofw_t ofw_obj, phandle_t package, const char *propname)
> +{
> +
> +   return (ofw_fdt_getproplen(ofw_obj, package, propname));
> +}
> +
> +ssize_t
> +OFW_GETPROP(ofw_t ofw_obj, phandle_t package, const char *propname,
> +void *buf, size_t buflen
> +)
> +{
> +
> +   return (ofw_fdt_getprop(ofw_obj, package, propname, buf, buflen));
> +}
> +
> +int
> +OFW_NEXTPROP(ofw_t ofw_obj, phandle_t package, const char *prev, char *buf,
> +size_t size)
> +{
> +
> +   return (ofw_fdt_nextprop(ofw_obj, package, prev, buf, 

[PATCH RTEMS v2 5/6] libfreebsd: Port OFW to RTEMS

2020-06-11 Thread G S Niteesh Babu
The following files have been ported to RTEMS
1) openfirm.h
2) openfirm.c
3) ofw_fdt.c
---
 cpukit/libfreebsd/dev/ofw/ofw_fdt.c  | 146 ++-
 cpukit/libfreebsd/dev/ofw/openfirm.c |  58 ++-
 cpukit/libfreebsd/dev/ofw/openfirm.h |  17 
 3 files changed, 219 insertions(+), 2 deletions(-)

diff --git a/cpukit/libfreebsd/dev/ofw/ofw_fdt.c 
b/cpukit/libfreebsd/dev/ofw/ofw_fdt.c
index e4f72e8142..aba170d67a 100644
--- a/cpukit/libfreebsd/dev/ofw/ofw_fdt.c
+++ b/cpukit/libfreebsd/dev/ofw/ofw_fdt.c
@@ -30,9 +30,12 @@
  */
 
 #include 
+#ifndef __rtems__
 __FBSDID("$FreeBSD$");
+#endif /* __rtems__ */
 
 #include 
+#ifndef __rtems__
 #include 
 #include 
 #include 
@@ -45,9 +48,20 @@ __FBSDID("$FreeBSD$");
 #include 
 #include 
 #include 
+#endif /* __rtems__ */
 
 #include "ofw_if.h"
 
+#ifdef __rtems__
+#include 
+#include 
+#include 
+#include 
+#include 
+#include "openfirm.h"
+#endif /* __rtems__ */
+
+#ifndef __rtems__
 #ifdef DEBUG
 #define debugf(fmt, args...) do { printf("%s(): ", __func__);  \
 printf(fmt,##args); } while (0)
@@ -63,6 +77,7 @@ __FBSDID("$FreeBSD$");
 #define FDT_MARVELL
 #endif
 #endif
+#endif /* __rtems__ */
 
 static int ofw_fdt_init(ofw_t, void *);
 static phandle_t ofw_fdt_peer(ofw_t, phandle_t);
@@ -78,6 +93,7 @@ static ssize_t ofw_fdt_canon(ofw_t, const char *, char *, 
size_t);
 static phandle_t ofw_fdt_finddevice(ofw_t, const char *);
 static ssize_t ofw_fdt_instance_to_path(ofw_t, ihandle_t, char *, size_t);
 static ssize_t ofw_fdt_package_to_path(ofw_t, phandle_t, char *, size_t);
+#ifndef __rtems__
 static int ofw_fdt_interpret(ofw_t, const char *, int, cell_t *);
 
 static ofw_method_t ofw_fdt_methods[] = {
@@ -104,9 +120,11 @@ static ofw_def_t ofw_fdt = {
0
 };
 OFW_DEF(ofw_fdt);
+#endif /* __rtems__ */
 
 static void *fdtp = NULL;
 
+#ifndef __rtems__
 static int
 sysctl_handle_dtb(SYSCTL_HANDLER_ARGS)
 {
@@ -127,6 +145,27 @@ sysctl_register_fdt_oid(void *arg)
sysctl_handle_dtb, "", "Device Tree Blob");
 }
 SYSINIT(dtb_oid, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_fdt_oid, NULL);
+#else /* __rtems__ */
+const void* bsp_fdt_get(void);
+static void
+ofw_init(void)
+{
+   int rv;
+   const void *fdt;
+
+   fdt = bsp_fdt_get();
+   rv = ofw_fdt_init(NULL, fdt);
+
+   /* Fatal if invalid FDT is provided */
+   if (rv != 0)
+   bsp_fatal(LIBFREEBSD_FATAL_INVALID_FDT);
+}
+RTEMS_SYSINIT_ITEM(
+   ofw_init,
+   RTEMS_SYSINIT_BSP_START,
+   RTEMS_SYSINIT_ORDER_FIRST
+);
+#endif /* __rtems__ */
 
 static int
 ofw_fdt_init(ofw_t ofw, void *data)
@@ -297,7 +336,11 @@ ofw_fdt_getprop(ofw_t ofw, phandle_t package, const char 
*propname, void *buf,
if (prop == NULL)
return (-1);
 
+#ifndef __rtems__
bcopy(prop, buf, min(len, buflen));
+#else /* __rtems__ */
+   memcpy(buf, prop, MIN(len, buflen));
+#endif /* __rtems__ */
 
return (len);
 }
@@ -407,6 +450,7 @@ ofw_fdt_package_to_path(ofw_t ofw, phandle_t package, char 
*buf, size_t len)
return (-1);
 }
 
+#ifndef __rtems__
 #if defined(FDT_MARVELL)
 static int
 ofw_fdt_fixup(ofw_t ofw)
@@ -476,4 +520,104 @@ ofw_fdt_interpret(ofw_t ofw, const char *cmd, int nret, 
cell_t *retvals)
 #else
return (0);
 #endif
-}
\ No newline at end of file
+}
+#endif /* __rtems__ */
+
+#ifdef __rtems__
+int
+OFW_INIT(ofw_t ofw_obj, void *cookie)
+{
+
+   return (ofw_fdt_init(ofw_obj, cookie));
+}
+
+phandle_t
+OFW_PEER(ofw_t ofw_obj, phandle_t node)
+{
+
+   return (ofw_fdt_peer(ofw_obj, node));
+}
+
+phandle_t
+OFW_CHILD(ofw_t ofw_obj, phandle_t node)
+{
+
+   return (ofw_fdt_child(ofw_obj, node));
+}
+
+phandle_t
+OFW_PARENT(ofw_t ofw_obj, phandle_t node)
+{
+
+   return (ofw_fdt_parent(ofw_obj, node));
+}
+
+phandle_t
+OFW_INSTANCE_TO_PACKAGE(ofw_t ofw_obj, ihandle_t instance)
+{
+
+   return (ofw_fdt_instance_to_package(ofw_obj, instance));
+}
+
+ssize_t
+OFW_GETPROPLEN(ofw_t ofw_obj, phandle_t package, const char *propname)
+{
+
+   return (ofw_fdt_getproplen(ofw_obj, package, propname));
+}
+
+ssize_t
+OFW_GETPROP(ofw_t ofw_obj, phandle_t package, const char *propname,
+void *buf, size_t buflen
+)
+{
+
+   return (ofw_fdt_getprop(ofw_obj, package, propname, buf, buflen));
+}
+
+int
+OFW_NEXTPROP(ofw_t ofw_obj, phandle_t package, const char *prev, char *buf,
+size_t size)
+{
+
+   return (ofw_fdt_nextprop(ofw_obj, package, prev, buf, size));
+}
+
+int
+OFW_SETPROP(ofw_t ofw_obj, phandle_t package, const char *propname,
+const void *buf, size_t len)
+{
+
+   return (ofw_fdt_setprop(ofw_obj, package, propname, buf, len));
+}
+
+ssize_t
+OFW_CANON(ofw_t ofw_obj, const char *device, char *buf, size_t len)
+{
+
+   return (ofw_fdt_canon(ofw_obj, device, buf, len));
+}
+
+phandle_t
+OFW_FINDDEVICE(ofw_t ofw_obj, const char *device)
+{
+
+   return (ofw_fdt_finddevice(ofw_obj, device));
+}
+
+ssize_t
+OFW_INSTANCE_TO_PATH(ofw_t ofw_obj,