<div dir="ltr"><div dir="ltr"><div class="gmail_default" style="font-size:small"><br></div></div><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sun, Aug 16, 2020 at 2:25 PM Chris Johns <<a href="mailto:chrisj@rtems.org">chrisj@rtems.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On 16/8/20 4:49 am, G S Niteesh Babu wrote:<br>
> ---<br><br>
> +<br>
> +#ifdef HAVE_CONFIG_H<br>
> +#include "config.h"<br>
> +#endif<br>
> +<br>
> +#include <bsp/fdt.h><br>
> +#include <sys/param.h><br>
> +#include <ofw/ofw.h><br>
> +#include <libfdt.h><br>
> +#include <assert.h><br>
> +#include <rtems/sysinit.h><br>
> +<br>
> +const void *fdtp = NULL;<br>
> +<br>
> +static phandle_t rtems_fdt_offset_to_phandle( int offset )<br>
> +{<br>
> + if (offset < 0) {<br>
> + return 0;<br>
> + }<br>
> +<br>
> + return (phandle_t)offset + fdt_off_dt_struct(fdtp);<br>
> +}<br>
> +<br>
> +static int rtems_fdt_phandle_to_offset( phandle_t handle )<br>
> +{<br>
> + int off;<br>
> + int fdt_off;<br>
> +<br>
> + off = (int) handle;<br>
> + fdt_off = fdt_off_dt_struct(fdtp);<br>
> +<br>
> + if (off < fdt_off) {<br>
> + return -1;<br>
> +<br>
> + }<br>
> +<br>
> + return off - fdt_off;<br>
> +}<br>
> +<br>
> +static void rtems_ofw_init( void ) {<br>
> + int rv;<br>
> + const void *fdt;<br>
> + <br>
> + fdt = bsp_fdt_get();<br>
> +<br>
> + rv = fdt_check_header(fdt);<br>
> +<br>
> + /*<br>
> + * Is assert a good option to handle error?<br>
> + * AFIAK there is no other way to recover from invalid FDT.<br>
> + * On providing an invalid FDT most driver's will fail/crash anyway.<br>
> + */<br>
<br>
If you want to fail with an error I suggest a documented internal error. I<br>
however would prefer we fail in degraded manner but I understand this may not be<br>
possible.<br></blockquote><div><br></div><div class="gmail_default" style="font-size:small">During the initial discussion, we discussed adding a new error code in case of an</div><div class="gmail_default" style="font-size:small">invalid FDT, something like BSP_INVALID_FDT. I had also sent a patch but then we</div><div class="gmail_default" style="font-size:small">decided not to add one(for some reason that I don't remember). Instead, use</div><div class="gmail_default" style="font-size:small">RTEMS_NOT_CONFIGURED. This has also been used in my previous OFW patch.</div><div class="gmail_default" style="font-size:small">So should we add a new error code like BSP_INVALID_FDT and fatal exit or just</div><div class="gmail_default" style="font-size:small">use RTEMS_NOT_CONIGURED like the previous patch?</div><div class="gmail_default" style="font-size:small"></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
> + assert(rv == 0);<br>
<br>
I would prefer we did not make this code dependent on -DNDEBUG.<br>
<br>
Chris<br>
<br>
> +<br>
> + fdtp = fdt;<br>
> +}<br>
> +<br>
> +RTEMS_SYSINIT_ITEM(<br>
> + rtems_ofw_init,<br>
> + RTEMS_SYSINIT_BSP_PRE_DRIVERS,<br>
> + RTEMS_SYSINIT_ORDER_FIRST<br>
> +);<br>
> +<br>
> +phandle_t rtems_ofw_peer( phandle_t node )<br>
> +{<br>
> + int offset;<br>
> +<br>
> + if (node == 0) {<br>
> + int root = fdt_path_offset(fdtp, "/");<br>
> + return rtems_fdt_offset_to_phandle(root);<br>
> + }<br>
> +<br>
> + offset = rtems_fdt_phandle_to_offset(node);<br>
> + if (offset < 0) {<br>
> + return 0;<br>
> + }<br>
> +<br>
> + offset = fdt_next_subnode(fdtp, offset);<br>
> + return rtems_fdt_offset_to_phandle(offset);<br>
> +}<br>
> +<br>
> +phandle_t rtems_ofw_child( phandle_t node )<br>
> +{<br>
> + int offset;<br>
> +<br>
> + offset = rtems_fdt_phandle_to_offset(node);<br>
> +<br>
> + if (offset < 0) {<br>
> + return 0;<br>
> + }<br>
> +<br>
> + offset = fdt_first_subnode(fdtp, offset);<br>
> + return rtems_fdt_offset_to_phandle(offset);<br>
> +}<br>
> +<br>
> +phandle_t rtems_ofw_parent( phandle_t node )<br>
> +{<br>
> + int offset;<br>
> +<br>
> + offset = rtems_fdt_phandle_to_offset(node);<br>
> +<br>
> + if (offset < 0) {<br>
> + return 0;<br>
> + }<br>
> +<br>
> + offset = fdt_parent_offset(fdtp, offset);<br>
> + return rtems_fdt_offset_to_phandle(offset);<br>
> +}<br>
> +<br>
> +size_t rtems_ofw_get_prop_len(<br>
> + phandle_t node,<br>
> + const char *propname<br>
> +)<br>
> +{<br>
> + int offset;<br>
> + int len;<br>
> + const void *prop;<br>
> +<br>
> + offset = rtems_fdt_phandle_to_offset(node);<br>
> +<br>
> + if (offset < 0) {<br>
> + return -1;<br>
> + }<br>
> +<br>
> + prop = fdt_getprop(fdtp, offset, propname, &len);<br>
> +<br>
> + if (prop == NULL && strcmp(propname, "name") == 0) {<br>
> + fdt_get_name(fdtp, offset, &len);<br>
> + return len + 1;<br>
> + }<br>
> +<br>
> + if (prop == NULL && strcmp(propname, "/chosen") == 0) {<br>
> + if (strcmp(propname, "fdtbootcpu") == 0)<br>
> + return sizeof(pcell_t);<br>
> + if (strcmp(propname, "fdtmemreserv") == 0)<br>
> + return 2 * sizeof(uint64_t) * fdt_num_mem_rsv(fdtp);<br>
> + }<br>
> +<br>
> + if (prop == NULL) {<br>
> + return -1;<br>
> + }<br>
> +<br>
> + return len;<br>
> +}<br>
> +<br>
> +size_t rtems_ofw_get_prop(<br>
> + phandle_t node,<br>
> + const char *propname,<br>
> + void *buf,<br>
> + size_t bufsize<br>
> +)<br>
> +{<br>
> + int offset;<br>
> + int len;<br>
> + const void *prop;<br>
> +<br>
> + offset = rtems_fdt_phandle_to_offset(node);<br>
> +<br>
> + if (offset < 0) {<br>
> + return -1;<br>
> + }<br>
> +<br>
> + prop = fdt_getprop(fdtp, offset, propname, &len);<br>
> +<br>
> + if (prop == NULL && strcmp(propname, "name") == 0) {<br>
> + fdt_get_name(fdtp, offset, &len);<br>
> + return len + 1;<br>
> + }<br>
> +<br>
> + if (prop == NULL && strcmp(propname, "/chosen") == 0) {<br>
> + if (strcmp(propname, "fdtbootcpu") == 0)<br>
> + return sizeof(pcell_t);<br>
> + if (strcmp(propname, "fdtmemreserv") == 0)<br>
> + return 2 * sizeof(uint64_t) * fdt_num_mem_rsv(fdtp);<br>
> + }<br>
> +<br>
> + if (prop == NULL) {<br>
> + return -1;<br>
> + }<br>
> +<br>
> + bcopy(prop, buf, MIN(len, bufsize));<br>
> +<br>
> + return len;<br>
> +}<br>
> +<br>
> +size_t rtems_ofw_get_enc_prop(<br>
> + phandle_t node,<br>
> + const char *prop,<br>
> + pcell_t *buf,<br>
> + size_t len<br>
> +)<br>
> +{<br>
> + size_t rv;<br>
> +<br>
> + assert(len % 4 == 0);<br>
> + rv = rtems_ofw_get_prop(node, prop, buf, len);<br>
> +<br>
> + if (rv < 0) {<br>
> + return rv;<br>
> + }<br>
> +<br>
> + for (int i = 0; i < (len / 4); i++) {<br>
> + buf[i] = fdt32_to_cpu(buf[i]);<br>
> + }<br>
> +<br>
> + return rv;<br>
> +}<br>
> +<br>
> +int rtems_ofw_has_prop(<br>
> + phandle_t node,<br>
> + const char *propname<br>
> +)<br>
> +{<br>
> + size_t rv;<br>
> +<br>
> + rv = rtems_ofw_get_prop_len(node, propname);<br>
> + return rv >= 0 ? 1 : 0;<br>
> +}<br>
> +<br>
> +size_t rtems_ofw_search_prop(<br>
> + phandle_t node,<br>
> + const char *propname,<br>
> + void *buf,<br>
> + size_t len<br>
> +)<br>
> +{<br>
> + size_t rv;<br>
> +<br>
> + for (; node != 0; node = rtems_ofw_parent(node)) {<br>
> + if ((rv = rtems_ofw_get_prop(node, propname, buf, len) != -1)) {<br>
> + return rv;<br>
> + }<br>
> + }<br>
> +<br>
> + return -1;<br>
> +}<br>
> +<br>
> +size_t rtems_ofw_search_enc_prop(<br>
> + phandle_t node,<br>
> + const char *propname,<br>
> + pcell_t *buf,<br>
> + size_t len<br>
> +)<br>
> +{<br>
> + size_t rv;<br>
> +<br>
> + for (; node != 0; node = rtems_ofw_parent(node)) {<br>
> + if ((rv = rtems_ofw_get_enc_prop(node, propname, buf, len) != -1)) {<br>
> + return rv;<br>
> + }<br>
> + }<br>
> +<br>
> + return -1;<br>
> +}<br>
> +<br>
> +size_t rtems_ofw_get_prop_alloc(<br>
> + phandle_t node,<br>
> + const char *propname,<br>
> + void **buf<br>
> +)<br>
> +{<br>
> + size_t len;<br>
> +<br>
> + if ((len = rtems_ofw_get_prop_len(node, propname)) == -1) {<br>
> + return -1;<br>
> + }<br>
> +<br>
> + *buf = NULL;<br>
> +<br>
> + if (len > 0) {<br>
> + *buf = malloc(len);<br>
> +<br>
> + if (rtems_ofw_get_prop(node, propname, *buf, len) == -1) {<br>
> + rtems_ofw_free(buf);<br>
> + *buf = NULL;<br>
> + return -1;<br>
> + }<br>
> + }<br>
> +<br>
> + return len;<br>
> +}<br>
> +<br>
> +size_t rtems_ofw_get_prop_alloc_multi(<br>
> + phandle_t node,<br>
> + const char *propname,<br>
> + int elsz,<br>
> + void **buf<br>
> +)<br>
> +{<br>
> + size_t len;<br>
> +<br>
> + if ((len = rtems_ofw_get_prop_len(node, propname)) == -1 ||<br>
> + (len % elsz != 0)) {<br>
> + return -1;<br>
> + }<br>
> +<br>
> + *buf = NULL;<br>
> +<br>
> + if (len > 0) {<br>
> + *buf = malloc(len);<br>
> +<br>
> + if (rtems_ofw_get_prop(node, propname, *buf, len) == -1) {<br>
> + rtems_ofw_free(buf);<br>
> + *buf = NULL;<br>
> + return -1;<br>
> + }<br>
> + }<br>
> +<br>
> + return (len / elsz);<br>
> +}<br>
> +<br>
> +size_t rtems_ofw_get_enc_prop_alloc(<br>
> + phandle_t node,<br>
> + const char *propname,<br>
> + void **buf<br>
> +)<br>
> +{<br>
> + size_t len;<br>
> +<br>
> + if ((len = rtems_ofw_get_prop_len(node, propname)) == -1) {<br>
> + return -1;<br>
> + }<br>
> +<br>
> + *buf = NULL;<br>
> +<br>
> + if (len > 0) {<br>
> + *buf = malloc(len);<br>
> +<br>
> + if (rtems_ofw_get_enc_prop(node, propname, *buf, len) == -1) {<br>
> + rtems_ofw_free(buf);<br>
> + *buf = NULL;<br>
> + return -1;<br>
> + }<br>
> + }<br>
> +<br>
> + return len;<br>
> +}<br>
> +<br>
> +size_t rtems_ofw_get_enc_prop_alloc_multi(<br>
> + phandle_t node,<br>
> + const char *propname,<br>
> + int elsz,<br>
> + void **buf<br>
> +)<br>
> +{<br>
> + size_t len;<br>
> +<br>
> + if ((len = rtems_ofw_get_prop_len(node, propname)) == -1 ||<br>
> + (len % elsz != 0)) {<br>
> + return -1;<br>
> + }<br>
> +<br>
> + *buf = NULL;<br>
> +<br>
> + if (len > 0) {<br>
> + *buf = malloc(len);<br>
> +<br>
> + if (rtems_ofw_get_enc_prop(node, propname, *buf, len) == -1) {<br>
> + rtems_ofw_free(buf);<br>
> + *buf = NULL;<br>
> + return -1;<br>
> + }<br>
> + }<br>
> +<br>
> + return (len / elsz);<br>
> +}<br>
> +<br>
> +void rtems_ofw_free( void *buf )<br>
> +{<br>
> + free(buf);<br>
> +}<br>
> +<br>
> +int rtems_ofw_next_prop(<br>
> + phandle_t node,<br>
> + const char *previous,<br>
> + char *buf,<br>
> + size_t len<br>
> +)<br>
> +{<br>
> + const void *name;<br>
> + const void *prop;<br>
> + int offset;<br>
> +<br>
> + offset = rtems_fdt_phandle_to_offset(node);<br>
> +<br>
> + if (offset < 0) {<br>
> + return -1;<br>
> + }<br>
> +<br>
> + if (previous == NULL) {<br>
> + offset = fdt_first_property_offset(fdtp, offset);<br>
> + } else {<br>
> + fdt_for_each_property_offset(offset, fdtp, offset) {<br>
> + prop = fdt_getprop_by_offset(fdtp, offset, (const char **)&name, NULL);<br>
> + if (prop == NULL)<br>
> + return -1;<br>
> +<br>
> + if (strcmp(previous, name) != 0)<br>
> + continue;<br>
> +<br>
> + offset = fdt_next_property_offset(fdtp, offset);<br>
> + break;<br>
> + }<br>
> + }<br>
> +<br>
> + if (offset < 0)<br>
> + return 0;<br>
> +<br>
> + prop = fdt_getprop_by_offset(fdtp, offset, (const char **)&name, &offset);<br>
> + if (prop == NULL)<br>
> + return -1;<br>
> +<br>
> + strncpy(buf, name, len);<br>
> +<br>
> + return 1;<br>
> +}<br>
> +<br>
> +int rtems_ofw_set_prop(<br>
> + phandle_t node,<br>
> + const char *name,<br>
> + const void *buf,<br>
> + size_t len<br>
> +)<br>
> +{<br>
> + int offset;<br>
> +<br>
> + offset = rtems_fdt_phandle_to_offset(node);<br>
> +<br>
> + if (offset < 0)<br>
> + return -1;<br>
> +<br>
> + if (fdt_setprop_inplace(fdtp, offset, name, buf, len) != 0)<br>
> + return (fdt_setprop(fdtp, offset, name, buf, len));<br>
> +<br>
> + return 0;<br>
> +}<br>
> +<br>
> +phandle_t rtems_ofw_find_device( const char *path )<br>
> +{<br>
> + int offset;<br>
> +<br>
> + offset = fdt_path_offset(fdtp, path);<br>
> + if (offset < 0)<br>
> + return -1;<br>
> +<br>
> + return rtems_fdt_offset_to_phandle(offset);<br>
> +}<br>
> +<br>
> +static phandle_t rtems_ofw_get_effective_phandle(<br>
> + phandle_t node,<br>
> + phandle_t xref<br>
> +)<br>
> +{<br>
> + phandle_t child;<br>
> + phandle_t ref;<br>
> +<br>
> + for (child = rtems_ofw_child(node); child != 0; child = rtems_ofw_peer(node)) {<br>
> + ref = rtems_ofw_get_effective_phandle(child, xref);<br>
> + if (ref != -1)<br>
> + return ref;<br>
> +<br>
> + if (rtems_ofw_get_enc_prop(child, "phandle", &ref, sizeof(ref)) == -1 &&<br>
> + rtems_ofw_get_enc_prop(child, "ibm,phandle", &ref, sizeof(ref)) == -1 &&<br>
> + rtems_ofw_get_enc_prop(child, "linux,phandle", &ref, sizeof(ref)) == -1<br>
> + ) {<br>
> + continue;<br>
> + }<br>
> +<br>
> + if (ref == xref)<br>
> + return child;<br>
> + }<br>
> +<br>
> + return -1;<br>
> +}<br>
> +<br>
> +phandle_t rtems_ofw_node_from_xref( phandle_t xref )<br>
> +{<br>
> + phandle_t node;<br>
> +<br>
> + if ((node = rtems_ofw_get_effective_phandle(rtems_ofw_peer(0), xref)) == -1)<br>
> + return xref;<br>
> +<br>
> + return node;<br>
> +}<br>
> +<br>
> +phandle_t rtems_ofw_xref_from_node( phandle_t node )<br>
> +{<br>
> + phandle_t ref;<br>
> +<br>
> + if (rtems_ofw_get_enc_prop(node, "phandle", &ref, sizeof(ref)) == -1 &&<br>
> + rtems_ofw_get_enc_prop(node, "ibm,phandle", &ref, sizeof(ref)) == -1 &&<br>
> + rtems_ofw_get_enc_prop(node, "linux,phandle", &ref, sizeof(ref)) == -1)<br>
> + {<br>
> + return node;<br>
> + }<br>
> +<br>
> + return ref;<br>
> +}<br>
> +<br>
> +phandle_t rtems_ofw_instance_to_package( ihandle_t instance )<br>
> +{<br>
> + return rtems_ofw_node_from_xref(instance);<br>
> +}<br>
> +<br>
> +size_t rtems_ofw_package_to_path(<br>
> + phandle_t node,<br>
> + char *buf,<br>
> + size_t len<br>
> +)<br>
> +{<br>
> + int offset;<br>
> + int rv;<br>
> +<br>
> + offset = rtems_fdt_phandle_to_offset(node);<br>
> +<br>
> + rv = fdt_get_path(fdtp, offset, buf, len);<br>
> + if (rv != 0)<br>
> + return -1;<br>
> +<br>
> + return rv;<br>
> +}<br>
> +<br>
> +size_t rtems_ofw_instance_to_path(<br>
> + ihandle_t instance,<br>
> + char *buf,<br>
> + size_t len<br>
> +)<br>
> +{<br>
> + int offset;<br>
> + int rv;<br>
> +<br>
> + offset = rtems_ofw_instance_to_package(instance);<br>
> + offset = rtems_fdt_phandle_to_offset(offset);<br>
> +<br>
> + rv = fdt_get_path(fdtp, offset, buf, len);<br>
> + if (rv != 0)<br>
> + return -1;<br>
> +<br>
> + return rv;<br>
> +}<br>
> +<br>
> +int rtems_ofw_get_reg(<br>
> + phandle_t node,<br>
> + rtems_ofw_memory_area *buf,<br>
> + size_t size<br>
> +)<br>
> +{<br>
> + int len;<br>
> + int offset;<br>
> + int nranges;<br>
> + int nregs;<br>
> + phandle_t parent;<br>
> + rtems_ofw_ranges range;<br>
> + const rtems_ofw_ranges *ptr;<br>
> +<br>
> + len = rtems_ofw_get_enc_prop(node, "reg", (pcell_t *)buf, size);<br>
> + if (len <= 0) {<br>
> + return len;<br>
> + }<br>
> +<br>
> + nregs = MIN(len, size) / sizeof(rtems_ofw_memory_area);<br>
> +<br>
> + for (parent = rtems_ofw_parent(node); parent > 0;<br>
> + parent = rtems_ofw_parent(parent)) {<br>
> +<br>
> + offset = rtems_fdt_phandle_to_offset(parent);<br>
> + ptr = fdt_getprop(fdtp, offset, "ranges", &len);<br>
> +<br>
> + if (len < 0) {<br>
> + break;<br>
> + }<br>
> +<br>
> + nranges = len / sizeof(rtems_ofw_ranges);<br>
> +<br>
> + offset = 0;<br>
> + for (int i=0; i < nregs; i++) {<br>
> + for (int j=0; j < nranges; j++) {<br>
> + <br>
> + range.parent_bus = fdt32_to_cpu(ptr[j].parent_bus);<br>
> + range.child_bus = fdt32_to_cpu(ptr[j].child_bus);<br>
> + range.size = fdt32_to_cpu(ptr[j].size);<br>
> +<br>
> + if (buf[i].start >= range.child_bus &&<br>
> + buf[i].start < range.child_bus + range.size) {<br>
> + offset = range.parent_bus - range.child_bus;<br>
> + break;<br>
> + }<br>
> +<br>
> + }<br>
> + buf[i].start += offset;<br>
> + }<br>
> + }<br>
> +<br>
> + return nregs;<br>
> +}<br>
> +<br>
> +int rtems_ofw_get_interrupts(<br>
> + phandle_t node,<br>
> + void *buf,<br>
> + size_t size<br>
> +)<br>
> +{<br>
> + int rv;<br>
> +<br>
> + rv = rtems_ofw_get_enc_prop(node, "interrupts", buf, size);<br>
> + return rv;<br>
> +}<br>
> +<br>
> +bool rtems_ofw_node_status( phandle_t node )<br>
> +{<br>
> + int len;<br>
> + const char buf[10];<br>
> +<br>
> + len = rtems_ofw_get_prop(node, "status", &buf[0], sizeof(buf));<br>
> + if ((len == -1) ||<br>
> + (strncmp(buf, "okay", MIN(5, len)) == 0) ||<br>
> + (strncmp(buf, "ok", MIN(3, len)) == 0)) {<br>
> + return true;<br>
> + }<br>
> +<br>
> + return false;<br>
> +}<br>
> diff --git a/spec/build/bsps/obj.yml b/spec/build/bsps/obj.yml<br>
> index 8809238057..141ba25f5e 100644<br>
> --- a/spec/build/bsps/obj.yml<br>
> +++ b/spec/build/bsps/obj.yml<br>
> @@ -24,6 +24,9 @@ install:<br>
> - bsps/include/bsp/u-boot.h<br>
> - bsps/include/bsp/uart-output-char.h<br>
> - bsps/include/bsp/utility.h<br>
> +- destination: ${BSP_INCLUDEDIR}/ofw<br>
> + source:<br>
> + - bsps/include/ofw/ofw.h<br>
> - destination: ${BSP_INCLUDEDIR}/libchip<br>
> source:<br>
> - bsps/include/libchip/am29lv160.h<br>
> @@ -104,4 +107,5 @@ source:<br>
> - bsps/shared/dev/serial/z85c30_reg.c<br>
> - bsps/shared/start/bootcard.c<br>
> - bsps/shared/rtems-version.c<br>
> +- bsps/shared/ofw/ofw.c<br>
> type: build<br>
> <br>
</blockquote></div></div>