diff --git a/lib/system/linux/alloc.h b/lib/system/linux/alloc.h index dbad764..81ba21e 100644 --- a/lib/system/linux/alloc.h +++ b/lib/system/linux/alloc.h @@ -24,7 +24,7 @@ extern "C" { static inline void *metal_allocate_memory(unsigned int size) { - return (malloc(size)); + return malloc(size); } static inline void metal_free_memory(void *ptr) diff --git a/lib/system/linux/cache.h b/lib/system/linux/cache.h index 7aba95e..67586a1 100644 --- a/lib/system/linux/cache.h +++ b/lib/system/linux/cache.h @@ -29,7 +29,6 @@ static inline void __metal_cache_flush(void *addr, unsigned int len) */ metal_unused(addr); metal_unused(len); - return; } static inline void __metal_cache_invalidate(void *addr, unsigned int len) @@ -39,7 +38,6 @@ static inline void __metal_cache_invalidate(void *addr, unsigned int len) */ metal_unused(addr); metal_unused(len); - return; } #ifdef __cplusplus diff --git a/lib/system/linux/condition.h b/lib/system/linux/condition.h index ca368a6..e8f98a4 100644 --- a/lib/system/linux/condition.h +++ b/lib/system/linux/condition.h @@ -30,11 +30,11 @@ extern "C" { struct metal_condition { atomic_uintptr_t mptr; /**< mutex pointer. - The condition variable is attached to - this mutex when it is waiting. - It is also used to check correctness - in case there are multiple waiters. */ - + * The condition variable is attached to + * this mutex when it is waiting. + * It is also used to check correctness + * in case there are multiple waiters. + */ atomic_int waiters; /**< number of waiters. */ atomic_int wakeups; /**< number of wakeups. */ }; diff --git a/lib/system/linux/device.c b/lib/system/linux/device.c index b6e4ff0..244f97e 100644 --- a/lib/system/linux/device.c +++ b/lib/system/linux/device.c @@ -73,8 +73,10 @@ static struct linux_device *to_linux_device(struct metal_device *device) return metal_container_of(device, struct linux_device, device); } -static int metal_uio_read_map_attr(struct linux_device *ldev, unsigned index, - const char *name, unsigned long *value) +static int metal_uio_read_map_attr(struct linux_device *ldev, + unsigned int index, + const char *name, + unsigned long *value) { const char *cls = ldev->cls_path; struct sysfs_attribute *attr; @@ -147,7 +149,7 @@ static int metal_uio_dev_open(struct linux_bus *lbus, struct linux_device *ldev) { char *instance, path[SYSFS_PATH_MAX]; struct linux_driver *ldrv = ldev->ldrv; - unsigned long *phys, offset=0, size=0; + unsigned long *phys, offset = 0, size = 0; struct metal_io_region *io; struct dlist *dlist; int result, i; @@ -337,17 +339,16 @@ static int metal_uio_dev_dma_map(struct linux_bus *lbus, } static void metal_uio_dev_dma_unmap(struct linux_bus *lbus, - struct linux_device *ldev, - uint32_t dir, - struct metal_sg *sg, - int nents) + struct linux_device *ldev, + uint32_t dir, + struct metal_sg *sg, + int nents) { (void) lbus; (void) ldev; (void) dir; (void) sg; (void) nents; - return; } static struct linux_bus linux_bus[] = { @@ -512,10 +513,10 @@ static int metal_linux_dev_dma_map(struct metal_bus *bus, } static void metal_linux_dev_dma_unmap(struct metal_bus *bus, - struct metal_device *device, - uint32_t dir, - struct metal_sg *sg, - int nents) + struct metal_device *device, + uint32_t dir, + struct metal_sg *sg, + int nents) { struct linux_device *ldev = to_linux_device(device); struct linux_bus *lbus = to_linux_bus(bus); diff --git a/lib/system/linux/init.c b/lib/system/linux/init.c index 0dbfa76..f34b5ee 100644 --- a/lib/system/linux/init.c +++ b/lib/system/linux/init.c @@ -16,14 +16,15 @@ struct metal_state _metal; -extern int metal_linux_irq_init(); -extern void metal_linux_irq_shutdown(); +extern int metal_linux_irq_init(void); +extern void metal_linux_irq_shutdown(void); /** Sort function for page size array. */ static int metal_pagesize_compare(const void *_a, const void *_b) { const struct metal_page_size *a = _a, *b = _b; long diff = a->page_size - b->page_size; + return metal_sign(diff); } @@ -48,7 +49,7 @@ static int metal_add_page_size(const char *path, int shift, int mmap_flags) _metal.page_sizes[index].page_size = size; _metal.page_sizes[index].mmap_flags = mmap_flags; strncpy(_metal.page_sizes[index].path, path, PATH_MAX); - _metal.num_page_sizes ++; + _metal.num_page_sizes++; metal_log(METAL_LOG_DEBUG, "added page size %ld @%s\n", size, path); @@ -87,6 +88,7 @@ static int metal_init_page_sizes(void) count = gethugepagesizes(sizes, max_sizes); for (i = 0; i < count; i++) { int shift = metal_log2(sizes[i]); + if ((shift & MAP_HUGE_MASK) != shift) continue; metal_add_page_size( @@ -110,7 +112,7 @@ int metal_sys_init(const struct metal_init_params *params) static char sysfs_path[SYSFS_PATH_MAX]; const char *tmp_path; unsigned int seed; - FILE* urandom; + FILE *urandom; int result; /* Determine sysfs mount point. */ diff --git a/lib/system/linux/irq.c b/lib/system/linux/irq.c index 0151bc4..d687066 100644 --- a/lib/system/linux/irq.c +++ b/lib/system/linux/irq.c @@ -32,7 +32,8 @@ static struct metal_device *irqs_devs[MAX_IRQS]; /**< Linux devices for IRQs */ static int irq_notify_fd; /**< irq handling state change notification file - descriptor */ + * descriptor + */ static metal_mutex_t irq_lock; /**< irq handling lock */ static bool irq_handling_stop; /**< stop interrupts handling */ @@ -54,9 +55,9 @@ static METAL_IRQ_CONTROLLER_DECLARE(linux_irq_cntr, 0, MAX_IRQS, NULL, metal_linux_irq_set_enable, NULL, - irqs) + irqs); -unsigned int metal_irq_save_disable() +unsigned int metal_irq_save_disable(void) { /* This is to avoid deadlock if it is called in ISR */ if (pthread_self() == irq_pthread) @@ -65,14 +66,14 @@ unsigned int metal_irq_save_disable() return 0; } -void metal_irq_restore_enable(unsigned flags) +void metal_irq_restore_enable(unsigned int flags) { (void)flags; if (pthread_self() != irq_pthread) metal_mutex_release(&irq_lock); } -static int metal_linux_irq_notify() +static int metal_linux_irq_notify(void) { uint64_t val = 1; int ret; @@ -105,15 +106,16 @@ static void metal_linux_irq_set_enable(struct metal_irq_controller *irq_cntr, /* Notify IRQ thread that IRQ state has changed */ ret = metal_linux_irq_notify(); if (ret < 0) { - metal_log(METAL_LOG_ERROR, "%s: failed to notify set %d enable\n", + metal_log(METAL_LOG_ERROR, + "%s: failed to notify set %d enable\n", __func__, irq); } } /** - * @brief IRQ handler - * @param[in] args not used. required for pthread. - */ + * @brief IRQ handler + * @param[in] args not used. required for pthread. + */ static void *metal_linux_irq_handling(void *args) { struct sched_param param; @@ -122,12 +124,12 @@ static void *metal_linux_irq_handling(void *args) int i, j, pfds_total; struct pollfd *pfds; - (void) args; + (void)args; pfds = (struct pollfd *)malloc(FD_SETSIZE * sizeof(struct pollfd)); if (!pfds) { - metal_log(METAL_LOG_ERROR, "%s: failed to allocate irq fds mem.\n", - __func__); + metal_log(METAL_LOG_ERROR, + "%s: failed to allocate irq fds mem.\n", __func__); return NULL; } @@ -135,13 +137,14 @@ static void *metal_linux_irq_handling(void *args) /* Ignore the set scheduler error */ ret = sched_setscheduler(0, SCHED_FIFO, ¶m); if (ret) { - metal_log(METAL_LOG_WARNING, "%s: Failed to set scheduler: %s.\n", - __func__, strerror(ret)); + metal_log(METAL_LOG_WARNING, + "%s: Failed to set scheduler: %s.\n", __func__, + strerror(ret)); } while (1) { metal_mutex_acquire(&irq_lock); - if (irq_handling_stop == true) { + if (irq_handling_stop) { /* Killing this IRQ handling thread */ metal_mutex_release(&irq_lock); break; @@ -169,12 +172,13 @@ static void *metal_linux_irq_handling(void *args) /* Waken up from interrupt */ pfds_total = j; for (i = 0; i < pfds_total; i++) { - if ( (pfds[i].fd == irq_notify_fd) && - (pfds[i].revents & (POLLIN | POLLRDNORM))) { + if ((pfds[i].fd == irq_notify_fd) && + (pfds[i].revents & (POLLIN | POLLRDNORM))) { /* IRQ registration change notification */ - if (read(pfds[i].fd, (void*)&val, sizeof(uint64_t)) < 0) + if (read(pfds[i].fd, + (void *)&val, sizeof(uint64_t)) < 0) metal_log(METAL_LOG_ERROR, - "%s, read irq fd %d failed.\n", + "%s, read irq fd %d failed\n", __func__, pfds[i].fd); } else if ((pfds[i].revents & (POLLIN | POLLRDNORM))) { struct metal_device *dev = NULL; @@ -189,13 +193,15 @@ static void *metal_linux_irq_handling(void *args) irq_handled = 1; if (irq_handled) { if (dev && dev->bus->ops.dev_irq_ack) - dev->bus->ops.dev_irq_ack(dev->bus, dev, fd); + dev->bus->ops.dev_irq_ack( + dev->bus, dev, fd); } metal_mutex_release(&irq_lock); } else if (pfds[i].revents) { metal_log(METAL_LOG_DEBUG, "%s: poll unexpected. fd %d: %d\n", - __func__, pfds[i].fd, pfds[i].revents); + __func__, + pfds[i].fd, pfds[i].revents); } } } @@ -204,18 +210,19 @@ static void *metal_linux_irq_handling(void *args) } /** - * @brief irq handling initialization - * @return 0 on sucess, non-zero on failure - */ -int metal_linux_irq_init() + * @brief irq handling initialization + * @return 0 on success, non-zero on failure + */ +int metal_linux_irq_init(void) { int ret; memset(&irqs, 0, sizeof(irqs)); - irq_notify_fd = eventfd(0,0); + irq_notify_fd = eventfd(0, 0); if (irq_notify_fd < 0) { - metal_log(METAL_LOG_ERROR, "Failed to create eventfd for IRQ handling.\n"); + metal_log(METAL_LOG_ERROR, + "Failed to create eventfd for IRQ handling.\n"); return -EAGAIN; } @@ -228,9 +235,10 @@ int metal_linux_irq_init() return -EINVAL; } ret = pthread_create(&irq_pthread, NULL, - metal_linux_irq_handling, NULL); + metal_linux_irq_handling, NULL); if (ret != 0) { - metal_log(METAL_LOG_ERROR, "Failed to create IRQ thread: %d.\n", ret); + metal_log(METAL_LOG_ERROR, "Failed to create IRQ thread: %d.\n", + ret); return -EAGAIN; } @@ -238,9 +246,9 @@ int metal_linux_irq_init() } /** - * @brief irq handling shutdown - */ -void metal_linux_irq_shutdown() + * @brief irq handling shutdown + */ +void metal_linux_irq_shutdown(void) { int ret; @@ -249,7 +257,8 @@ void metal_linux_irq_shutdown() metal_linux_irq_notify(); ret = pthread_join(irq_pthread, NULL); if (ret) { - metal_log(METAL_LOG_ERROR, "Failed to join IRQ thread: %d.\n", ret); + metal_log(METAL_LOG_ERROR, "Failed to join IRQ thread: %d.\n", + ret); } close(irq_notify_fd); metal_mutex_deinit(&irq_lock); @@ -258,8 +267,8 @@ void metal_linux_irq_shutdown() void metal_linux_irq_register_dev(struct metal_device *dev, int irq) { if (irq > MAX_IRQS) { - metal_log(METAL_LOG_ERROR, "Failed to register device to irq %d\n", - irq); + metal_log(METAL_LOG_ERROR, + "Failed to register device to irq %d\n", irq); return; } irqs_devs[irq] = dev; diff --git a/lib/system/linux/mutex.h b/lib/system/linux/mutex.h index 7fc1ccb..8b2862e 100644 --- a/lib/system/linux/mutex.h +++ b/lib/system/linux/mutex.h @@ -61,6 +61,7 @@ static inline void __metal_mutex_deinit(metal_mutex_t *mutex) static inline int __metal_mutex_try_acquire(metal_mutex_t *mutex) { int val = 0; + return atomic_compare_exchange_strong(&mutex->v, &val, 1); } diff --git a/lib/system/linux/shmem.c b/lib/system/linux/shmem.c index 067f86c..046192c 100644 --- a/lib/system/linux/shmem.c +++ b/lib/system/linux/shmem.c @@ -77,6 +77,7 @@ static int metal_shmem_try_map(struct metal_page_size *ps, int fd, size_t size, } else { for (virt = mem, page = 0; page < pages; page++) { size_t offset = page * ps->page_size; + error = metal_virt2phys(virt + offset, &phys[page]); if (error < 0) phys[page] = METAL_BAD_OFFSET; diff --git a/lib/system/linux/time.c b/lib/system/linux/time.c index 67ad5b7..6a40988 100644 --- a/lib/system/linux/time.c +++ b/lib/system/linux/time.c @@ -23,12 +23,12 @@ unsigned long long metal_get_timestamp(void) r = clock_gettime(CLOCK_MONOTONIC, &tp); if (r == -1) { - metal_log(METAL_LOG_ERROR,"clock_gettime failed!\n"); + metal_log(METAL_LOG_ERROR, "clock_gettime failed!\n"); return t; - } else { - t = tp.tv_sec * (NS_PER_S); - t += tp.tv_nsec; } + t = tp.tv_sec * (NS_PER_S); + t += tp.tv_nsec; + return t; } diff --git a/lib/system/linux/utilities.c b/lib/system/linux/utilities.c index a5e7773..7deda65 100644 --- a/lib/system/linux/utilities.c +++ b/lib/system/linux/utilities.c @@ -132,11 +132,12 @@ int metal_mktemp(char *template, int fifo) if (fifo) { result = mkfifo(template, mode); if (result < 0) { - if (errno == EEXIST) - continue; - metal_log(METAL_LOG_ERROR, "mkfifo(%s) failed (%s)\n", - template, strerror(errno)); - return -errno; + if (errno == EEXIST) + continue; + metal_log(METAL_LOG_ERROR, + "mkfifo(%s) failed (%s)\n", + template, strerror(errno)); + return -errno; } }