aboutsummaryrefslogtreecommitdiff
path: root/machine/configstring.c
blob: 4e72f045715a1633bea5c857948d2d8f23eab9b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "configstring.h"
#include "encoding.h"
#include "mtrap.h"
#include "atomic.h"
#include <stdio.h>

static void query_mem(const char* config_string)
{
  query_result res = query_config_string(config_string, "ram{0{addr");
  assert(res.start);
  uintptr_t base = get_uint(res);
  assert(base == DRAM_BASE);
  res = query_config_string(config_string, "ram{0{size");
  mem_size = get_uint(res);
}

static void query_rtc(const char* config_string)
{
  query_result res = query_config_string(config_string, "rtc{addr");
  assert(res.start);
  mtime = (void*)(uintptr_t)get_uint(res);
}

static void query_plic(const char* config_string)
{
  query_result res = query_config_string(config_string, "plic{priority");
  if (!res.start)
    return;
  plic_priorities = (uint32_t*)(uintptr_t)get_uint(res);

  res = query_config_string(config_string, "plic{ndevs");
  if (!res.start)
    return;
  plic_ndevs = get_uint(res);
}

static void query_hart_plic(const char* config_string, hls_t* hls, int core, int hart)
{
  char buf[48];
  snprintf(buf, sizeof buf, "core{%d{%d{plic{m{ie", core, hart);
  query_result res = query_config_string(config_string, buf);
  if (res.start)
    hls->plic_m_ie = (void*)(uintptr_t)get_uint(res);

  snprintf(buf, sizeof buf, "core{%d{%d{plic{m{thresh", core, hart);
  res = query_config_string(config_string, buf);
  if (res.start)
    hls->plic_m_thresh = (void*)(uintptr_t)get_uint(res);

  snprintf(buf, sizeof buf, "core{%d{%d{plic{s{ie", core, hart);
  res = query_config_string(config_string, buf);
  if (res.start)
    hls->plic_s_ie = (void*)(uintptr_t)get_uint(res);

  snprintf(buf, sizeof buf, "core{%d{%d{plic{s{thresh", core, hart);
  res = query_config_string(config_string, buf);
  if (res.start)
    hls->plic_s_thresh = (void*)(uintptr_t)get_uint(res);
}

static void query_harts(const char* config_string)
{
  for (int core = 0, hart; ; core++) {
    for (hart = 0; ; hart++) {
      char buf[40];
      snprintf(buf, sizeof buf, "core{%d{%d{ipi", core, hart);
      query_result res = query_config_string(config_string, buf);
      if (!res.start)
        break;
      hls_t* hls = hls_init(num_harts);
      hls->ipi = (void*)(uintptr_t)get_uint(res);

      query_hart_plic(config_string, hls, core, hart);

      snprintf(buf, sizeof buf, "core{%d{%d{timecmp", core, hart);
      res = query_config_string(config_string, buf);
      assert(res.start);
      hls->timecmp = (void*)(uintptr_t)get_uint(res);

      mb();

      // wake up the hart
      *hls->ipi = 1;

      num_harts++;
    }
    if (!hart)
      break;
  }
  assert(num_harts);
  assert(num_harts <= MAX_HARTS);
}

void parse_config_string()
{
  uint32_t addr = *(uint32_t*)CONFIG_STRING_ADDR;
  const char* s = (const char*)(uintptr_t)addr;
  query_mem(s);
  query_plic(s);
  query_rtc(s);
  query_harts(s);
}