forked from Mirrors/opensbi
lib: More improvements to sbi_fifo
This patch does following improvements to sbi_fifo: 1. Use valid SBI_Exxxx error codes instead of -1 2. The sbi_fifo_is_full() and sbi_fifo_is_empty() did not acquire qlock before accessing head and tail hence fixed it 3. Added avail member for ease in debugging and simplifying head/tail updates. Due to above changes size of sbi_fifo changes from 48 bytes to 56 bytes. Signed-off-by: Anup Patel <anup.patel@wdc.com>
This commit is contained in:
@@ -21,5 +21,6 @@
|
|||||||
#define SBI_ETIMEDOUT -8
|
#define SBI_ETIMEDOUT -8
|
||||||
#define SBI_EIO -9
|
#define SBI_EIO -9
|
||||||
#define SBI_EILL -10
|
#define SBI_EILL -10
|
||||||
|
#define SBI_ENOSPC -11
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@@ -15,18 +15,22 @@
|
|||||||
#include <sbi/sbi_types.h>
|
#include <sbi/sbi_types.h>
|
||||||
|
|
||||||
struct sbi_fifo {
|
struct sbi_fifo {
|
||||||
int head;
|
/* Static members of struct */
|
||||||
int tail;
|
|
||||||
spinlock_t qlock;
|
|
||||||
unsigned long entrysize;
|
|
||||||
unsigned long num_entries;
|
|
||||||
void *queue;
|
void *queue;
|
||||||
|
unsigned long entry_size;
|
||||||
|
unsigned long num_entries;
|
||||||
|
/* Dynamic members of struct protected by lock */
|
||||||
|
spinlock_t qlock;
|
||||||
|
unsigned long avail;
|
||||||
|
unsigned long head;
|
||||||
|
unsigned long tail;
|
||||||
};
|
};
|
||||||
|
|
||||||
int sbi_fifo_dequeue(struct sbi_fifo *fifo, void *data);
|
int sbi_fifo_dequeue(struct sbi_fifo *fifo, void *data);
|
||||||
int sbi_fifo_enqueue(struct sbi_fifo *fifo, void *data);
|
int sbi_fifo_enqueue(struct sbi_fifo *fifo, void *data);
|
||||||
void sbi_fifo_init(struct sbi_fifo *fifo, unsigned long entries,
|
void sbi_fifo_init(struct sbi_fifo *fifo, void *queue_mem,
|
||||||
unsigned long entrysize);
|
unsigned long entries, unsigned long entry_size);
|
||||||
bool sbi_fifo_is_empty(struct sbi_fifo *fifo);
|
bool sbi_fifo_is_empty(struct sbi_fifo *fifo);
|
||||||
bool sbi_fifo_is_full(struct sbi_fifo *fifo);
|
bool sbi_fifo_is_full(struct sbi_fifo *fifo);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@@ -8,51 +8,75 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
#include <sbi/riscv_locks.h>
|
#include <sbi/riscv_locks.h>
|
||||||
|
#include <sbi/sbi_error.h>
|
||||||
#include <sbi/sbi_fifo.h>
|
#include <sbi/sbi_fifo.h>
|
||||||
#include <plat/string.h>
|
#include <plat/string.h>
|
||||||
|
|
||||||
void sbi_fifo_init(struct sbi_fifo *fifo, unsigned long entries,
|
void sbi_fifo_init(struct sbi_fifo *fifo, void *queue_mem,
|
||||||
unsigned long size)
|
unsigned long entries, unsigned long entry_size)
|
||||||
{
|
{
|
||||||
fifo->head = -1;
|
fifo->queue = queue_mem;
|
||||||
fifo->tail = -1;
|
|
||||||
fifo->num_entries = entries;
|
fifo->num_entries = entries;
|
||||||
fifo->entrysize = size;
|
fifo->entry_size = entry_size;
|
||||||
SPIN_LOCK_INIT(&fifo->qlock);
|
SPIN_LOCK_INIT(&fifo->qlock);
|
||||||
memset(fifo->queue, 0, entries * size);
|
fifo->avail = fifo->head = fifo->tail = 0;
|
||||||
|
memset(fifo->queue, 0, entries * entry_size);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Note: must be called with fifo->qlock held */
|
||||||
|
static inline bool __sbi_fifo_is_full(struct sbi_fifo *fifo)
|
||||||
|
{
|
||||||
|
return (fifo->avail == fifo->num_entries) ? TRUE : FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool sbi_fifo_is_full(struct sbi_fifo *fifo)
|
bool sbi_fifo_is_full(struct sbi_fifo *fifo)
|
||||||
{
|
{
|
||||||
if (((fifo->head == fifo->num_entries-1) && fifo->tail == 0) ||
|
bool ret;
|
||||||
(fifo->tail == fifo->head + 1)) {
|
|
||||||
return TRUE;
|
spin_lock(&fifo->qlock);
|
||||||
}
|
ret = __sbi_fifo_is_full(fifo);
|
||||||
return FALSE;
|
spin_unlock(&fifo->qlock);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Note: must be called with fifo->qlock held */
|
||||||
|
static inline bool __sbi_fifo_is_empty(struct sbi_fifo *fifo)
|
||||||
|
{
|
||||||
|
return (fifo->avail == 0) ? TRUE : FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool sbi_fifo_is_empty(struct sbi_fifo *fifo)
|
bool sbi_fifo_is_empty(struct sbi_fifo *fifo)
|
||||||
{
|
{
|
||||||
if (fifo->head == -1)
|
bool ret;
|
||||||
return TRUE;
|
|
||||||
return FALSE;
|
spin_lock(&fifo->qlock);
|
||||||
|
ret = __sbi_fifo_is_empty(fifo);
|
||||||
|
spin_unlock(&fifo->qlock);
|
||||||
|
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int sbi_fifo_enqueue(struct sbi_fifo *fifo, void *data)
|
int sbi_fifo_enqueue(struct sbi_fifo *fifo, void *data)
|
||||||
{
|
{
|
||||||
if (!fifo || !data)
|
if (!fifo || !data)
|
||||||
return -1;
|
return SBI_EINVAL;
|
||||||
|
|
||||||
spin_lock(&fifo->qlock);
|
spin_lock(&fifo->qlock);
|
||||||
if (sbi_fifo_is_full(fifo)) {
|
|
||||||
|
if (__sbi_fifo_is_full(fifo)) {
|
||||||
spin_unlock(&fifo->qlock);
|
spin_unlock(&fifo->qlock);
|
||||||
return -1;
|
return SBI_ENOSPC;
|
||||||
}
|
}
|
||||||
if (fifo->tail == -1)
|
|
||||||
fifo->tail = 0;
|
memcpy(fifo->queue + fifo->head * fifo->entry_size, data,
|
||||||
fifo->head = (fifo->head + 1) % fifo->num_entries;
|
fifo->entry_size);
|
||||||
memcpy(fifo->queue + fifo->head * fifo->entrysize, data,
|
|
||||||
fifo->entrysize);
|
fifo->avail++;
|
||||||
|
fifo->head++;
|
||||||
|
if (fifo->head >= fifo->num_entries)
|
||||||
|
fifo->head = 0;
|
||||||
|
|
||||||
spin_unlock(&fifo->qlock);
|
spin_unlock(&fifo->qlock);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
@@ -61,22 +85,23 @@ int sbi_fifo_enqueue(struct sbi_fifo *fifo, void *data)
|
|||||||
int sbi_fifo_dequeue(struct sbi_fifo *fifo, void *data)
|
int sbi_fifo_dequeue(struct sbi_fifo *fifo, void *data)
|
||||||
{
|
{
|
||||||
if (!fifo || !data)
|
if (!fifo || !data)
|
||||||
return -1;
|
return SBI_EINVAL;
|
||||||
|
|
||||||
spin_lock(&fifo->qlock);
|
spin_lock(&fifo->qlock);
|
||||||
if (sbi_fifo_is_empty(fifo)) {
|
|
||||||
spin_unlock(&fifo->qlock);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
memcpy(data, fifo->queue + fifo->tail * fifo->entrysize,
|
|
||||||
fifo->entrysize);
|
|
||||||
|
|
||||||
if (fifo->tail == fifo->head) {
|
if (__sbi_fifo_is_empty(fifo)) {
|
||||||
fifo->tail = -1;
|
spin_unlock(&fifo->qlock);
|
||||||
fifo->head = -1;
|
return SBI_ENOENT;
|
||||||
} else {
|
|
||||||
fifo->tail = (fifo->tail + 1) % fifo->num_entries;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
memcpy(data, fifo->queue + fifo->tail * fifo->entry_size,
|
||||||
|
fifo->entry_size);
|
||||||
|
|
||||||
|
fifo->avail--;
|
||||||
|
fifo->tail++;
|
||||||
|
if (fifo->tail >= fifo->num_entries)
|
||||||
|
fifo->tail = 0;
|
||||||
|
|
||||||
spin_unlock(&fifo->qlock);
|
spin_unlock(&fifo->qlock);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
@@ -184,8 +184,8 @@ int sbi_ipi_init(struct sbi_scratch *scratch, bool cold_boot)
|
|||||||
struct sbi_fifo *tlb_info_q = sbi_tlb_fifo_head_ptr(scratch);
|
struct sbi_fifo *tlb_info_q = sbi_tlb_fifo_head_ptr(scratch);
|
||||||
|
|
||||||
sbi_ipi_data_ptr(scratch)->ipi_type = 0x00;
|
sbi_ipi_data_ptr(scratch)->ipi_type = 0x00;
|
||||||
tlb_info_q->queue = sbi_tlb_fifo_mem_ptr(scratch);
|
sbi_fifo_init(tlb_info_q, sbi_tlb_fifo_mem_ptr(scratch),
|
||||||
sbi_fifo_init(tlb_info_q, SBI_TLB_FIFO_NUM_ENTRIES, SBI_TLB_INFO_SIZE);
|
SBI_TLB_FIFO_NUM_ENTRIES, SBI_TLB_INFO_SIZE);
|
||||||
|
|
||||||
/* Enable software interrupts */
|
/* Enable software interrupts */
|
||||||
csr_set(CSR_MIE, MIP_MSIP);
|
csr_set(CSR_MIE, MIP_MSIP);
|
||||||
|
Reference in New Issue
Block a user