Skip to contentSkip to conversation

util/fifo8: Introduce fifo8_change_capacity()

PATCH Superseded Philippe Mathieu-Daudé 3 participants 10 messages Jul 19, 2024 – Jul 22, 2024

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover util/fifo8: Introduce fifo8_change_capacity()

Preliminary series to PL011 FIFO work:
- Clarify doc in qemu_chr_fe_*
- Introduce fifo8_change_capacity()

Philippe Mathieu-Daudé (3):
chardev/char-fe: Document returned value on error util/fifo8: Use fifo8_reset() in fifo8_create() util/fifo8: Introduce fifo8_change_capacity()

 include/chardev/char-fe.h |  3 +++
 include/qemu/fifo8.h      | 10 ++++++++++
 util/fifo8.c              | 22 ++++++++++++++--------
 3 files changed, 27 insertions(+), 8 deletions(-)
Signature · 1 line
2.41.0
#1 lore raw

PATCH 1/3 chardev/char-fe: Document returned value on error

qemu_chr_fe_add_watch() and qemu_chr_fe_write[_all]() return -1 on error. Mention it in the documentation.

---
 include/chardev/char-fe.h | 3 +++
 1 file changed, 3 insertions(+)
include/chardev/char-fe.h +3 -0
index ecef182835..3310449eaf 100644
@@ -228,6 +228,7 @@ guint qemu_chr_fe_add_watch(CharBackend *be, GIOCondition cond,
  * is thread-safe.
  *
  * Returns: the number of bytes consumed (0 if no associated Chardev)
+ *          or -1 on error.
  */
 int qemu_chr_fe_write(CharBackend *be, const uint8_t *buf, int len);
 
@@ -242,6 +243,7 @@ int qemu_chr_fe_write(CharBackend *be, const uint8_t *buf, int len);
  * attempted to be written.  This function is thread-safe.
  *
  * Returns: the number of bytes consumed (0 if no associated Chardev)
+ *          or -1 on error.
  */
 int qemu_chr_fe_write_all(CharBackend *be, const uint8_t *buf, int len);
 
@@ -253,6 +255,7 @@ int qemu_chr_fe_write_all(CharBackend *be, const uint8_t *buf, int len);
  * Read data to a buffer from the back end.
  *
  * Returns: the number of bytes read (0 if no associated Chardev)
+ *          or -1 on error.
  */
 int qemu_chr_fe_read_all(CharBackend *be, uint8_t *buf, int len);
 
Signature · 1 line
2.41.0
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] chardev/char-fe: Document returned value on error

Quoted 29 lines · Philippe Mathieu-Daudé

qemu_chr_fe_add_watch() and qemu_chr_fe_write[_all]() return -1 on error. Mention it in the documentation.

---
  include/chardev/char-fe.h | 3 +++
  1 file changed, 3 insertions(+)
include/chardev/char-fe.h +2 -0
index ecef182835..3310449eaf 100644
@@ -228,6 +228,7 @@ guint qemu_chr_fe_add_watch(CharBackend *be, GIOCondition cond,
   * is thread-safe.
   *
   * Returns: the number of bytes consumed (0 if no associated Chardev)
+ *          or -1 on error.
   */
  int qemu_chr_fe_write(CharBackend *be, const uint8_t *buf, int len);
 
@@ -242,6 +243,7 @@ int qemu_chr_fe_write(CharBackend *be, const uint8_t *buf, int len);
   * attempted to be written.  This function is thread-safe.
   *
   * Returns: the number of bytes consumed (0 if no associated Chardev)
+ *          or -1 on error.
   */
  int qemu_chr_fe_write_all(CharBackend *be, const uint8_t *buf, int len);
 

include/chardev/char-fe.h int qemu_chr_fe_write_all(CharBackend *be, const uint8_t *buf, int len); · line 260

Quoted 6 lines
include/chardev/char-fe.h +1 -0
@@ -253,6 +255,7 @@ int qemu_chr_fe_write_all(CharBackend *be, const uint8_t *buf, int len);
   * Read data to a buffer from the back end.
   *
   * Returns: the number of bytes read (0 if no associated Chardev)
+ *          or -1 on error.
   */
  int qemu_chr_fe_read_all(CharBackend *be, uint8_t *buf, int len);

ATB,

Mark.

#3 lore raw

PATCH 2/3 util/fifo8: Use fifo8_reset() in fifo8_create()

In reply to Philippe Mathieu-Daudé

Avoid open-coding fifo8_reset() in fifo8_create().

---
 util/fifo8.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)
util/fifo8.c +7 -8
index 4e01b532d9..2925fe5611 100644
@@ -16,12 +16,17 @@
 #include "migration/vmstate.h"
 #include "qemu/fifo8.h"
 
+void fifo8_reset(Fifo8 *fifo)
+{
+    fifo->num = 0;
+    fifo->head = 0;
+}
+
 void fifo8_create(Fifo8 *fifo, uint32_t capacity)
 {
     fifo->data = g_new(uint8_t, capacity);
     fifo->capacity = capacity;
-    fifo->head = 0;
-    fifo->num = 0;
+    fifo8_reset(fifo);
 }
 
 void fifo8_destroy(Fifo8 *fifo)
@@ -97,12 +102,6 @@ const uint8_t *fifo8_pop_buf(Fifo8 *fifo, uint32_t max, uint32_t *numptr)
     return fifo8_peekpop_buf(fifo, max, numptr, true);
 }
 
-void fifo8_reset(Fifo8 *fifo)
-{
-    fifo->num = 0;
-    fifo->head = 0;
-}
-
 bool fifo8_is_empty(Fifo8 *fifo)
 {
     return (fifo->num == 0);
Signature · 1 line
2.41.0
Reviewed-by on v1 2/3
#4 lore raw

Re: [PATCH 2/3] util/fifo8: Use fifo8_reset() in fifo8_create()

Quoted 34 lines · Philippe Mathieu-Daudé

Avoid open-coding fifo8_reset() in fifo8_create().

---
  util/fifo8.c | 15 +++++++--------
  1 file changed, 7 insertions(+), 8 deletions(-)
util/fifo8.c +7 -2
index 4e01b532d9..2925fe5611 100644
@@ -16,12 +16,17 @@
  #include "migration/vmstate.h"
  #include "qemu/fifo8.h"
 
+void fifo8_reset(Fifo8 *fifo)
+{
+    fifo->num = 0;
+    fifo->head = 0;
+}
+
  void fifo8_create(Fifo8 *fifo, uint32_t capacity)
  {
      fifo->data = g_new(uint8_t, capacity);
      fifo->capacity = capacity;
-    fifo->head = 0;
-    fifo->num = 0;
+    fifo8_reset(fifo);
  }
 
  void fifo8_destroy(Fifo8 *fifo)
@@ -97,12 +102,6 @@ const uint8_t *fifo8_pop_buf(Fifo8 *fifo, uint32_t max, uint32_t *numptr)
      return fifo8_peekpop_buf(fifo, max, numptr, true);
  }

util/fifo8.c const uint8_t *fifo8_pop_buf(Fifo8 *fifo, uint32_t max, uint32_t *numptr) · line 107

Quoted 10 lines
util/fifo8.c +0 -6
 
-void fifo8_reset(Fifo8 *fifo)
-{
-    fifo->num = 0;
-    fifo->head = 0;
-}
-
  bool fifo8_is_empty(Fifo8 *fifo)
  {
      return (fifo->num == 0);

ATB,

Mark.

#5 lore raw

PATCH 3/3 util/fifo8: Introduce fifo8_change_capacity()

In reply to Philippe Mathieu-Daudé

FIFOs can be resized at runtime. Introduce the
fifo8_change_capacity() method to do that.
When capacity is changed, the FIFO must be reset.

---
 include/qemu/fifo8.h | 10 ++++++++++
 util/fifo8.c         |  7 +++++++
 2 files changed, 17 insertions(+)
include/qemu/fifo8.h +10 -0
index c6295c6ff0..9fe0555a24 100644
@@ -31,6 +31,16 @@ void fifo8_create(Fifo8 *fifo, uint32_t capacity);
 
 void fifo8_destroy(Fifo8 *fifo);
 
+/**
+ * fifo8_change_capacity:
+ * @fifo: struct Fifo8 to change the capacity
+ * @capacity: new capacity of the FIFO
+ *
+ * Change a FIFO capacity to the specified size. The FIFO is reset.
+ */
+
+void fifo8_change_capacity(Fifo8 *fifo, uint32_t capacity);
+
 /**
  * fifo8_push:
  * @fifo: FIFO to push to
util/fifo8.c +7 -0
index 2925fe5611..c453afd774 100644
@@ -34,6 +34,13 @@ void fifo8_destroy(Fifo8 *fifo)
     g_free(fifo->data);
 }
 
+void fifo8_change_capacity(Fifo8 *fifo, uint32_t capacity)
+{
+    fifo->data = g_renew(uint8_t, fifo->data, capacity);
+    fifo->capacity = capacity;
+    fifo8_reset(fifo);
+}
+
 void fifo8_push(Fifo8 *fifo, uint8_t data)
 {
     assert(fifo->num < fifo->capacity);
Signature · 1 line
2.41.0
#6 lore raw
Quoted 39 lines · Philippe Mathieu-Daudé

FIFOs can be resized at runtime. Introduce the
fifo8_change_capacity() method to do that.
When capacity is changed, the FIFO must be reset.

---
  include/qemu/fifo8.h | 10 ++++++++++
  util/fifo8.c         |  7 +++++++
  2 files changed, 17 insertions(+)
include/qemu/fifo8.h +10 -0
index c6295c6ff0..9fe0555a24 100644
@@ -31,6 +31,16 @@ void fifo8_create(Fifo8 *fifo, uint32_t capacity);
 
  void fifo8_destroy(Fifo8 *fifo);
 
+/**
+ * fifo8_change_capacity:
+ * @fifo: struct Fifo8 to change the capacity
+ * @capacity: new capacity of the FIFO
+ *
+ * Change a FIFO capacity to the specified size. The FIFO is reset.
+ */
+
+void fifo8_change_capacity(Fifo8 *fifo, uint32_t capacity);
+
  /**
   * fifo8_push:
   * @fifo: FIFO to push to
util/fifo8.c +0 -0
index 2925fe5611..c453afd774 100644
@@ -34,6 +34,13 @@ void fifo8_destroy(Fifo8 *fifo)
      g_free(fifo->data);
  }
 

util/fifo8.c void fifo8_destroy(Fifo8 *fifo) · line 46

Quoted 10 lines
util/fifo8.c +7 -0
+void fifo8_change_capacity(Fifo8 *fifo, uint32_t capacity)
+{
+    fifo->data = g_renew(uint8_t, fifo->data, capacity);
+    fifo->capacity = capacity;
+    fifo8_reset(fifo);
+}
+
  void fifo8_push(Fifo8 *fifo, uint8_t data)
  {
      assert(fifo->num < fifo->capacity);

The changes look okay, however I'm a little confused as to why this is needed as generally hardware FIFOs are a fixed size? Presumably this is related to the PL011 series?

ATB,

Mark.

#7 lore raw
Quoted 42 lines · Mark Cave-Ayland
Quoted 40 lines · Philippe Mathieu-Daudé

FIFOs can be resized at runtime. Introduce the
fifo8_change_capacity() method to do that.
When capacity is changed, the FIFO must be reset.

---
  include/qemu/fifo8.h | 10 ++++++++++
  util/fifo8.c         |  7 +++++++

  2 files changed, 17 insertions(+)
include/qemu/fifo8.h +10 -0
index c6295c6ff0..9fe0555a24 100644
@@ -31,6 +31,16 @@ void fifo8_create(Fifo8 *fifo, uint32_t capacity);
   void fifo8_destroy(Fifo8 *fifo);
+/**
+ * fifo8_change_capacity:
+ * @fifo: struct Fifo8 to change the capacity
+ * @capacity: new capacity of the FIFO
+ *
+ * Change a FIFO capacity to the specified size. The FIFO is reset.
+ */
+
+void fifo8_change_capacity(Fifo8 *fifo, uint32_t capacity);
+
   /**
    * fifo8_push:
    * @fifo: FIFO to push to
util/fifo8.c +4 -0
index 2925fe5611..c453afd774 100644
@@ -34,6 +34,13 @@ void fifo8_destroy(Fifo8 *fifo)
       g_free(fifo->data);
   }
+void fifo8_change_capacity(Fifo8 *fifo, uint32_t capacity)
+{
+    fifo->data = g_renew(uint8_t, fifo->data, capacity);
+    fifo->capacity = capacity;
Quoted 8 lines

util/fifo8.c void fifo8_destroy(Fifo8 *fifo) · line 46

Quoted 6 lines
util/fifo8.c +3 -0
+    fifo8_reset(fifo);
+}
+
   void fifo8_push(Fifo8 *fifo, uint8_t data)
   {
       assert(fifo->num < fifo->capacity);

The changes look okay, however I'm a little confused as to why this is needed as generally hardware FIFOs are a fixed size? Presumably this is related to the PL011 series?

Indeed, this is to model trying to stay as close as possible to the datasheet, which states:

2.4.3 UART operation

Disabling the FIFOs

Additionally, you can disable the FIFOs. In this case, the transmit and receive sides of the UART have 1-byte holding registers (the bottom entry of the FIFOs). The overrun bit is set when a word has been received, and the previous one was not yet read. In this implementation, the FIFOs are not physically disabled, but the flags are manipulated to give the illusion of a 1-byte register.

#8 lore raw
Quoted 62 lines · Philippe Mathieu-Daudé
Quoted 44 lines · Mark Cave-Ayland
Quoted 42 lines · Philippe Mathieu-Daudé

FIFOs can be resized at runtime. Introduce the
fifo8_change_capacity() method to do that.
When capacity is changed, the FIFO must be reset.

---
  include/qemu/fifo8.h | 10 ++++++++++
  util/fifo8.c         |  7 +++++++
  2 files changed, 17 insertions(+)
include/qemu/fifo8.h +10 -0
index c6295c6ff0..9fe0555a24 100644
@@ -31,6 +31,16 @@ void fifo8_create(Fifo8 *fifo, uint32_t capacity);
  void fifo8_destroy(Fifo8 *fifo);
+/**
+ * fifo8_change_capacity:
+ * @fifo: struct Fifo8 to change the capacity
+ * @capacity: new capacity of the FIFO
+ *
+ * Change a FIFO capacity to the specified size. The FIFO is reset.
+ */
+
+void fifo8_change_capacity(Fifo8 *fifo, uint32_t capacity);
+
  /**
   * fifo8_push:
   * @fifo: FIFO to push to
util/fifo8.c +6 -0
index 2925fe5611..c453afd774 100644
@@ -34,6 +34,13 @@ void fifo8_destroy(Fifo8 *fifo)
      g_free(fifo->data);
  }
+void fifo8_change_capacity(Fifo8 *fifo, uint32_t capacity)
+{
+    fifo->data = g_renew(uint8_t, fifo->data, capacity);
+    fifo->capacity = capacity;
+    fifo8_reset(fifo);
+}
Quoted 8 lines

util/fifo8.c void fifo8_destroy(Fifo8 *fifo) · line 46

Quoted 4 lines
util/fifo8.c +1 -0
+
  void fifo8_push(Fifo8 *fifo, uint8_t data)
  {
      assert(fifo->num < fifo->capacity);

The changes look okay, however I'm a little confused as to why this is needed as generally hardware FIFOs are a fixed size? Presumably this is related to the PL011 series?

Indeed, this is to model trying to stay as close as possible to the datasheet, which states:

2.4.3 UART operation

Disabling the FIFOs

Quoted 7 lines

Additionally, you can disable the FIFOs. In this case, the transmit and receive sides of the UART have 1-byte holding registers (the bottom entry of the FIFOs). The overrun bit is set when a word has been received, and the previous one was not yet read. In this implementation, the FIFOs are not physically disabled, but the flags are manipulated to give the illusion of a 1-byte register.

Notice that in the hardware we don't actually resize the FIFO, though -- we just only ever keep one element in it...

thanks
-- PMM

#9 lore raw
Quoted 71 lines · Peter Maydell
Quoted 62 lines · Philippe Mathieu-Daudé
Quoted 44 lines · Mark Cave-Ayland
Quoted 42 lines · Philippe Mathieu-Daudé

FIFOs can be resized at runtime. Introduce the
fifo8_change_capacity() method to do that.
When capacity is changed, the FIFO must be reset.

---
   include/qemu/fifo8.h | 10 ++++++++++
   util/fifo8.c         |  7 +++++++
   2 files changed, 17 insertions(+)
include/qemu/fifo8.h +10 -0
index c6295c6ff0..9fe0555a24 100644
@@ -31,6 +31,16 @@ void fifo8_create(Fifo8 *fifo, uint32_t capacity);
   void fifo8_destroy(Fifo8 *fifo);
+/**
+ * fifo8_change_capacity:
+ * @fifo: struct Fifo8 to change the capacity
+ * @capacity: new capacity of the FIFO
+ *
+ * Change a FIFO capacity to the specified size. The FIFO is reset.
+ */
+
+void fifo8_change_capacity(Fifo8 *fifo, uint32_t capacity);
+
   /**
    * fifo8_push:
    * @fifo: FIFO to push to
util/fifo8.c +6 -0
index 2925fe5611..c453afd774 100644
@@ -34,6 +34,13 @@ void fifo8_destroy(Fifo8 *fifo)
       g_free(fifo->data);
   }
+void fifo8_change_capacity(Fifo8 *fifo, uint32_t capacity)
+{
+    fifo->data = g_renew(uint8_t, fifo->data, capacity);
+    fifo->capacity = capacity;
+    fifo8_reset(fifo);
+}
Quoted 8 lines

util/fifo8.c void fifo8_destroy(Fifo8 *fifo) · line 46

Quoted 4 lines
util/fifo8.c +1 -0
+
   void fifo8_push(Fifo8 *fifo, uint8_t data)
   {
       assert(fifo->num < fifo->capacity);

The changes look okay, however I'm a little confused as to why this is needed as generally hardware FIFOs are a fixed size? Presumably this is related to the PL011 series?

Indeed, this is to model trying to stay as close as possible to the datasheet, which states:

2.4.3 UART operation

Disabling the FIFOs

Quoted 7 lines

Additionally, you can disable the FIFOs. In this case, the transmit and receive sides of the UART have 1-byte holding registers (the bottom entry of the FIFOs). The overrun bit is set when a word has been received, and the previous one was not yet read. In this implementation, the FIFOs are not physically disabled, but the flags are manipulated to give the illusion of a 1-byte register.

Quoted 2 lines

Notice that in the hardware we don't actually resize the FIFO, though -- we just only ever keep one element in it...

Yes. IIUC Mark's comment in the PL011 series, we don't need this (confusing) method at all:
https://lore.kernel.org/qemu-devel/[email protected]/