diff mbox series

[v2,2/3] include: Add a header to define PCI MMIO functions

Message ID 20250328190627.3025-3-alifm@linux.ibm.com (mailing list archive)
State New
Headers show
Series Enable QEMU NVMe userspace driver on s390x | expand

Commit Message

Farhan Ali March 28, 2025, 7:06 p.m. UTC
Add a generic QEMU API for PCI MMIO reads/writes.
The functions access little endian memory and returns
the result in host cpu endianness.

Signed-off-by: Farhan Ali <alifm@linux.ibm.com>
---
 include/qemu/pci-mmio.h | 116 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 116 insertions(+)
 create mode 100644 include/qemu/pci-mmio.h

Comments

Philippe Mathieu-Daudé March 28, 2025, 8:38 p.m. UTC | #1
On 28/3/25 20:06, Farhan Ali wrote:
> Add a generic QEMU API for PCI MMIO reads/writes.
> The functions access little endian memory and returns
> the result in host cpu endianness.
> 
> Signed-off-by: Farhan Ali <alifm@linux.ibm.com>
> ---
>   include/qemu/pci-mmio.h | 116 ++++++++++++++++++++++++++++++++++++++++
>   1 file changed, 116 insertions(+)
>   create mode 100644 include/qemu/pci-mmio.h
> 
> diff --git a/include/qemu/pci-mmio.h b/include/qemu/pci-mmio.h
> new file mode 100644
> index 0000000000..2ef92455b1
> --- /dev/null
> +++ b/include/qemu/pci-mmio.h
> @@ -0,0 +1,116 @@
> +/*
> + * QEMU PCI MMIO API
> + *
> + * Copyright 2025 IBM Corp.
> + * Author(s): Farhan Ali <alifm@linux.ibm.com>
> + *
> + * SPDX-License-Identifier: GPL-2.0-or-later
> + */
> +
> +#ifndef QEMU_PCI_MMIO_H
> +#define QEMU_PCI_MMIO_H
> +

Missing:

#include "qemu/bswap.h"

> +#ifdef __s390x__
> +#include "s390x_pci_mmio.h"
> +#endif
> +
> +static inline uint8_t qemu_pci_mmio_read_8(const void *ioaddr)
> +{
> +    uint8_t ret = 0;
> +#ifdef __s390x__
> +    ret = s390x_pci_mmio_read_8(ioaddr);
> +#else
> +    /* Prevent the compiler from optimizing away the load */
> +    ret = *((volatile uint8_t *)ioaddr);
> +#endif
> +
> +    return ret;
> +}
> +
> +static inline uint16_t qemu_pci_mmio_read_16(const void *ioaddr)
> +{
> +    uint16_t ret = 0;
> +#ifdef __s390x__
> +    ret = s390x_pci_mmio_read_16(ioaddr);
> +#else
> +    /* Prevent the compiler from optimizing away the load */
> +    ret = *((volatile uint16_t *)ioaddr);
> +#endif
> +
> +    return le16_to_cpu(ret);
> +}

Otherwise:
Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Philippe Mathieu-Daudé March 28, 2025, 8:44 p.m. UTC | #2
On 28/3/25 20:06, Farhan Ali wrote:
> Add a generic QEMU API for PCI MMIO reads/writes.
> The functions access little endian memory and returns
> the result in host cpu endianness.
> 
> Signed-off-by: Farhan Ali <alifm@linux.ibm.com>
> ---
>   include/qemu/pci-mmio.h | 116 ++++++++++++++++++++++++++++++++++++++++
>   1 file changed, 116 insertions(+)
>   create mode 100644 include/qemu/pci-mmio.h
> 
> diff --git a/include/qemu/pci-mmio.h b/include/qemu/pci-mmio.h
> new file mode 100644
> index 0000000000..2ef92455b1
> --- /dev/null
> +++ b/include/qemu/pci-mmio.h
> @@ -0,0 +1,116 @@
> +/*
> + * QEMU PCI MMIO API
> + *
> + * Copyright 2025 IBM Corp.
> + * Author(s): Farhan Ali <alifm@linux.ibm.com>
> + *
> + * SPDX-License-Identifier: GPL-2.0-or-later
> + */
> +
> +#ifndef QEMU_PCI_MMIO_H
> +#define QEMU_PCI_MMIO_H
> +
> +#ifdef __s390x__
 > +#include "s390x_pci_mmio.h"

Does this ifdef belong to the header instead?
Otherwise remove?

> +#endif
Farhan Ali March 29, 2025, 5:58 a.m. UTC | #3
On 3/28/2025 1:38 PM, Philippe Mathieu-Daudé wrote:
> On 28/3/25 20:06, Farhan Ali wrote:
>> Add a generic QEMU API for PCI MMIO reads/writes.
>> The functions access little endian memory and returns
>> the result in host cpu endianness.
>>
>> Signed-off-by: Farhan Ali <alifm@linux.ibm.com>
>> ---
>>   include/qemu/pci-mmio.h | 116 ++++++++++++++++++++++++++++++++++++++++
>>   1 file changed, 116 insertions(+)
>>   create mode 100644 include/qemu/pci-mmio.h
>>
>> diff --git a/include/qemu/pci-mmio.h b/include/qemu/pci-mmio.h
>> new file mode 100644
>> index 0000000000..2ef92455b1
>> --- /dev/null
>> +++ b/include/qemu/pci-mmio.h
>> @@ -0,0 +1,116 @@
>> +/*
>> + * QEMU PCI MMIO API
>> + *
>> + * Copyright 2025 IBM Corp.
>> + * Author(s): Farhan Ali <alifm@linux.ibm.com>
>> + *
>> + * SPDX-License-Identifier: GPL-2.0-or-later
>> + */
>> +
>> +#ifndef QEMU_PCI_MMIO_H
>> +#define QEMU_PCI_MMIO_H
>> +
>
> Missing:
>
> #include "qemu/bswap.h"
>
Thanks, will add this. Though I didn't have any issue compiling, but i 
think we should add this.


>> +#ifdef __s390x__
>> +#include "s390x_pci_mmio.h"
>> +#endif
>> +
>> +static inline uint8_t qemu_pci_mmio_read_8(const void *ioaddr)
>> +{
>> +    uint8_t ret = 0;
>> +#ifdef __s390x__
>> +    ret = s390x_pci_mmio_read_8(ioaddr);
>> +#else
>> +    /* Prevent the compiler from optimizing away the load */
>> +    ret = *((volatile uint8_t *)ioaddr);
>> +#endif
>> +
>> +    return ret;
>> +}
>> +
>> +static inline uint16_t qemu_pci_mmio_read_16(const void *ioaddr)
>> +{
>> +    uint16_t ret = 0;
>> +#ifdef __s390x__
>> +    ret = s390x_pci_mmio_read_16(ioaddr);
>> +#else
>> +    /* Prevent the compiler from optimizing away the load */
>> +    ret = *((volatile uint16_t *)ioaddr);
>> +#endif
>> +
>> +    return le16_to_cpu(ret);
>> +}
>
> Otherwise:
> Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
>
>
Farhan Ali March 29, 2025, 6:03 a.m. UTC | #4
On 3/28/2025 1:44 PM, Philippe Mathieu-Daudé wrote:
> On 28/3/25 20:06, Farhan Ali wrote:
>> Add a generic QEMU API for PCI MMIO reads/writes.
>> The functions access little endian memory and returns
>> the result in host cpu endianness.
>>
>> Signed-off-by: Farhan Ali <alifm@linux.ibm.com>
>> ---
>>   include/qemu/pci-mmio.h | 116 ++++++++++++++++++++++++++++++++++++++++
>>   1 file changed, 116 insertions(+)
>>   create mode 100644 include/qemu/pci-mmio.h
>>
>> diff --git a/include/qemu/pci-mmio.h b/include/qemu/pci-mmio.h
>> new file mode 100644
>> index 0000000000..2ef92455b1
>> --- /dev/null
>> +++ b/include/qemu/pci-mmio.h
>> @@ -0,0 +1,116 @@
>> +/*
>> + * QEMU PCI MMIO API
>> + *
>> + * Copyright 2025 IBM Corp.
>> + * Author(s): Farhan Ali <alifm@linux.ibm.com>
>> + *
>> + * SPDX-License-Identifier: GPL-2.0-or-later
>> + */
>> +
>> +#ifndef QEMU_PCI_MMIO_H
>> +#define QEMU_PCI_MMIO_H
>> +
>> +#ifdef __s390x__
> > +#include "s390x_pci_mmio.h"
>
> Does this ifdef belong to the header instead?
> Otherwise remove?

Just to clarify, are you suggesting to move this ifdef to the 
s390x_pci_mmio.h header file? so we can include s390x_pci_mmio.h file 
here without any ifdef?


>
>> +#endif
>
Philippe Mathieu-Daudé March 29, 2025, 6:50 a.m. UTC | #5
On 29/3/25 07:03, Farhan Ali wrote:
> 
> On 3/28/2025 1:44 PM, Philippe Mathieu-Daudé wrote:
>> On 28/3/25 20:06, Farhan Ali wrote:
>>> Add a generic QEMU API for PCI MMIO reads/writes.
>>> The functions access little endian memory and returns
>>> the result in host cpu endianness.
>>>
>>> Signed-off-by: Farhan Ali <alifm@linux.ibm.com>
>>> ---
>>>   include/qemu/pci-mmio.h | 116 ++++++++++++++++++++++++++++++++++++++++
>>>   1 file changed, 116 insertions(+)
>>>   create mode 100644 include/qemu/pci-mmio.h
>>>
>>> diff --git a/include/qemu/pci-mmio.h b/include/qemu/pci-mmio.h
>>> new file mode 100644
>>> index 0000000000..2ef92455b1
>>> --- /dev/null
>>> +++ b/include/qemu/pci-mmio.h
>>> @@ -0,0 +1,116 @@
>>> +/*
>>> + * QEMU PCI MMIO API
>>> + *
>>> + * Copyright 2025 IBM Corp.
>>> + * Author(s): Farhan Ali <alifm@linux.ibm.com>
>>> + *
>>> + * SPDX-License-Identifier: GPL-2.0-or-later
>>> + */
>>> +
>>> +#ifndef QEMU_PCI_MMIO_H
>>> +#define QEMU_PCI_MMIO_H
>>> +
>>> +#ifdef __s390x__
>> > +#include "s390x_pci_mmio.h"
>>
>> Does this ifdef belong to the header instead?
>> Otherwise remove?
> 
> Just to clarify, are you suggesting to move this ifdef to the 
> s390x_pci_mmio.h header file? so we can include s390x_pci_mmio.h file 
> here without any ifdef?

Exactly!
Stefan Hajnoczi March 31, 2025, 1:46 p.m. UTC | #6
On Fri, Mar 28, 2025 at 12:06:26PM -0700, Farhan Ali wrote:
> Add a generic QEMU API for PCI MMIO reads/writes.
> The functions access little endian memory and returns
> the result in host cpu endianness.
> 
> Signed-off-by: Farhan Ali <alifm@linux.ibm.com>
> ---
>  include/qemu/pci-mmio.h | 116 ++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 116 insertions(+)
>  create mode 100644 include/qemu/pci-mmio.h
> 
> diff --git a/include/qemu/pci-mmio.h b/include/qemu/pci-mmio.h
> new file mode 100644
> index 0000000000..2ef92455b1
> --- /dev/null
> +++ b/include/qemu/pci-mmio.h
> @@ -0,0 +1,116 @@
> +/*
> + * QEMU PCI MMIO API

QEMU also emulates PCI devices that handle MMIO accesses. It is easy to
get confused between host PCI MMIO accesses and emulated guest PCI MMIO
accesses if the name is just "PCI MMIO API".

Please update the commit message, filenames, function names, and doc
comments to make it clear that this is only for host PCI MMIO accesses
(e.g. Linux VFIO BAR accesses).

For example "qemu/host-pci-mmio.h", "API for host PCI MMIO accesses
(e.g. Linux VFIO BARs)", and host_pci_mmio_read_8().

> + *
> + * Copyright 2025 IBM Corp.
> + * Author(s): Farhan Ali <alifm@linux.ibm.com>
> + *
> + * SPDX-License-Identifier: GPL-2.0-or-later
> + */
> +
> +#ifndef QEMU_PCI_MMIO_H
> +#define QEMU_PCI_MMIO_H
> +
> +#ifdef __s390x__
> +#include "s390x_pci_mmio.h"
> +#endif
> +
> +static inline uint8_t qemu_pci_mmio_read_8(const void *ioaddr)
> +{
> +    uint8_t ret = 0;
> +#ifdef __s390x__
> +    ret = s390x_pci_mmio_read_8(ioaddr);
> +#else
> +    /* Prevent the compiler from optimizing away the load */
> +    ret = *((volatile uint8_t *)ioaddr);
> +#endif
> +
> +    return ret;
> +}
> +
> +static inline uint16_t qemu_pci_mmio_read_16(const void *ioaddr)
> +{
> +    uint16_t ret = 0;
> +#ifdef __s390x__
> +    ret = s390x_pci_mmio_read_16(ioaddr);
> +#else
> +    /* Prevent the compiler from optimizing away the load */
> +    ret = *((volatile uint16_t *)ioaddr);
> +#endif
> +
> +    return le16_to_cpu(ret);
> +}
> +
> +static inline uint32_t qemu_pci_mmio_read_32(const void *ioaddr)
> +{
> +    uint32_t ret = 0;
> +#ifdef __s390x__
> +    ret = s390x_pci_mmio_read_32(ioaddr);
> +#else
> +    /* Prevent the compiler from optimizing away the load */
> +    ret = *((volatile uint32_t *)ioaddr);
> +#endif
> +
> +    return le32_to_cpu(ret);
> +}
> +
> +static inline uint64_t qemu_pci_mmio_read_64(const void *ioaddr)
> +{
> +    uint64_t ret = 0;
> +#ifdef __s390x__
> +    ret = s390x_pci_mmio_read_64(ioaddr);
> +#else
> +    /* Prevent the compiler from optimizing away the load */
> +    ret = *((volatile uint64_t *)ioaddr);
> +#endif
> +
> +    return le64_to_cpu(ret);
> +}
> +
> +static inline void qemu_pci_mmio_write_8(void *ioaddr, uint8_t val)
> +{
> +
> +#ifdef __s390x__
> +    s390x_pci_mmio_write_8(ioaddr, val);
> +#else
> +    /* Prevent the compiler from optimizing away the store */
> +    *((volatile uint8_t *)ioaddr) = val;
> +#endif
> +}
> +
> +static inline void qemu_pci_mmio_write_16(void *ioaddr, uint16_t val)
> +{
> +    val = cpu_to_le16(val);
> +
> +#ifdef __s390x__
> +    s390x_pci_mmio_write_16(ioaddr, val);
> +#else
> +    /* Prevent the compiler from optimizing away the store */
> +    *((volatile uint16_t *)ioaddr) = val;
> +#endif
> +}
> +
> +static inline void qemu_pci_mmio_write_32(void *ioaddr, uint32_t val)
> +{
> +    val = cpu_to_le32(val);
> +
> +#ifdef __s390x__
> +    s390x_pci_mmio_write_32(ioaddr, val);
> +#else
> +    /* Prevent the compiler from optimizing away the store */
> +    *((volatile uint32_t *)ioaddr) = val;
> +#endif
> +}
> +
> +static inline void qemu_pci_mmio_write_64(void *ioaddr, uint64_t val)
> +{
> +    val = cpu_to_le64(val);
> +
> +#ifdef __s390x__
> +    s390x_pci_mmio_write_64(ioaddr, val);
> +#else
> +    /* Prevent the compiler from optimizing away the store */
> +    *((volatile uint64_t *)ioaddr) = val;
> +#endif
> +}
> +
> +#endif
> -- 
> 2.43.0
>
diff mbox series

Patch

diff --git a/include/qemu/pci-mmio.h b/include/qemu/pci-mmio.h
new file mode 100644
index 0000000000..2ef92455b1
--- /dev/null
+++ b/include/qemu/pci-mmio.h
@@ -0,0 +1,116 @@ 
+/*
+ * QEMU PCI MMIO API
+ *
+ * Copyright 2025 IBM Corp.
+ * Author(s): Farhan Ali <alifm@linux.ibm.com>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef QEMU_PCI_MMIO_H
+#define QEMU_PCI_MMIO_H
+
+#ifdef __s390x__
+#include "s390x_pci_mmio.h"
+#endif
+
+static inline uint8_t qemu_pci_mmio_read_8(const void *ioaddr)
+{
+    uint8_t ret = 0;
+#ifdef __s390x__
+    ret = s390x_pci_mmio_read_8(ioaddr);
+#else
+    /* Prevent the compiler from optimizing away the load */
+    ret = *((volatile uint8_t *)ioaddr);
+#endif
+
+    return ret;
+}
+
+static inline uint16_t qemu_pci_mmio_read_16(const void *ioaddr)
+{
+    uint16_t ret = 0;
+#ifdef __s390x__
+    ret = s390x_pci_mmio_read_16(ioaddr);
+#else
+    /* Prevent the compiler from optimizing away the load */
+    ret = *((volatile uint16_t *)ioaddr);
+#endif
+
+    return le16_to_cpu(ret);
+}
+
+static inline uint32_t qemu_pci_mmio_read_32(const void *ioaddr)
+{
+    uint32_t ret = 0;
+#ifdef __s390x__
+    ret = s390x_pci_mmio_read_32(ioaddr);
+#else
+    /* Prevent the compiler from optimizing away the load */
+    ret = *((volatile uint32_t *)ioaddr);
+#endif
+
+    return le32_to_cpu(ret);
+}
+
+static inline uint64_t qemu_pci_mmio_read_64(const void *ioaddr)
+{
+    uint64_t ret = 0;
+#ifdef __s390x__
+    ret = s390x_pci_mmio_read_64(ioaddr);
+#else
+    /* Prevent the compiler from optimizing away the load */
+    ret = *((volatile uint64_t *)ioaddr);
+#endif
+
+    return le64_to_cpu(ret);
+}
+
+static inline void qemu_pci_mmio_write_8(void *ioaddr, uint8_t val)
+{
+
+#ifdef __s390x__
+    s390x_pci_mmio_write_8(ioaddr, val);
+#else
+    /* Prevent the compiler from optimizing away the store */
+    *((volatile uint8_t *)ioaddr) = val;
+#endif
+}
+
+static inline void qemu_pci_mmio_write_16(void *ioaddr, uint16_t val)
+{
+    val = cpu_to_le16(val);
+
+#ifdef __s390x__
+    s390x_pci_mmio_write_16(ioaddr, val);
+#else
+    /* Prevent the compiler from optimizing away the store */
+    *((volatile uint16_t *)ioaddr) = val;
+#endif
+}
+
+static inline void qemu_pci_mmio_write_32(void *ioaddr, uint32_t val)
+{
+    val = cpu_to_le32(val);
+
+#ifdef __s390x__
+    s390x_pci_mmio_write_32(ioaddr, val);
+#else
+    /* Prevent the compiler from optimizing away the store */
+    *((volatile uint32_t *)ioaddr) = val;
+#endif
+}
+
+static inline void qemu_pci_mmio_write_64(void *ioaddr, uint64_t val)
+{
+    val = cpu_to_le64(val);
+
+#ifdef __s390x__
+    s390x_pci_mmio_write_64(ioaddr, val);
+#else
+    /* Prevent the compiler from optimizing away the store */
+    *((volatile uint64_t *)ioaddr) = val;
+#endif
+}
+
+#endif