Commit d2443e15 authored by Jens Korinth's avatar Jens Korinth
Browse files

Bugfixes in libplatform

* minor memory leaks fixed
* destroy sequence fixed
* using DEV logging macros
parent b8f7b8f0
......@@ -31,6 +31,7 @@
#include <assert.h>
struct platform_addr_map {
platform_dev_id_t dev_id;
platform_info_t const *info;
};
......@@ -40,19 +41,20 @@ platform_res_t platform_addr_map_init(platform_devctx_t *ctx,
{
*am = (platform_addr_map_t *)malloc(sizeof(**am));
if (! *am) {
ERR("could not allocate memory for platform_addr_map_t");
DEVERR(ctx->dev_id, "could not allocate memory for platform_addr_map_t");
return PERR_OUT_OF_MEMORY;
}
(*am)->info = info;
(*am)->info = info;
(*am)->dev_id = ctx->dev_id;
LOG(LPLL_ADDR, "address map successfully initialized");
DEVLOG(ctx->dev_id, LPLL_ADDR, "address map successfully initialized");
return PLATFORM_SUCCESS;
}
void platform_addr_map_deinit(platform_devctx_t *ctx, platform_addr_map_t *am)
{
if (am) free(am);
LOG(LPLL_ADDR, "destroyed");
DEVLOG(ctx->dev_id, LPLL_ADDR, "destroyed");
}
platform_res_t platform_addr_map_get_slot_base(platform_addr_map_t const* am,
......@@ -62,7 +64,7 @@ platform_res_t platform_addr_map_get_slot_base(platform_addr_map_t const* am,
#ifndef NDEBUG
assert(am || "addr struct must not be NULL");
if (slot_id < 0 || slot_id >= PLATFORM_NUM_SLOTS) {
ERR("invalid slot_id %d: must be >= 0 and <= %d",
DEVERR(am->dev_id, "invalid slot_id %d: must be >= 0 and <= %d",
slot_id, PLATFORM_NUM_SLOTS);
return 0;
}
......@@ -90,12 +92,12 @@ platform_res_t platform_addr_map_get_component_base(
}
#ifndef NDEBUG
if (comp_id < 0 || comp_id >= PLATFORM_NUM_SLOTS) {
ERR("invalid comp_id %d: must be >= 0 and <= %d",
DEVERR(am->dev_id, "invalid comp_id %d: must be >= 0 and <= %d",
comp_id, PLATFORM_NUM_SLOTS);
return PERR_ADDR_INVALID_COMP_ID;
}
if (am->info->base.platform[comp_id] == 0) {
ERR("no base defined for component #%lu", (unsigned long)comp_id);
DEVERR(am->dev_id, "no base defined for component #%lu", (unsigned long)comp_id);
return PERR_COMPONENT_NOT_FOUND;
}
#endif
......
......@@ -198,7 +198,7 @@ platform_res_t platform_create_device(platform_ctx_t *ctx,
LOG(LPLL_TLKM, "created device #%03u, initializing device context ...");
if ((res = platform_devctx_init(ctx, dev_id, mode, &ctx->devctx[dev_id])) != PLATFORM_SUCCESS) {
ERR("could not initialize device context for #%03u: %s (%d)",
dev_id, platform_strerror(r), r);
dev_id, platform_strerror(res), res);
goto err_pdev;
}
if (pdctx) *pdctx = ctx->devctx[dev_id];
......
......@@ -28,10 +28,11 @@ platform_res_t platform_devctx_init(platform_ctx_t *ctx,
platform_devctx_t *devctx = (platform_devctx_t *)calloc(sizeof(*devctx), 1);
if (! devctx) {
DEVERR(dev_id, "could not allocate memory for device context");
free(fn);
return PERR_OUT_OF_MEMORY;
}
LOG(LPLL_DEVICE, "preparing device #%03u ...", dev_id);
DEVLOG(dev_id, LPLL_DEVICE, "preparing device ...");
devctx->dev_id = dev_id;
devctx->mode = mode;
default_dops(&devctx->dops);
......@@ -39,13 +40,16 @@ platform_res_t platform_devctx_init(platform_ctx_t *ctx,
if ((res = platform_device_info(ctx, dev_id, &devctx->dev_info)) != PLATFORM_SUCCESS) {
DEVERR(dev_id, "could not get device information: %s (%d)", platform_strerror(res), res);
free (devctx);
free(fn);
return res;
}
devctx->fd_ctrl = open(control_file(dev_id), O_RDWR);
devctx->fd_ctrl = open(fn, O_RDWR);
if (devctx->fd_ctrl == -1) {
DEVERR(dev_id, "could not open %s: %s (%d)", fn, strerror(errno), errno);
free(fn);
res = PERR_OPEN_DEV;
return res;
}
free(fn);
......@@ -59,7 +63,7 @@ platform_res_t platform_devctx_init(platform_ctx_t *ctx,
DEVERR(dev_id, "could not initialize platform address map: %s (%d)", platform_strerror(res), res);
goto err_addr_map;
}
DEVLOG(dev_id, LPLL_INIT, "device #%03u: initialized device address map");
DEVLOG(dev_id, LPLL_INIT, "initialized device address map");
res = platform_signaling_init(devctx, &devctx->signaling);
if (res != PLATFORM_SUCCESS) {
......@@ -69,7 +73,7 @@ platform_res_t platform_devctx_init(platform_ctx_t *ctx,
DEVLOG(dev_id, LPLL_INIT, "initialized device signaling");
if ((res = zynq_init(devctx)) != PLATFORM_SUCCESS) {
DEVERR(dev_id, "found no matching platform definition for device #%03u");
DEVERR(dev_id, "found no matching platform definition");
goto err_platform;
}
......@@ -90,6 +94,7 @@ err_info:
void platform_devctx_deinit(platform_devctx_t *devctx)
{
if (devctx) {
zynq_exit(devctx);
platform_dev_id_t dev_id = devctx->dev_id;
DEVLOG(dev_id, LPLL_INIT, "destroying platform signaling ...");
platform_signaling_deinit(devctx->signaling);
......
......@@ -46,15 +46,15 @@ platform_res_t default_read_ctl(platform_devctx_t const *devctx,
platform_ctl_flags_t const flags)
{
long ret = 0;
DEVLOG(devctx->dev_id, LPLL_TLKM, "reading %zu bytes from 0x%08llx with flags 0x%08llx",
length, (u64)addr, (u64)flags);
DEVLOG(devctx->dev_id, LPLL_TLKM, "reading %zu bytes from 0x%08llx to 0x%08lx with flags 0x%08llx",
length, (u64)addr, (ulong)data, (u64)flags);
struct tlkm_copy_cmd cmd = {
.length = length,
.user_addr = data,
.dev_addr = addr,
};
if ((ret = ioctl(devctx->fd_ctrl, TLKM_DEV_IOCTL_READ, &cmd))) {
DEVERR(devctx->dev_id, "error writing to 0x%08llx: %s (%d)",
DEVERR(devctx->dev_id, "error reading from 0x%08llx: %s (%d)",
(u64)addr, strerror(errno), errno);
return PERR_TLKM_ERROR;
}
......@@ -68,8 +68,8 @@ platform_res_t default_write_ctl(platform_devctx_t const *devctx,
platform_ctl_flags_t const flags)
{
long ret = 0;
DEVLOG(devctx->dev_id, LPLL_TLKM, "writing %zu bytes to 0x%08llx with flags 0x%08llx",
length, (u64)addr, (u64)flags);
DEVLOG(devctx->dev_id, LPLL_TLKM, "writing %zu bytes from 0x%08lx to 0x%08llx with flags 0x%08llx",
length, (ulong)data, (u64)addr, (u64)flags);
struct tlkm_copy_cmd cmd = {
.length = length,
.user_addr = (void *)data,
......
......@@ -12,9 +12,10 @@
#include <fcntl.h>
struct platform_signaling {
int fd_wait;
pthread_t collector;
sem_t finished[PLATFORM_NUM_SLOTS];
int fd_wait;
platform_dev_id_t dev_id;
pthread_t collector;
sem_t finished[PLATFORM_NUM_SLOTS];
};
static
......@@ -26,9 +27,9 @@ void *platform_signaling_read_waitfile(void *p)
assert(a->fd_wait);
do {
if (read(a->fd_wait, &s, sizeof(s)) == sizeof(s)) {
LOG(LPLL_ASYNC, "received finish notification from slot #%u", (unsigned)s);
DEVLOG(a->dev_id, LPLL_ASYNC, "received finish notification from slot #%u", (unsigned)s);
if (s < PLATFORM_NUM_SLOTS) sem_post(&a->finished[s]);
else ERR("invalid slot id received: %u", (unsigned)s);
else DEVERR(a->dev_id, "invalid slot id received: %u", (unsigned)s);
}
} while (1);
return NULL;
......@@ -38,7 +39,7 @@ platform_res_t platform_signaling_init(platform_devctx_t const *pctx, platform_s
{
*a = (platform_signaling_t *)malloc(sizeof(**a));
if (! a) {
ERR("could not allocate platform_signaling");
DEVERR(pctx->dev_id, "could not allocate platform_signaling");
return PERR_OUT_OF_MEMORY;
}
......@@ -47,22 +48,24 @@ platform_res_t platform_signaling_init(platform_devctx_t const *pctx, platform_s
}
(*a)->fd_wait = pctx->fd_ctrl;
(*a)->dev_id = pctx->dev_id;
assert((*a)->fd_wait != -1);
LOG(LPLL_ASYNC, "starting collector thread");
DEVLOG(pctx->dev_id, LPLL_ASYNC, "starting collector thread");
int x = pthread_create(&(*a)->collector, NULL, platform_signaling_read_waitfile, *a);
if (x != 0) {
ERR("could not start collector thread: %s (%d)", strerror(errno), errno);
DEVERR(pctx->dev_id, "could not start collector thread: %s (%d)", strerror(errno), errno);
free(*a);
return PERR_PTHREAD_ERROR;
}
LOG(LPLL_ASYNC, "async initialized successfully");
DEVLOG(pctx->dev_id, LPLL_ASYNC, "async initialized successfully");
return PLATFORM_SUCCESS;
}
void platform_signaling_deinit(platform_signaling_t *a)
{
platform_dev_id_t const dev_id = a->dev_id;
pthread_cancel(a->collector);
pthread_join(a->collector, NULL);
......@@ -72,14 +75,14 @@ void platform_signaling_deinit(platform_signaling_t *a)
sem_close(&a->finished[s]);
}
if (a) free(a);
LOG(LPLL_ASYNC, "async deinitialized");
DEVLOG(dev_id, LPLL_ASYNC, "async deinitialized");
}
platform_res_t platform_signaling_wait_for_slot(platform_signaling_t *a, platform_slot_id_t const slot)
{
LOG(LPLL_ASYNC, "waiting for slot #%lu", (unsigned long)slot);
DEVLOG(a->dev_id, LPLL_ASYNC, "waiting for slot #%lu", (unsigned long)slot);
sem_wait(&a->finished[slot]);
LOG(LPLL_ASYNC, "slot #%lu has finished", (unsigned long)slot);
DEVLOG(a->dev_id, LPLL_ASYNC, "slot #%lu has finished", (unsigned long)slot);
return PLATFORM_SUCCESS;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment