// RPMsg dynamic endpoints binding

digraph G {
  rankdir="LR";

  subgraph roles {
    node [style="filled", fillcolor="lightblue"];
    master [label="Master"];
    slave [label="Slave"];
  }

  subgraph m_comment_nodes {
    node [group=m_comment, shape="note", style="filled", fillcolor="yellow"];
    rank="same";
    m_remoteproc_init_comment [label="this is initialize rproc call"];
    m_remoteproc_boot_comment [label="it will setup vdev before booting the remote"];
    m_rpmsg_vdev_init_comment [label="\l* It will initialize vrings with the shared memory\l* As vdev doesn't support name service, it will not create name service endpoint;\l* it sets vdev status to DRVIER_READY, And will notify remote.\l"];
    m_rpmsg_create_ep_comment [label="\lIf vdev supports name service,\lit will send out name service.\l"];
    m_rpmsg_send_comment [label="\lIf endpoint hasn't binded, it fail\lreturn failure to indicate ep hasn't been binded.\l"];

  }

  subgraph m_flow_nodes {
    node [shape="box"];
    rank="same";
    m_remoteproc_init [label="rproc = remoteproc_init(&remoteproc_ops, &arg);"]
    m_remoteproc_load [label="calls remoteproc_load() to load applicaiton"];
    m_remoteproc_boot [shape="box", label="ret=remoteproc_boot(&rproc)"];
    m_remoteproc_get_vdev [label="vdev=remoteproc_create_virtio(rproc, rpmsg_vdev_id, MASTER, NULL);"];
    m_rpmsg_shmpool_init[label="rpmsg_virtio_init_shm_pool(shpool, shbuf, shbuf_pool_size);"];
    m_rpmsg_vdev_init [label="rpdev=rpmsg_init_vdev(rpvdev, vdev, ns_bind_cb, &shm_io, shpool);"];
    m_rpmsg_ns_cb [label="\lrpmsg_ns_callback() will see if there is a local ep registered.\lIf yes, bind the ep; otherwise, call ns_bind_cb.\l"];
    m_rpmsg_create_ep [label="\lept=rpmsg_create_ept(ept, rdev, ept_name, ept_addr, dest_addr, \lendpoint_cb, ns_unbind_cb);\l"];
    m_rpmsg_send [label="rpmsg_send(ept,data)"];
    m_rpmsg_rx_cb [label="rpmsg_rx_callback()"];
    m_ep_cb [label="endpoint_cb(ept, data, size, src_addr)"];
    m_rpmsg_destroy_ep [label="rpmsg_destroy_endpoint(ept)"];

    m_remoteproc_init -> m_remoteproc_load -> m_remoteproc_boot -> m_remoteproc_get_vdev ->
    m_rpmsg_shmpool_init -> m_rpmsg_vdev_init -> m_rpmsg_ns_cb -> m_rpmsg_create_ep -> m_rpmsg_send;
    m_rpmsg_send -> m_rpmsg_rx_cb -> m_ep_cb ->
    m_rpmsg_destroy_ep [dir="none", style="dashed"];
  }

  subgraph s_flow_nodes {
    rank="same";
    node [shape="box"];
    s_remoteproc_init [label="rproc = remoteproc_init(&remoteproc_ops, &arg);"];

    s_remoteproc_parse_rsc [label="ret = remoteproc_set_rsc_table(rproc, &rsc_table, rsc_size)"];
    s_remoteproc_get_vdev [label="vdev=remoteproc_create_virtio(rproc, rpmsg_vdev_id, SLAVE, rst_cb);"];
    s_rpmsg_vdev_init [label="rpdev=rpmsg_init_vdev(rpvdev, vdev, ns_bind_cb, &shm_io, NULL);"];
    s_rpmsg_create_ep [label="\lept=rpmsg_create_ept(ept, rdev, ept_name, ept_addr, dest_addr, \lendpoint_cb, ns_unbind_cb);\l"];
    s_rpmsg_ns_cb [label="\lrpmsg_ns_callback() will see if there is a local ep registered.\lIf yes, bind the ep; otherwise, call ns_binc_cb.\l"];
    s_rpmsg_send [label="rpmsg_send(ept,data)"];
    s_rpmsg_rx_cb [label="rpmsg_rx_callback()"];
    s_ep_cb [label="endpoint_cb(ept, data, size, src_addr)"];
    s_rpmsg_ns_unbind_cb [label="\lrpmsg_ns_callback() will call the previous\lregistered endpoint unbind callback\l"];

    s_remoteproc_init -> s_remoteproc_parse_rsc -> s_remoteproc_get_vdev ->
    s_rpmsg_vdev_init -> s_rpmsg_create_ep;
    s_rpmsg_create_ep -> s_rpmsg_ns_cb -> s_rpmsg_rx_cb ->
    s_ep_cb -> s_rpmsg_send -> s_rpmsg_ns_unbind_cb [dir="none", style="dash"];

  }

  subgraph s_comment_nodes {
    node [group=s_comment, shape="note", style="filled", fillcolor="yellow"];
    rank="same";
    s_rpmsg_vdev_init_comment [label="\l* If vdev supports name service, it will create name service endpoint;\l* It will not return until the master set status to DRIVER READY\l"];
    s_rpmsg_rx_cb_comment [label="\l* It will look for the endpoint which matches the destination address.\lIf the two endpoints hasn't binded yet,\lit will set the local endpoint's destination address with the source address in the message\l"];
  }

  master -> m_remoteproc_init [dir="none"];
  slave -> s_remoteproc_init [dir="none"];
  s_rpmsg_create_ep -> m_rpmsg_ns_cb [label="NS annoucement"];
  m_rpmsg_create_ep -> s_rpmsg_ns_cb [label="NS annoucement"];
  m_rpmsg_send -> s_rpmsg_rx_cb [label="RPMsg data"];
  s_rpmsg_send -> m_rpmsg_rx_cb [label="RPMsg data"];
  m_rpmsg_destroy_ep -> s_rpmsg_ns_unbind_cb [label="Endpoint destroy NS"];

  m_remoteproc_init_comment -> m_remoteproc_init [dir="none"];
  m_remoteproc_boot_comment -> m_remoteproc_boot [dir="none"];
  m_rpmsg_vdev_init_comment -> m_rpmsg_vdev_init [dir="none"];
  m_rpmsg_create_ep_comment -> m_rpmsg_create_ep [dir="none"];
  m_rpmsg_send_comment -> m_rpmsg_send [dir="none"];

  s_rpmsg_vdev_init -> s_rpmsg_vdev_init_comment [dir="none"];
  s_rpmsg_rx_cb -> s_rpmsg_rx_cb_comment [dir="none"];

  {rank=same; master; m_remoteproc_init}
  {rank=same; slave; s_remoteproc_init}

}

