aboutsummaryrefslogtreecommitdiff
path: root/src/components/bus/simple_bus.c
blob: 218e9306e790a3479179201a44cdcce9cc7fa69a (plain) (blame)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/* SPDX-License-Identifier: copyleft-next-0.3.1 */
/* Copyright 2023 Kim Kuparinen < kimi.h.kuparinen@gmail.com > */

#include <threads.h>
#include <inttypes.h>
#include <stdlib.h>

#include <gran/common.h>
#include <gran/component.h>

#include <gran/bus/simple_bus.h>

struct mem_region {
	uintptr_t addr;
	size_t size;
	struct component *component;
	struct mem_region *next;
};

struct simple_bus {
	struct component component;
	mtx_t lock;

	struct mem_region *mem_regions;
};

static struct mem_region *find_mem_region(struct simple_bus *bus,
                                          uintptr_t addr)
{
	if (!bus->mem_regions)
		return NULL;

	struct mem_region *cur = bus->mem_regions;
	while (cur) {
		/* address is within memory region */
		if (addr >= cur->addr && addr < cur->addr + cur->size)
			return cur;

		cur = cur->next;
	}

	return NULL;
}

static stat add_mem_region(struct simple_bus *bus, struct mem_region *new)
{
	if (!bus->mem_regions) {
		bus->mem_regions = new;
		return OK;
	}

	struct mem_region *found = NULL;
	if ((found = find_mem_region(bus, new->addr))) {
		error("%s overlaps with %s at %" PRIuPTR,
		      new->component->name,
		      found->component->name,
		      new->addr
		      );
		return EEXISTS;
	}

	new->next = bus->mem_regions;
	bus->mem_regions = new;
	return OK;
}

static stat simple_bus_write(struct simple_bus *bus, struct packet *pkt)
{
	/* only one device can drive the bus at one time */
	if (mtx_trylock(&bus->lock) != thrd_success)
		return EBUSY;

	struct mem_region *mem_region = find_mem_region(bus, packet_addr(pkt));
	if (!mem_region) {
		warn("nothing to write on bus %s at %" PRIuPTR,
		     bus->component.name, packet_addr(pkt));
		return EBUS;
	}

	stat ret = write(mem_region->component, pkt);

	mtx_unlock(&bus->lock);
	return ret;
}

static stat simple_bus_read(struct simple_bus *bus, struct packet *pkt)
{
	/* only one device can drive the bus at one time */
	if (mtx_trylock(&bus->lock) != thrd_success)
		return EBUSY;

	struct mem_region *mem_region = find_mem_region(bus, packet_addr(pkt));
	if (!mem_region) {
		warn("nothing to read on bus %s at %" PRIuPTR,
		     bus->component.name, packet_addr(pkt));
		return EBUS;
	}

	stat ret = read(mem_region->component, pkt);

	mtx_unlock(&bus->lock);
	return ret;
}

static stat simple_bus_swap(struct simple_bus *bus, struct packet *pkt)
{
	if (mtx_trylock(&bus->lock) != thrd_success)
		return EBUSY;

	struct mem_region *mem_region = find_mem_region(bus, packet_addr(pkt));
	if (!mem_region) {
		warn("nothing to swap on bus %s at %" PRIuPTR,
		     bus->component.name, packet_addr(pkt));
		return EBUS;
	}

	stat ret = swap(mem_region->component, pkt);

	mtx_unlock(&bus->lock);
	return ret;
}

static stat simple_bus_snoop(struct simple_bus *bus, struct snoop *snoop)
{
	struct mem_region *cur = bus->mem_regions;
	while (cur) {
		if (cur->component->snoop) {
			stat ret = cur->component->snoop(cur->component, snoop);
			if (ret)
				return ret;

			if (snoop_state(snoop) == SNOOP_ANSWERED)
				return OK;
		}

		cur = cur->next;
	}

	return OK;
}

static void simple_bus_destroy(struct simple_bus *bus)
{
	struct mem_region *cur = bus->mem_regions, *next = NULL;
	if (cur)
		do {
			next = cur->next;
			destroy(cur->component);
			free(cur);
		} while ((cur = next));

	free(bus);
}

struct component *create_simple_bus()
{
	struct simple_bus *bus = calloc(1, sizeof(struct simple_bus));
	if (!bus)
		return NULL;

	bus->component.write = (write_callback)simple_bus_write;
	bus->component.read = (read_callback)simple_bus_read;
	bus->component.swap = (swap_callback)simple_bus_swap;
	bus->component.snoop = (snoop_callback)simple_bus_snoop;
	// actually, still not sure about what API I want to use for controls
	// bus->component.ctrl = (ctrl_callback)simple_bus_ctrl;

	bus->component.destroy = (destroy_callback)simple_bus_destroy;

	mtx_init(&bus->lock, mtx_plain);
	return (struct component *)bus;
}

stat simple_bus_add(struct component *bus, struct component *component,
                    uint64_t addr, uint64_t size)
{
	struct mem_region *new = calloc(1, sizeof(struct mem_region));
	if (!new)
		return ENOMEM;

	new->addr = addr;
	new->size = size;
	new->component = component;

	if (add_mem_region((struct simple_bus *)bus, new)) {
		free(new);
		return EEXISTS;
	}

	return OK;
}