aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/host/dwc_common_port/dwc_mem.c
blob: ad645ff1ba7e06c852440396767e69279b3b31db (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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/* Memory Debugging */
#ifdef DWC_DEBUG_MEMORY

#include "dwc_os.h"
#include "dwc_list.h"

struct allocation {
	void *addr;
	void *ctx;
	char *func;
	int line;
	uint32_t size;
	int dma;
	DWC_CIRCLEQ_ENTRY(allocation) entry;
};

DWC_CIRCLEQ_HEAD(allocation_queue, allocation);

struct allocation_manager {
	void *mem_ctx;
	struct allocation_queue allocations;

	/* statistics */
	int num;
	int num_freed;
	int num_active;
	uint32_t total;
	uint32_t cur;
	uint32_t max;
};

static struct allocation_manager *manager = NULL;

static int add_allocation(void *ctx, uint32_t size, char const *func, int line, void *addr,
			  int dma)
{
	struct allocation *a;

	DWC_ASSERT(manager != NULL, "manager not allocated");

	a = __DWC_ALLOC_ATOMIC(manager->mem_ctx, sizeof(*a));
	if (!a) {
		return -DWC_E_NO_MEMORY;
	}

	a->func = __DWC_ALLOC_ATOMIC(manager->mem_ctx, DWC_STRLEN(func) + 1);
	if (!a->func) {
		__DWC_FREE(manager->mem_ctx, a);
		return -DWC_E_NO_MEMORY;
	}

	DWC_MEMCPY(a->func, func, DWC_STRLEN(func) + 1);
	a->addr = addr;
	a->ctx = ctx;
	a->line = line;
	a->size = size;
	a->dma = dma;
	DWC_CIRCLEQ_INSERT_TAIL(&manager->allocations, a, entry);

	/* Update stats */
	manager->num++;
	manager->num_active++;
	manager->total += size;
	manager->cur += size;

	if (manager->max < manager->cur) {
		manager->max = manager->cur;
	}

	return 0;
}

static struct allocation *find_allocation(void *ctx, void *addr)
{
	struct allocation *a;

	DWC_CIRCLEQ_FOREACH(a, &manager->allocations, entry) {
		if (a->ctx == ctx && a->addr == addr) {
			return a;
		}
	}

	return NULL;
}

static void free_allocation(void *ctx, void *addr, char const *func, int line)
{
	struct allocation *a = find_allocation(ctx, addr);

	if (!a) {
		DWC_ASSERT(0,
			   "Free of address %p that was never allocated or already freed %s:%d",
			   addr, func, line);
		return;
	}

	DWC_CIRCLEQ_REMOVE(&manager->allocations, a, entry);

	manager->num_active--;
	manager->num_freed++;
	manager->cur -= a->size;
	__DWC_FREE(manager->mem_ctx, a->func);
	__DWC_FREE(manager->mem_ctx, a);
}

int dwc_memory_debug_start(void *mem_ctx)
{
	DWC_ASSERT(manager == NULL, "Memory debugging has already started\n");

	if (manager) {
		return -DWC_E_BUSY;
	}

	manager = __DWC_ALLOC(mem_ctx, sizeof(*manager));
	if (!manager) {
		return -DWC_E_NO_MEMORY;
	}

	DWC_CIRCLEQ_INIT(&manager->allocations);
	manager->mem_ctx = mem_ctx;
	manager->num = 0;
	manager->num_freed = 0;
	manager->num_active = 0;
	manager->total = 0;
	manager->cur = 0;
	manager->max = 0;

	return 0;
}

void dwc_memory_debug_stop(void)
{
	struct allocation *a;

	dwc_memory_debug_report();

	DWC_CIRCLEQ_FOREACH(a, &manager->allocations, entry) {
		DWC_ERROR("Memory leaked from %s:%d\n", a->func, a->line);
		free_allocation(a->ctx, a->addr, NULL, -1);
	}

	__DWC_FREE(manager->mem_ctx, manager);
}

void dwc_memory_debug_report(void)
{
	struct allocation *a;

	DWC_PRINTF("\n\n\n----------------- Memory Debugging Report -----------------\n\n");
	DWC_PRINTF("Num Allocations = %d\n", manager->num);
	DWC_PRINTF("Freed = %d\n", manager->num_freed);
	DWC_PRINTF("Active = %d\n", manager->num_active);
	DWC_PRINTF("Current Memory Used = %d\n", manager->cur);
	DWC_PRINTF("Total Memory Used = %d\n", manager->total);
	DWC_PRINTF("Maximum Memory Used at Once = %d\n", manager->max);
	DWC_PRINTF("Unfreed allocations:\n");

	DWC_CIRCLEQ_FOREACH(a, &manager->allocations, entry) {
		DWC_PRINTF("    addr=%p, size=%d from %s:%d, DMA=%d\n",
			   a->addr, a->size, a->func, a->line, a->dma);
	}
}

/* The replacement functions */
void *dwc_alloc_debug(void *mem_ctx, uint32_t size, char const *func, int line)
{
	void *addr = __DWC_ALLOC(mem_ctx, size);

	if (!addr) {
		return NULL;
	}

	if (add_allocation(mem_ctx, size, func, line, addr, 0)) {
		__DWC_FREE(mem_ctx, addr);
		return NULL;
	}

	return addr;
}

void *dwc_alloc_atomic_debug(void *mem_ctx, uint32_t size, char const *func,
			     int line)
{
	void *addr = __DWC_ALLOC_ATOMIC(mem_ctx, size);

	if (!addr) {
		return NULL;
	}

	if (add_allocation(mem_ctx, size, func, line, addr, 0)) {
		__DWC_FREE(mem_ctx, addr);
		return NULL;
	}

	return addr;
}

void dwc_free_debug(void *mem_ctx, void *addr, char const *func, int line)
{
	free_allocation(mem_ctx, addr, func, line);
	__DWC_FREE(mem_ctx, addr);
}

void *dwc_dma_alloc_debug(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr,
			  char const *func, int line)
{
	void *addr = __DWC_DMA_ALLOC(dma_ctx, size, dma_addr);

	if (!addr) {
		return NULL;
	}

	if (add_allocation(dma_ctx, size, func, line, addr, 1)) {
		__DWC_DMA_FREE(dma_ctx, size, addr, *dma_addr);
		return NULL;
	}

	return addr;
}

void *dwc_dma_alloc_atomic_debug(void *dma_ctx, uint32_t size,
				 dwc_dma_t *dma_addr, char const *func, int line)
{
	void *addr = __DWC_DMA_ALLOC_ATOMIC(dma_ctx, size, dma_addr);

	if (!addr) {
		return NULL;
	}

	if (add_allocation(dma_ctx, size, func, line, addr, 1)) {
		__DWC_DMA_FREE(dma_ctx, size, addr, *dma_addr);
		return NULL;
	}

	return addr;
}

void dwc_dma_free_debug(void *dma_ctx, uint32_t size, void *virt_addr,
			dwc_dma_t dma_addr, char const *func, int line)
{
	free_allocation(dma_ctx, virt_addr, func, line);
	__DWC_DMA_FREE(dma_ctx, size, virt_addr, dma_addr);
}

#endif /* DWC_DEBUG_MEMORY */