Skip to contentSkip to conversation

Add Cadence PCIe endpoint driver with new uclass

PATCH Superseded Ramon Fried 2 participants 11 messages Apr 6, 2019 – Apr 25, 2019

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover Add Cadence PCIe endpoint driver with new uclass

This patchset adds support for new uclass, UCLASS_PCI_EP allowing new set of PCI endpoint drivers.
Included in the patchset is also a driver for Cadence PCIe endpoint.

Ramon Fried (3):
drivers: pci_ep: Introduce UCLASS_PCI_EP uclass
pci: pci.h: add missing maskbit
pci_ep: add Cadence PCIe endpoint driver

 .../pci_endpoint/cdns,cdns-pcie-ep.txt        |  18 +
 drivers/Kconfig                               |   2 +
 drivers/Makefile                              |   1 +
 drivers/pci_endpoint/Kconfig                  |  25 ++
 drivers/pci_endpoint/Makefile                 |   7 +
 drivers/pci_endpoint/pci_ep-uclass.c          | 192 +++++++++
 drivers/pci_endpoint/pcie-cadence-ep.c        | 177 +++++++++
 drivers/pci_endpoint/pcie-cadence.h           | 309 +++++++++++++++
 include/dm/uclass-id.h                        |   1 +
 include/pci.h                                 |   1 +
 include/pci_ep.h                              | 375 ++++++++++++++++++
 11 files changed, 1108 insertions(+)
 create mode 100644 doc/device-tree-bindings/pci_endpoint/cdns,cdns-pcie-ep.txt
 create mode 100644 drivers/pci_endpoint/Kconfig
 create mode 100644 drivers/pci_endpoint/Makefile
 create mode 100644 drivers/pci_endpoint/pci_ep-uclass.c
 create mode 100644 drivers/pci_endpoint/pcie-cadence-ep.c
 create mode 100644 drivers/pci_endpoint/pcie-cadence.h
 create mode 100644 include/pci_ep.h
Signature · 1 line
2.21.0
#1 lore raw

PATCH 1/3 drivers: pci_ep: Introduce UCLASS_PCI_EP uclass

Introduce new UCLASS_PCI_EP class for handling PCI endpoint devices, allowing to set various attributes of the PCI endpoint device, such as:
* configuration space header
* BAR definitions
* outband memory mapping
* start/stop PCI link

---

 drivers/Kconfig                      |   2 +
 drivers/Makefile                     |   1 +
 drivers/pci_endpoint/Kconfig         |  16 ++
 drivers/pci_endpoint/Makefile        |   6 +
 drivers/pci_endpoint/pci_ep-uclass.c | 192 ++++++++++++++
 include/dm/uclass-id.h               |   1 +
 include/pci_ep.h                     | 375 +++++++++++++++++++++++++++
 7 files changed, 593 insertions(+)
 create mode 100644 drivers/pci_endpoint/Kconfig
 create mode 100644 drivers/pci_endpoint/Makefile
 create mode 100644 drivers/pci_endpoint/pci_ep-uclass.c
 create mode 100644 include/pci_ep.h
drivers/Kconfig +2 -0
index f24351ac4f..59e2c22cc6 100644
@@ -64,6 +64,8 @@ source "drivers/nvme/Kconfig"
 
 source "drivers/pci/Kconfig"
 
+source "drivers/pci_endpoint/Kconfig"
+
 source "drivers/pch/Kconfig"
 
 source "drivers/pcmcia/Kconfig"
drivers/Makefile +1 -0
index a7bba3ed56..480b97ef58 100644
@@ -85,6 +85,7 @@ obj-$(CONFIG_FPGA) += fpga/
 obj-y += misc/
 obj-$(CONFIG_MMC) += mmc/
 obj-$(CONFIG_NVME) += nvme/
+obj-$(CONFIG_PCI_ENDPOINT) += pci_endpoint/
 obj-y += pcmcia/
 obj-y += dfu/
 obj-$(CONFIG_PCH) += pch/
drivers/pci_endpoint/Kconfig new file +16 -0
new file mode 100644
index 0000000000..2c0a399a08
@@ -0,0 +1,16 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# PCI Endpoint Support
+#
+
+menu "PCI Endpoint"
+
+config PCI_ENDPOINT
+	bool "PCI Endpoint Support"
+	depends on DM
+	help
+	   Enable this configuration option to support configurable PCI
+	   endpoint. This should be enabled if the platform has a PCI
+	   controller that can operate in endpoint mode.
+
+endmenu
drivers/pci_endpoint/Makefile new file +6 -0
new file mode 100644
index 0000000000..80a1066925
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0+
+#
+# (C) Copyright 2019
+# Ramon Fried <[email protected]>
+
+obj-y += pci_ep-uclass.o
drivers/pci_endpoint/pci_ep-uclass.c new file +192 -0

192 lines not on this page · raw message

include/dm/uclass-id.h +1 -0
index 86e59781b0..d29f7d5a34 100644
@@ -67,6 +67,7 @@ enum uclass_id {
 	UCLASS_PANEL_BACKLIGHT,	/* Backlight controller for panel */
 	UCLASS_PCH,		/* x86 platform controller hub */
 	UCLASS_PCI,		/* PCI bus */
+	UCLASS_PCI_EP,		/* PCI endpoint device */
 	UCLASS_PCI_GENERIC,	/* Generic PCI bus device */
 	UCLASS_PHY,		/* Physical Layer (PHY) device */
 	UCLASS_PINCONFIG,	/* Pin configuration node device */
include/pci_ep.h new file +375 -0

375 lines not on this page · raw message

Signature · 1 line
2.21.0
#2 lore raw

PATCH 1/3 drivers: pci_ep: Introduce UCLASS_PCI_EP uclass

Hi Ramon,

Quoted 13 lines · Ramon Fried

Introduce new UCLASS_PCI_EP class for handling PCI endpoint devices, allowing to set various attributes of the PCI endpoint device, such as:
* configuration space header
* BAR definitions
* outband memory mapping
* start/stop PCI link

---

Quoted 12 lines
 drivers/Kconfig                      |   2 +
 drivers/Makefile                     |   1 +
 drivers/pci_endpoint/Kconfig         |  16 ++
 drivers/pci_endpoint/Makefile        |   6 +
 drivers/pci_endpoint/pci_ep-uclass.c | 192 ++++++++++++++
 include/dm/uclass-id.h               |   1 +
 include/pci_ep.h                     | 375 +++++++++++++++++++++++++++
 7 files changed, 593 insertions(+)
 create mode 100644 drivers/pci_endpoint/Kconfig
 create mode 100644 drivers/pci_endpoint/Makefile
 create mode 100644 drivers/pci_endpoint/pci_ep-uclass.c
 create mode 100644 include/pci_ep.h

This looks pretty good but I have a lot of nits, sorry.

Please can you add a sandbox driver and a test for this (can be in a separate patch).

Quoted 35 lines
drivers/Kconfig +2 -0
index f24351ac4f..59e2c22cc6 100644
@@ -64,6 +64,8 @@ source "drivers/nvme/Kconfig"
 
 source "drivers/pci/Kconfig"
 
+source "drivers/pci_endpoint/Kconfig"
+
 source "drivers/pch/Kconfig"
 
 source "drivers/pcmcia/Kconfig"
drivers/Makefile +1 -0
index a7bba3ed56..480b97ef58 100644
@@ -85,6 +85,7 @@ obj-$(CONFIG_FPGA) += fpga/
 obj-y += misc/
 obj-$(CONFIG_MMC) += mmc/
 obj-$(CONFIG_NVME) += nvme/
+obj-$(CONFIG_PCI_ENDPOINT) += pci_endpoint/
 obj-y += pcmcia/
 obj-y += dfu/
 obj-$(CONFIG_PCH) += pch/
drivers/pci_endpoint/Kconfig new file +3 -0
new file mode 100644
index 0000000000..2c0a399a08
@@ -0,0 +1,16 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# PCI Endpoint Support

drivers/pci_endpoint/Kconfig line 13

Quoted 10 lines
drivers/pci_endpoint/Kconfig new file +10 -0
+#
+
+menu "PCI Endpoint"
+
+config PCI_ENDPOINT
+       bool "PCI Endpoint Support"
+       depends on DM
+       help
+          Enable this configuration option to support configurable PCI
+          endpoint. This should be enabled if the platform has a PCI

s/endpoint/endpoints/ I think

drivers/pci_endpoint/Kconfig line 14

Quoted 1 line
drivers/pci_endpoint/Kconfig new file +1 -0
+          controller that can operate in endpoint mode.

Can you explain a bit more about what this means. I understand that people can look at the spec, but what is the purpose of 'endpoint mode' and what does it enable?

Quoted 36 lines
drivers/pci_endpoint/Kconfig new file +2 -0
+
+endmenu
drivers/pci_endpoint/Makefile new file +6 -0
new file mode 100644
index 0000000000..80a1066925
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0+
+#
+# (C) Copyright 2019
+# Ramon Fried <[email protected]>
+
+obj-y += pci_ep-uclass.o
drivers/pci_endpoint/pci_ep-uclass.c new file +16 -0
new file mode 100644
index 0000000000..06fcfc5d14
@@ -0,0 +1,192 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * PCI Endpoint uclass
+ *
+ * Based on Linux PCI-EP driver written by
+ * Kishon Vijay Abraham I <[email protected]>
+ *
+ * Copyright (c) 2019
+ * Written by Ramon Fried <[email protected]>
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <errno.h>
+#include <linux/log2.h>
+#include <pci_ep.h>

drivers/pci_endpoint/pci_ep-uclass.c line 26

Quoted 10 lines
drivers/pci_endpoint/pci_ep-uclass.c new file +10 -0
+
+DECLARE_GLOBAL_DATA_PTR;
+
+int dm_pci_ep_write_header(struct udevice *dev, u8 fn,
+                          struct pci_ep_header *hdr)
+{
+       struct dm_pci_ep_ops *ops = pci_ep_get_ops(dev);
+
+       if (!ops->write_header)
+               return -ENODEV;

-ENOSYS (please fix globally)

There is a device. The problem here is that the driver does not implement the method.

Quoted 6 lines
drivers/pci_endpoint/pci_ep-uclass.c new file +6 -0
+
+       return ops->write_header(dev, fn, hdr);
+}
+
+int dm_pci_ep_read_header(struct udevice *dev, u8 fn,
+                         struct pci_ep_header *hdr)

drivers/pci_endpoint/pci_ep-uclass.c line 42

Quoted 10 lines
drivers/pci_endpoint/pci_ep-uclass.c new file +10 -0
+{
+       struct dm_pci_ep_ops *ops = pci_ep_get_ops(dev);
+
+       if (!ops->read_header)
+               return -ENODEV;
+
+       return ops->read_header(dev, fn, hdr);
+}
+
+int dm_pci_ep_set_bar(struct udevice *dev, u8 func_no,

Please use uint for func_no. There is no need to limit it to 8 bits, and this may not be efficient for non-8-bit machines. Please fix globally.

drivers/pci_endpoint/pci_ep-uclass.c line 52

Quoted 10 lines
drivers/pci_endpoint/pci_ep-uclass.c new file +10 -0
+                     struct pci_bar *ep_bar)
+{
+       struct dm_pci_ep_ops *ops = pci_ep_get_ops(dev);
+       int flags = ep_bar->flags;
+
+       /* Some basic bar validity checks */
+       if ((ep_bar->barno == BAR_5 && flags &
+            PCI_BASE_ADDRESS_MEM_TYPE_64) ||
+           (flags & PCI_BASE_ADDRESS_SPACE_IO &&
+            flags & PCI_BASE_ADDRESS_IO_MASK) ||

Can you add another set of () in those two lines?

drivers/pci_endpoint/pci_ep-uclass.c line 63

Quoted 11 lines
drivers/pci_endpoint/pci_ep-uclass.c new file +11 -0
+           (upper_32_bits(ep_bar->size) &&
+            !(flags & PCI_BASE_ADDRESS_MEM_TYPE_64)))
+               return -EINVAL;
+
+       if (!ops->set_bar)
+               return -ENODEV;
+
+       return ops->set_bar(dev, func_no, ep_bar);
+}
+
+void pci_ep_clear_bar(struct udevice *dev, u8 func_num, enum pci_barno bar)

This should return an error code.

Quoted 21 lines
drivers/pci_endpoint/pci_ep-uclass.c new file +21 -0
+{
+       struct dm_pci_ep_ops *ops = pci_ep_get_ops(dev);
+
+       if (!ops->clear_bar)
+               return;
+
+       ops->clear_bar(dev, func_num, bar);
+}
+
+int dm_pci_ep_map_addr(struct udevice *dev, u8 func_no, phys_addr_t addr,
+                      u64 pci_addr, size_t size)
+{
+       struct dm_pci_ep_ops *ops = pci_ep_get_ops(dev);
+
+       if (!ops->map_addr)
+               return -ENODEV;
+
+       return ops->map_addr(dev, func_no, addr, pci_addr, size);
+}
+
+void dm_pci_ep_unmap_addr(struct udevice *dev, u8 func_no, phys_addr_t addr)

drivers/pci_endpoint/pci_ep-uclass.c line 94

Quoted 10 lines
drivers/pci_endpoint/pci_ep-uclass.c new file +10 -0
+{
+       struct dm_pci_ep_ops *ops = pci_ep_get_ops(dev);
+
+       if (!ops->unmap_addr)
+               return;
+
+       ops->unmap_addr(dev, func_no, addr);
+}
+
+int dm_pci_ep_set_msi(struct udevice *dev, u8 func_no, u8 interrupts)

Similar with interrupts (use uint).

drivers/pci_endpoint/pci_ep-uclass.c line 97

Quoted 3 lines
drivers/pci_endpoint/pci_ep-uclass.c new file +3 -0
+{
+       struct dm_pci_ep_ops *ops = pci_ep_get_ops(dev);
+       u8 encode_int;

uint

Quoted 16 lines
drivers/pci_endpoint/pci_ep-uclass.c new file +16 -0
+
+       if (interrupts > 32)
+               return -EINVAL;
+
+       if (!ops->set_msi)
+               return -ENODEV;
+
+       encode_int = order_base_2(interrupts);
+
+       return ops->set_msi(dev, func_no, encode_int);
+}
+
+int dm_pci_ep_get_msi(struct udevice *dev, u8 func_no)
+{
+       struct dm_pci_ep_ops *ops = pci_ep_get_ops(dev);
+       int interrupt;

drivers/pci_endpoint/pci_ep-uclass.c line 123

Quoted 10 lines
drivers/pci_endpoint/pci_ep-uclass.c new file +10 -0
+
+       if (!ops->get_msi)
+               return -ENODEV;
+
+       interrupt = ops->get_msi(dev, func_no);
+
+       if (interrupt < 0)
+               return 0;
+
+       interrupt = 1 << interrupt;

Perhaps you should declare a 'mask' variable? In any case, it is confusing for the same variable to have two different meanings, and it does not save code@run-time.

drivers/pci_endpoint/pci_ep-uclass.c line 128

Quoted 5 lines
drivers/pci_endpoint/pci_ep-uclass.c new file +5 -0
+
+       return interrupt;
+}
+
+int dm_pci_ep_set_msix(struct udevice *dev, u8 func_no, u16 interrupts)

s/u16/uint/

There is no saving by using u16, only potential cost. You range-check it below anyway. Please fix globally.

Quoted 16 lines
drivers/pci_endpoint/pci_ep-uclass.c new file +16 -0
+{
+       struct dm_pci_ep_ops *ops = pci_ep_get_ops(dev);
+
+       if (interrupts < 1 || interrupts > 2048)
+               return -EINVAL;
+
+       if (!ops->set_msix)
+               return -ENODEV;
+
+       return ops->set_msix(dev, func_no, interrupts);
+}
+
+int dm_pci_ep_get_msix(struct udevice *dev, u8 func_no)
+{
+       struct dm_pci_ep_ops *ops = pci_ep_get_ops(dev);
+       int interrupt;

drivers/pci_endpoint/pci_ep-uclass.c line 154

Quoted 10 lines
drivers/pci_endpoint/pci_ep-uclass.c new file +10 -0
+
+       if (!ops->get_msix)
+               return -ENODEV;
+
+       interrupt = ops->get_msix(dev, func_no);
+
+       if (interrupt < 0)
+               return 0;
+
+       return interrupt + 1;

It's odd that your uclass function returns a different value from your driver function. I think that will be confusing. Is it necessary?

Quoted 14 lines
drivers/pci_endpoint/pci_ep-uclass.c new file +14 -0
+}
+
+int dm_pci_ep_raise_irq(struct udevice *dev, u8 func_no,
+                       enum pci_ep_irq_type type, u16 interrupt_num)
+{
+       struct dm_pci_ep_ops *ops = pci_ep_get_ops(dev);
+
+       if (!ops->raise_irq)
+               return -ENODEV;
+
+       return ops->raise_irq(dev, func_no, type, interrupt_num);
+}
+
+int dm_pci_ep_start(struct udevice *dev)

drivers/pci_endpoint/pci_ep-uclass.c line 178

Quoted 10 lines
drivers/pci_endpoint/pci_ep-uclass.c new file +10 -0
+{
+       struct dm_pci_ep_ops *ops = pci_ep_get_ops(dev);
+
+       if (!ops->start)
+               return -ENODEV;
+
+       return ops->start(dev);
+}
+
+void dm_pci_ep_stop(struct udevice *dev)

Needs a return value.

Quoted 74 lines
drivers/pci_endpoint/pci_ep-uclass.c new file +14 -0
+{
+       struct dm_pci_ep_ops *ops = pci_ep_get_ops(dev);
+
+       if (!ops->stop)
+               return;
+
+       ops->stop(dev);
+}
+
+UCLASS_DRIVER(pci_ep) = {
+       .id             = UCLASS_PCI_EP,
+       .name           = "pci_ep",
+       .flags          = DM_UC_FLAG_SEQ_ALIAS,
+};
include/dm/uclass-id.h +1 -0
index 86e59781b0..d29f7d5a34 100644
@@ -67,6 +67,7 @@ enum uclass_id {
        UCLASS_PANEL_BACKLIGHT, /* Backlight controller for panel */
        UCLASS_PCH,             /* x86 platform controller hub */
        UCLASS_PCI,             /* PCI bus */
+       UCLASS_PCI_EP,          /* PCI endpoint device */
        UCLASS_PCI_GENERIC,     /* Generic PCI bus device */
        UCLASS_PHY,             /* Physical Layer (PHY) device */
        UCLASS_PINCONFIG,       /* Pin configuration node device */
include/pci_ep.h new file +42 -0
new file mode 100644
index 0000000000..c98453ccfa
@@ -0,0 +1,375 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Adapted from Linux kernel driver
+ * Copyright (C) 2017 Texas Instruments
+ * Author: Kishon Vijay Abraham I <[email protected]>
+ *
+ * (C) Copyright 2019
+ * Ramon Fried <[email protected]>
+ */
+
+#ifndef _PCI_EP_H
+#define _PCI_EP_H
+
+#include <pci.h>
+
+/**
+ * enum pci_interrupt_pin - PCI INTx interrupt values
+ * @PCI_INTERRUPT_UNKNOWN: Unknown or unassigned interrupt
+ * @PCI_INTERRUPT_INTA: PCI INTA pin
+ * @PCI_INTERRUPT_INTB: PCI INTB pin
+ * @PCI_INTERRUPT_INTC: PCI INTC pin
+ * @PCI_INTERRUPT_INTD: PCI INTD pin
+ *
+ * Corresponds to values for legacy PCI INTx interrupts, as can be found in the
+ * PCI_INTERRUPT_PIN register.
+ */
+enum pci_interrupt_pin {
+       PCI_INTERRUPT_UNKNOWN,
+       PCI_INTERRUPT_INTA,
+       PCI_INTERRUPT_INTB,
+       PCI_INTERRUPT_INTC,
+       PCI_INTERRUPT_INTD,
+};
+
+enum pci_barno {
+       BAR_0,
+       BAR_1,
+       BAR_2,
+       BAR_3,
+       BAR_4,
+       BAR_5,
+};

include/pci_ep.h line 52

Quoted 10 lines
include/pci_ep.h new file +10 -0
+
+enum pci_ep_irq_type {
+       PCI_EP_IRQ_UNKNOWN,
+       PCI_EP_IRQ_LEGACY,
+       PCI_EP_IRQ_MSI,
+       PCI_EP_IRQ_MSIX,
+};
+
+/**
+ * struct pci_bar - represents the BAR of EP device

What is a BAR and what is an EP? Please spell things out in comments, at least ones per file:

base-address register (BAR)

include/pci_ep.h line 54

Quoted 2 lines
include/pci_ep.h new file +2 -0
+ * @phys_addr: physical address that should be mapped to the BAR
+ * @size: the size of the address space present in BAR

Please add the other two also

Quoted 7 lines
include/pci_ep.h new file +7 -0
+ */
+struct pci_bar {
+       dma_addr_t      phys_addr;
+       size_t          size;
+       enum pci_barno  barno;
+       int             flags;
+};

include/pci_ep.h line 71

Quoted 10 lines
include/pci_ep.h new file +10 -0
+
+/**
+ * struct pci_ep_header - represents standard configuration header
+ * @vendorid: identifies device manufacturer
+ * @deviceid: identifies a particular device
+ * @revid: specifies a device-specific revision identifier
+ * @progif_code: identifies a specific register-level programming interface
+ * @subclass_code: identifies more specifically the function of the device
+ * @baseclass_code: broadly classifies the type of function the device performs
+ * @cache_line_size: specifies the system cacheline size in units of DWORDs

What is a DWORD? I think i is 32-bits, so just say that, since WORD is a confusing term on a non-16-bit machine.

Quoted 5 lines
include/pci_ep.h new file +5 -0
+ * @subsys_vendor_id: vendor of the add-in card or subsystem
+ * @subsys_id: id specific to vendor
+ * @interrupt_pin: interrupt pin the device (or device function) uses
+ */
+struct pci_ep_header {

include/pci_ep.h line 86

Quoted 10 lines
include/pci_ep.h new file +10 -0
+       u16     vendorid;
+       u16     deviceid;
+       u8      revid;
+       u8      progif_code;
+       u8      subclass_code;
+       u8      baseclass_code;
+       u8      cache_line_size;
+       u16     subsys_vendor_id;
+       u16     subsys_id;
+       enum pci_interrupt_pin interrupt_pin;

Shouldn't that be a u16 or something? The enum does not have a defined size, I think.

include/pci_ep.h line 95

Quoted 9 lines
include/pci_ep.h new file +9 -0
+};
+
+/* PCI endpoint operations */
+struct dm_pci_ep_ops {
+       /**
+        * write_header() - Write a PCI configuration space header
+        *
+        * @dev:        device to write to
+        * @func_num:   Function to fill

Is this the function number within the device? If so, how about 'Function number (within device) to write to'? Similarly below.

Quoted 7 lines
include/pci_ep.h new file +7 -0
+        * @hdr:        header to write
+        * @return 0 if OK, -ve on error
+        */
+       int     (*write_header)(struct udevice *dev, u8 func_num,
+                               struct pci_ep_header *hdr);
+       /**
+        * read_header() - Read a PCI configuration space header

include/pci_ep.h line 112

Quoted 10 lines
include/pci_ep.h new file +10 -0
+        *
+        * @dev:        device to write to
+        * @func_num:   Function to fill
+        * @hdr:        header to read to
+        * @return 0 if OK, -ve on error
+        */
+       int     (*read_header)(struct udevice *dev, u8 func_num,
+                              struct pci_ep_header *hdr);
+       /**
+        * set_bar() - set BAR (Base Address Register)

Good to expand BAR as you have here

Quoted 67 lines
include/pci_ep.h new file +67 -0
+        *
+        * @dev:        device to set
+        * @func_num:   Function to set
+        * @bar:        bar data
+        * @return 0 if OK, -ve on error
+        */
+       int     (*set_bar)(struct udevice *dev, u8 func_num,
+                          struct pci_bar *bar);
+       /**
+        * clear_bar() - clear BAR (Base Address Register)
+        *
+        * @dev:        device to clear
+        * @func_num:   Function to clear
+        * @bar:        bar number
+        */
+       void    (*clear_bar)(struct udevice *dev, u8 func_num,
+                            enum pci_barno bar);
+       /**
+        * map_addr() - map CPU address to PCI address
+        *
+        * outband region is used in order to generate PCI read/write
+        * transaction from local memory/write.
+        *
+        * @dev:        device to set
+        * @func_num:   Function to set
+        * @addr:       local physical address base
+        * @pci_addr:   pci address to translate to
+        * @size:       region size
+        * @return 0 if OK, -ve on error
+        */
+       int     (*map_addr)(struct udevice *dev, u8 func_num,
+                           phys_addr_t addr, u64 pci_addr, size_t size);
+       /**
+        * unmap_addr() - unmap CPU address to PCI address
+        *
+        * unmap previously mapped region.
+        *
+        * @dev:        device to set
+        * @func_num:   Function to set
+        * @addr:       local physical address base
+        */
+       void    (*unmap_addr)(struct udevice *dev, u8 func_num,
+                             phys_addr_t addr);
+       /**
+        * set_msi() - set msi capability property
+        *
+        * set the number of required MSI vectors the device
+        * needs for operation.
+        *
+        * @dev:        device to set
+        * @func_num:   Function to set
+        * @interrupts: required interrupts count
+        * @return 0 if OK, -ve on error
+        */
+       int     (*set_msi)(struct udevice *dev, u8 func_num, u8 interrupts);
+
+       /**
+        * get_msi() - get the number of MSI interrupts allocated by the host.
+        *
+        * Read the Multiple Message Enable bitfield from
+        * Message control register.
+        *
+        * @dev:        device to use
+        * @func_num:   Function to use
+        * @return msi count if OK, -EINVAL if msi were not enabled at host.
+        */
+       int     (*get_msi)(struct udevice *dev, u8 func_num);

include/pci_ep.h line 189

Quoted 10 lines
include/pci_ep.h new file +10 -0
+
+       /**
+        * set_msix() - set msix capability property
+        *
+        * set the number of required MSIx vectors the device
+        * needs for operation.
+        *
+        * @dev:        device to set
+        * @func_num:   Function to set
+        * @interrupts: required interrupts count

This is too vague, please expand it.

Quoted 20 lines
include/pci_ep.h new file +20 -0
+        * @return 0 if OK, -ve on error
+        */
+       int     (*set_msix)(struct udevice *dev, u8 func_num, u16 interrupts);
+
+       /**
+        * get_msix() - get the number of MSIx interrupts allocated by the host.
+        *
+        * Read the Multiple Message Enable bitfield from
+        * Message control register.
+        *
+        * @dev:        device to use
+        * @func_num:   Function to use
+        * @return msi count if OK, -EINVAL if msi were not enabled at host.
+        */
+       int     (*get_msix)(struct udevice *dev, u8 func_num);
+
+       /**
+        * raise_irq() - raise a legacy, MSI or MSI-X interrupt
+        *
+        * @dev:        device to set

include/pci_ep.h line 219

Quoted 10 lines
include/pci_ep.h new file +10 -0
+        * @func_num:   Function to set
+        * @type:       type of irq to send
+        * @interrupt_num: interrupt vector to use
+        * @return 0 if OK, -ve on error
+        */
+       int     (*raise_irq)(struct udevice *dev, u8 func_num,
+                            enum pci_ep_irq_type type, u16 interrupt_num);
+       /**
+        * start() - start the PCI link
+        *

Needs more docs. What is the effect of this?

include/pci_ep.h line 226

Quoted 7 lines
include/pci_ep.h new file +7 -0
+        * @dev:        device to set
+        * @return 0 if OK, -ve on error
+        */
+       int     (*start)(struct udevice *dev);
+
+       /**
+        * stop() - stop the PCI link

Needs more docs. What is the effect of this?

Quoted 150 lines
include/pci_ep.h new file +149 -1
+        *
+        * @dev:        device to set
+        */
+       void    (*stop)(struct udevice *dev);
+};
+
+#define pci_ep_get_ops(dev)    ((struct dm_pci_ep_ops *)(dev)->driver->ops)
+
+/**
+ * pci_ep_write_header() - Write a PCI configuration space header
+ *
+ * @dev:       device to write to
+ * @func_num:  Function to fill
+ * @hdr:       header to write
+ * @return 0 if OK, -ve on error
+ */
+int pci_ep_write_header(struct udevice *dev, u8 func_num,
+                       struct pci_ep_header *hdr);
+
+/**
+ * dm_pci_ep_read_header() - Read a PCI configuration space header
+ *
+ * @dev:       device to write to
+ * @func_num:  Function to fill
+ * @hdr:       header to read to
+ * @return 0 if OK, -ve on error
+ */
+int pci_ep_read_header(struct udevice *dev, u8 func_num,
+                      struct pci_ep_header *hdr);
+/**
+ * pci_ep_set_bar() - set BAR (Base Address Register)
+ *
+ * @dev:       device to set
+ * @func_num:  Function to set
+ * @bar:       bar data
+ * @return 0 if OK, -ve on error
+ */
+int pci_ep_set_bar(struct udevice *dev, u8 func_num, struct pci_bar *bar);
+
+/**
+ * pci_ep_clear_bar() - clear BAR (Base Address Register)
+ *
+ * @dev:       device to clear
+ * @func_num:  Function to clear
+ * @bar:       bar number
+ */
+void pci_ep_clear_bar(struct udevice *dev, u8 func_num, enum pci_barno bar);
+/**
+ * pci_ep_map_addr() - map CPU address to PCI address
+ *
+ * outband region is used in order to generate PCI read/write
+ * transaction from local memory/write.
+ *
+ * @dev:       device to set
+ * @func_num:  Function to set
+ * @addr:      local physical address base
+ * @pci_addr:  pci address to translate to
+ * @size:      region size
+ * @return 0 if OK, -ve on error
+ */
+int pci_ep_map_addr(struct udevice *dev, u8 func_num, phys_addr_t addr,
+                   u64 pci_addr, size_t size);
+/**
+ * pci_ep_unmap_addr() - unmap CPU address to PCI address
+ *
+ * unmap previously mapped region.
+ *
+ * @dev:       device to set
+ * @func_num:  Function to set
+ * @addr:      local physical address base
+ */
+void pci_ep_unmap_addr(struct udevice *dev, u8 func_num, phys_addr_t addr);
+/**
+ * pci_ep_set_msi() - set msi capability property
+ *
+ * set the number of required MSI vectors the device
+ * needs for operation.
+ *
+ * @dev:       device to set
+ * @func_num:  Function to set
+ * @interrupts:        required interrupts count
+ * @return 0 if OK, -ve on error
+ */
+int pci_ep_set_msi(struct udevice *dev, u8 func_num, u8 interrupts);
+
+/**
+ * pci_ep_get_msi() - get the number of MSI interrupts allocated by the host.
+ *
+ * Read the Multiple Message Enable bitfield from
+ * Message control register.
+ *
+ * @dev:       device to use
+ * @func_num:  Function to use
+ * @return msi count if OK, -EINVAL if msi were not enabled at host.
+ */
+int pci_ep_get_msi(struct udevice *dev, u8 func_num);
+
+/**
+ * pci_ep_set_msix() - set msi capability property
+ *
+ * set the number of required MSIx vectors the device
+ * needs for operation.
+ *
+ * @dev:       device to set
+ * @func_num:  Function to set
+ * @interrupts:        required interrupts count
+ * @return 0 if OK, -ve on error
+ */
+int pci_ep_set_msix(struct udevice *dev, u8 func_num, u16 interrupts);
+
+/**
+ * pci_ep_get_msix() - get the number of MSIx interrupts allocated by the host.
+ *
+ * Read the Multiple Message Enable bitfield from
+ * Message control register.
+ *
+ * @dev:       device to use
+ * @func_num:  Function to use
+ * @return msi count if OK, -EINVAL if msi were not enabled at host.
+ */
+int pci_ep_get_msix(struct udevice *dev, u8 func_num);
+
+/**
+ * pci_ep_raise_irq() - raise a legacy, MSI or MSI-X interrupt
+ *
+ * @dev:       device to set
+ * @func_num:  Function to set
+ * @type:      type of irq to send
+ * @interrupt_num: interrupt vector to use
+ * @return 0 if OK, -ve on error
+ */
+int pci_ep_raise_irq(struct udevice *dev, u8 func_num,
+                    enum pci_ep_irq_type type, u16 interrupt_num);
+/**
+ * pci_ep_start() - start the PCI link
+ *
+ * @dev:       device to set
+ * @return 0 if OK, -ve on error
+ */
+int pci_ep_start(struct udevice *dev);
+
+/**
+ * pci_ep_stop() - stop the PCI link
+ *
+ * @dev:       device to set
+ */
+void pci_ep_stop(struct udevice *dev);
+
+#endif
--
Quoted 2 lines

2.21.0

Regards,
Simon

#3 lore raw

PATCH 1/3 drivers: pci_ep: Introduce UCLASS_PCI_EP uclass

Hi Simon,
Thanks for the review.
please see inline, I have few questions/suggestions regarding your notes.

Thanks,
Ramon.

Quoted 87 lines · Simon Glass

Hi Ramon,

Quoted 13 lines · Ramon Fried

Introduce new UCLASS_PCI_EP class for handling PCI endpoint devices, allowing to set various attributes of the PCI endpoint device, such as:
* configuration space header
* BAR definitions
* outband memory mapping
* start/stop PCI link

---

Quoted 12 lines
 drivers/Kconfig                      |   2 +
 drivers/Makefile                     |   1 +
 drivers/pci_endpoint/Kconfig         |  16 ++
 drivers/pci_endpoint/Makefile        |   6 +
 drivers/pci_endpoint/pci_ep-uclass.c | 192 ++++++++++++++
 include/dm/uclass-id.h               |   1 +
 include/pci_ep.h                     | 375 +++++++++++++++++++++++++++
 7 files changed, 593 insertions(+)
 create mode 100644 drivers/pci_endpoint/Kconfig
 create mode 100644 drivers/pci_endpoint/Makefile
 create mode 100644 drivers/pci_endpoint/pci_ep-uclass.c
 create mode 100644 include/pci_ep.h

This looks pretty good but I have a lot of nits, sorry.

no worries.

Please can you add a sandbox driver and a test for this (can be in a separate patch).

sure.

Quoted 35 lines
drivers/Kconfig +2 -0
index f24351ac4f..59e2c22cc6 100644
@@ -64,6 +64,8 @@ source "drivers/nvme/Kconfig"
 
 source "drivers/pci/Kconfig"
 
+source "drivers/pci_endpoint/Kconfig"
+
 source "drivers/pch/Kconfig"
 
 source "drivers/pcmcia/Kconfig"
drivers/Makefile +1 -0
index a7bba3ed56..480b97ef58 100644
@@ -85,6 +85,7 @@ obj-$(CONFIG_FPGA) += fpga/
 obj-y += misc/
 obj-$(CONFIG_MMC) += mmc/
 obj-$(CONFIG_NVME) += nvme/
+obj-$(CONFIG_PCI_ENDPOINT) += pci_endpoint/
 obj-y += pcmcia/
 obj-y += dfu/
 obj-$(CONFIG_PCH) += pch/
drivers/pci_endpoint/Kconfig new file +3 -0
new file mode 100644
index 0000000000..2c0a399a08
@@ -0,0 +1,16 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# PCI Endpoint Support

drivers/pci_endpoint/Kconfig line 13

Quoted 10 lines
drivers/pci_endpoint/Kconfig new file +10 -0
+#
+
+menu "PCI Endpoint"
+
+config PCI_ENDPOINT
+       bool "PCI Endpoint Support"
+       depends on DM
+       help
+          Enable this configuration option to support configurable PCI
+          endpoint. This should be enabled if the platform has a PCI

s/endpoint/endpoints/ I think

Quoted 4 lines

Right.

Quoted 1 line
drivers/pci_endpoint/Kconfig new file +1 -0
+          controller that can operate in endpoint mode.

Can you explain a bit more about what this means. I understand that

Quoted 71 lines

people can look at the spec, but what is the purpose of 'endpoint mode' and what does it enable?

Review

Quoted 3 lines
drivers/pci_endpoint/Kconfig new file +2 -0
+
+endmenu
(quoted diff) +0 -0

b/drivers/pci_endpoint/Makefile

Review

Quoted 12 lines

new file mode 100644
index 0000000000..80a1066925

drivers/pci_endpoint/Makefile new file +6 -0
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0+
+#
+# (C) Copyright 2019
+# Ramon Fried <[email protected]>
+
+obj-y += pci_ep-uclass.o
(quoted diff) +0 -0

b/drivers/pci_endpoint/pci_ep-uclass.c

Quoted 21 lines

new file mode 100644
index 0000000000..06fcfc5d14

drivers/pci_endpoint/pci_ep-uclass.c new file +16 -0
@@ -0,0 +1,192 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * PCI Endpoint uclass
+ *
+ * Based on Linux PCI-EP driver written by
+ * Kishon Vijay Abraham I <[email protected]>
+ *
+ * Copyright (c) 2019
+ * Written by Ramon Fried <[email protected]>
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <errno.h>
+#include <linux/log2.h>
+#include <pci_ep.h>

drivers/pci_endpoint/pci_ep-uclass.c line 26

Quoted 10 lines
drivers/pci_endpoint/pci_ep-uclass.c new file +10 -0
+
+DECLARE_GLOBAL_DATA_PTR;
+
+int dm_pci_ep_write_header(struct udevice *dev, u8 fn,
+                          struct pci_ep_header *hdr)
+{
+       struct dm_pci_ep_ops *ops = pci_ep_get_ops(dev);
+
+       if (!ops->write_header)
+               return -ENODEV;

-ENOSYS (please fix globally)

OK.

There is a device. The problem here is that the driver does not implement the method.

Quoted 12 lines
drivers/pci_endpoint/pci_ep-uclass.c new file +12 -0
+
+       return ops->write_header(dev, fn, hdr);
+}
+
+int dm_pci_ep_read_header(struct udevice *dev, u8 fn,
+                         struct pci_ep_header *hdr)
+{
+       struct dm_pci_ep_ops *ops = pci_ep_get_ops(dev);
+
+       if (!ops->read_header)
+               return -ENODEV;
+
Quoted 9 lines

drivers/pci_endpoint/pci_ep-uclass.c line 42

Quoted 4 lines
drivers/pci_endpoint/pci_ep-uclass.c new file +4 -0
+       return ops->read_header(dev, fn, hdr);
+}
+
+int dm_pci_ep_set_bar(struct udevice *dev, u8 func_no,

Please use uint for func_no. There is no need to limit it to 8 bits, and this may not be efficient for non-8-bit machines. Please fix globally.

I tried to keep the API as similar as it can to the Linux API. I can change it, no problem, but IMHO I think it's better to keep it similar.

Quoted 14 lines

drivers/pci_endpoint/pci_ep-uclass.c line 52

Quoted 11 lines
drivers/pci_endpoint/pci_ep-uclass.c new file +10 -0
+                     struct pci_bar *ep_bar)
+{
+       struct dm_pci_ep_ops *ops = pci_ep_get_ops(dev);
+       int flags = ep_bar->flags;
+
+       /* Some basic bar validity checks */
+       if ((ep_bar->barno == BAR_5 && flags &
+            PCI_BASE_ADDRESS_MEM_TYPE_64) ||
+           (flags & PCI_BASE_ADDRESS_SPACE_IO &&
+            flags & PCI_BASE_ADDRESS_IO_MASK) ||

Can you add another set of () in those two lines?

yep

Quoted 14 lines

drivers/pci_endpoint/pci_ep-uclass.c

Quoted 12 lines
drivers/pci_endpoint/pci_ep-uclass.c new file +11 -0
+           (upper_32_bits(ep_bar->size) &&
+            !(flags & PCI_BASE_ADDRESS_MEM_TYPE_64)))
+               return -EINVAL;
+
+       if (!ops->set_bar)
+               return -ENODEV;
+
+       return ops->set_bar(dev, func_no, ep_bar);
+}
+
+void pci_ep_clear_bar(struct udevice *dev, u8 func_num, enum pci_barno

bar)

Quoted 2 lines

This should return an error code.

ok

Quoted 65 lines

drivers/pci_endpoint/pci_ep-uclass.c

Quoted 11 lines
drivers/pci_endpoint/pci_ep-uclass.c new file +10 -0
+{
+       struct dm_pci_ep_ops *ops = pci_ep_get_ops(dev);
+
+       if (!ops->clear_bar)
+               return;
+
+       ops->clear_bar(dev, func_num, bar);
+}
+
+int dm_pci_ep_map_addr(struct udevice *dev, u8 func_no, phys_addr_t

addr,

drivers/pci_endpoint/pci_ep-uclass.c

Quoted 11 lines
drivers/pci_endpoint/pci_ep-uclass.c new file +11 -0
+                      u64 pci_addr, size_t size)
+{
+       struct dm_pci_ep_ops *ops = pci_ep_get_ops(dev);
+
+       if (!ops->map_addr)
+               return -ENODEV;
+
+       return ops->map_addr(dev, func_no, addr, pci_addr, size);
+}
+
+void dm_pci_ep_unmap_addr(struct udevice *dev, u8 func_no, phys_addr_t

addr)

drivers/pci_endpoint/pci_ep-uclass.c line 94

Quoted 10 lines
drivers/pci_endpoint/pci_ep-uclass.c new file +10 -0
+{
+       struct dm_pci_ep_ops *ops = pci_ep_get_ops(dev);
+
+       if (!ops->unmap_addr)
+               return;
+
+       ops->unmap_addr(dev, func_no, addr);
+}
+
+int dm_pci_ep_set_msi(struct udevice *dev, u8 func_no, u8 interrupts)

Similar with interrupts (use uint).

drivers/pci_endpoint/pci_ep-uclass.c line 97

Quoted 3 lines
drivers/pci_endpoint/pci_ep-uclass.c new file +3 -0
+{
+       struct dm_pci_ep_ops *ops = pci_ep_get_ops(dev);
+       u8 encode_int;

uint

Quoted 22 lines
drivers/pci_endpoint/pci_ep-uclass.c new file +22 -0
+
+       if (interrupts > 32)
+               return -EINVAL;
+
+       if (!ops->set_msi)
+               return -ENODEV;
+
+       encode_int = order_base_2(interrupts);
+
+       return ops->set_msi(dev, func_no, encode_int);
+}
+
+int dm_pci_ep_get_msi(struct udevice *dev, u8 func_no)
+{
+       struct dm_pci_ep_ops *ops = pci_ep_get_ops(dev);
+       int interrupt;
+
+       if (!ops->get_msi)
+               return -ENODEV;
+
+       interrupt = ops->get_msi(dev, func_no);
+
Quoted 9 lines

drivers/pci_endpoint/pci_ep-uclass.c line 123

Quoted 4 lines
drivers/pci_endpoint/pci_ep-uclass.c new file +4 -0
+       if (interrupt < 0)
+               return 0;
+
+       interrupt = 1 << interrupt;

Perhaps you should declare a 'mask' variable? In any case, it is confusing for the same variable to have two different meanings, and it does not save code at run-time.

as before, this is practically a copy-paste from Linux, I can change it, but I think it's clearer if the code looks the same as in Linux, might be easier the port patches like that.

Quoted 9 lines

drivers/pci_endpoint/pci_ep-uclass.c line 128

Quoted 6 lines
drivers/pci_endpoint/pci_ep-uclass.c new file +5 -0
+
+       return interrupt;
+}
+
+int dm_pci_ep_set_msix(struct udevice *dev, u8 func_no, u16 interrupts)

s/u16/uint/

Quoted 3 lines

There is no saving by using u16, only potential cost. You range-check it below anyway. Please fix globally.

ok

Quoted 22 lines
Quoted 22 lines
drivers/pci_endpoint/pci_ep-uclass.c new file +21 -0
+{
+       struct dm_pci_ep_ops *ops = pci_ep_get_ops(dev);
+
+       if (interrupts < 1 || interrupts > 2048)
+               return -EINVAL;
+
+       if (!ops->set_msix)
+               return -ENODEV;
+
+       return ops->set_msix(dev, func_no, interrupts);
+}
+
+int dm_pci_ep_get_msix(struct udevice *dev, u8 func_no)
+{
+       struct dm_pci_ep_ops *ops = pci_ep_get_ops(dev);
+       int interrupt;
+
+       if (!ops->get_msix)
+               return -ENODEV;
+
+       interrupt = ops->get_msix(dev, func_no);
Quoted 9 lines

drivers/pci_endpoint/pci_ep-uclass.c line 154

Quoted 5 lines
drivers/pci_endpoint/pci_ep-uclass.c new file +5 -0
+
+       if (interrupt < 0)
+               return 0;
+
+       return interrupt + 1;

It's odd that your uclass function returns a different value from your driver function. I think that will be confusing. Is it necessary?

As you can understand from the code, 0 means 1 interrupt. basically the driver functions just return the msix field in the PCI registers, The translation to real number is done by the uclass wrapper.

Quoted 19 lines
Quoted 19 lines
drivers/pci_endpoint/pci_ep-uclass.c new file +18 -0
+}
+
+int dm_pci_ep_raise_irq(struct udevice *dev, u8 func_no,
+                       enum pci_ep_irq_type type, u16 interrupt_num)
+{
+       struct dm_pci_ep_ops *ops = pci_ep_get_ops(dev);
+
+       if (!ops->raise_irq)
+               return -ENODEV;
+
+       return ops->raise_irq(dev, func_no, type, interrupt_num);
+}
+
+int dm_pci_ep_start(struct udevice *dev)
+{
+       struct dm_pci_ep_ops *ops = pci_ep_get_ops(dev);
+
+       if (!ops->start)
Quoted 9 lines

drivers/pci_endpoint/pci_ep-uclass.c line 178

Quoted 6 lines
drivers/pci_endpoint/pci_ep-uclass.c new file +6 -0
+               return -ENODEV;
+
+       return ops->start(dev);
+}
+
+void dm_pci_ep_stop(struct udevice *dev)

Needs a return value.

ok

Quoted 113 lines
Quoted 47 lines
drivers/pci_endpoint/pci_ep-uclass.c new file +14 -0
+{
+       struct dm_pci_ep_ops *ops = pci_ep_get_ops(dev);
+
+       if (!ops->stop)
+               return;
+
+       ops->stop(dev);
+}
+
+UCLASS_DRIVER(pci_ep) = {
+       .id             = UCLASS_PCI_EP,
+       .name           = "pci_ep",
+       .flags          = DM_UC_FLAG_SEQ_ALIAS,
+};
include/dm/uclass-id.h +1 -0
index 86e59781b0..d29f7d5a34 100644
@@ -67,6 +67,7 @@ enum uclass_id {
        UCLASS_PANEL_BACKLIGHT, /* Backlight controller for panel */
        UCLASS_PCH,             /* x86 platform controller hub */
        UCLASS_PCI,             /* PCI bus */
+       UCLASS_PCI_EP,          /* PCI endpoint device */
        UCLASS_PCI_GENERIC,     /* Generic PCI bus device */
        UCLASS_PHY,             /* Physical Layer (PHY) device */
        UCLASS_PINCONFIG,       /* Pin configuration node device */
include/pci_ep.h new file +14 -0
new file mode 100644
index 0000000000..c98453ccfa
@@ -0,0 +1,375 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Adapted from Linux kernel driver
+ * Copyright (C) 2017 Texas Instruments
+ * Author: Kishon Vijay Abraham I <[email protected]>
+ *
+ * (C) Copyright 2019
+ * Ramon Fried <[email protected]>
+ */
+
+#ifndef _PCI_EP_H
+#define _PCI_EP_H
+
+#include <pci.h>

include/pci_ep.h line 24

Quoted 10 lines
include/pci_ep.h new file +10 -0
+
+/**
+ * enum pci_interrupt_pin - PCI INTx interrupt values
+ * @PCI_INTERRUPT_UNKNOWN: Unknown or unassigned interrupt
+ * @PCI_INTERRUPT_INTA: PCI INTA pin
+ * @PCI_INTERRUPT_INTB: PCI INTB pin
+ * @PCI_INTERRUPT_INTC: PCI INTC pin
+ * @PCI_INTERRUPT_INTD: PCI INTD pin
+ *
+ * Corresponds to values for legacy PCI INTx interrupts, as can be

found in the

Quoted 18 lines
include/pci_ep.h new file +18 -0
+ * PCI_INTERRUPT_PIN register.
+ */
+enum pci_interrupt_pin {
+       PCI_INTERRUPT_UNKNOWN,
+       PCI_INTERRUPT_INTA,
+       PCI_INTERRUPT_INTB,
+       PCI_INTERRUPT_INTC,
+       PCI_INTERRUPT_INTD,
+};
+
+enum pci_barno {
+       BAR_0,
+       BAR_1,
+       BAR_2,
+       BAR_3,
+       BAR_4,
+       BAR_5,
+};

include/pci_ep.h line 52

Quoted 10 lines
include/pci_ep.h new file +10 -0
+
+enum pci_ep_irq_type {
+       PCI_EP_IRQ_UNKNOWN,
+       PCI_EP_IRQ_LEGACY,
+       PCI_EP_IRQ_MSI,
+       PCI_EP_IRQ_MSIX,
+};
+
+/**
+ * struct pci_bar - represents the BAR of EP device

What is a BAR and what is an EP? Please spell things out in comments, at least ones per file:

base-address register (BAR)

include/pci_ep.h line 54

Quoted 2 lines
include/pci_ep.h new file +2 -0
+ * @phys_addr: physical address that should be mapped to the BAR
+ * @size: the size of the address space present in BAR

Please add the other two also

Quoted 4 lines
include/pci_ep.h new file +4 -0
+ */
+struct pci_bar {
+       dma_addr_t      phys_addr;
+       size_t          size;

include/pci_ep.h

Quoted 10 lines
include/pci_ep.h new file +10 -0
+       enum pci_barno  barno;
+       int             flags;
+};
+
+/**
+ * struct pci_ep_header - represents standard configuration header
+ * @vendorid: identifies device manufacturer
+ * @deviceid: identifies a particular device
+ * @revid: specifies a device-specific revision identifier
+ * @progif_code: identifies a specific register-level programming

interface

include/pci_ep.h line 69

Quoted 1 line
include/pci_ep.h new file +1 -0
+ * @subclass_code: identifies more specifically the function of the
Quoted 9 lines

device

include/pci_ep.h line 70

Quoted 1 line
include/pci_ep.h new file +1 -0
+ * @baseclass_code: broadly classifies the type of function the device

performs

include/pci_ep.h line 71

Quoted 1 line
include/pci_ep.h new file +1 -0
+ * @cache_line_size: specifies the system cacheline size in units of

DWORDs

What is a DWORD? I think i is 32-bits, so just say that, since WORD is a confusing term on a non-16-bit machine.

hehe, I presume it's just a copy-paste from the PCI spec. I'll check, if so, I'll prefer to keep the original naming.

Quoted 17 lines
Quoted 6 lines
include/pci_ep.h new file +5 -0
+ * @subsys_vendor_id: vendor of the add-in card or subsystem
+ * @subsys_id: id specific to vendor
+ * @interrupt_pin: interrupt pin the device (or device function) uses
+ */
+struct pci_ep_header {

include/pci_ep.h line 86

Quoted 10 lines
include/pci_ep.h new file +10 -0
+       u16     vendorid;
+       u16     deviceid;
+       u8      revid;
+       u8      progif_code;
+       u8      subclass_code;
+       u8      baseclass_code;
+       u8      cache_line_size;
+       u16     subsys_vendor_id;
+       u16     subsys_id;
+       enum pci_interrupt_pin interrupt_pin;
Quoted 3 lines

Shouldn't that be a u16 or something? The enum does not have a defined size, I think.

well, there can be only 4 different interrupt pins, so it doesn't matter as long as the rage
is checked.

Quoted 14 lines

include/pci_ep.h line 95

Quoted 10 lines
include/pci_ep.h new file +9 -0
+};
+
+/* PCI endpoint operations */
+struct dm_pci_ep_ops {
+       /**
+        * write_header() - Write a PCI configuration space header
+        *
+        * @dev:        device to write to
+        * @func_num:   Function to fill

Is this the function number within the device? If so, how about 'Function number (within device) to write to'? Similarly below.

OK, I concur.

Quoted 102 lines
Quoted 8 lines
include/pci_ep.h new file +7 -0
+        * @hdr:        header to write
+        * @return 0 if OK, -ve on error
+        */
+       int     (*write_header)(struct udevice *dev, u8 func_num,
+                               struct pci_ep_header *hdr);
+       /**
+        * read_header() - Read a PCI configuration space header

include/pci_ep.h line 112

Quoted 10 lines
include/pci_ep.h new file +10 -0
+        *
+        * @dev:        device to write to
+        * @func_num:   Function to fill
+        * @hdr:        header to read to
+        * @return 0 if OK, -ve on error
+        */
+       int     (*read_header)(struct udevice *dev, u8 func_num,
+                              struct pci_ep_header *hdr);
+       /**
+        * set_bar() - set BAR (Base Address Register)

Good to expand BAR as you have here

Quoted 45 lines
include/pci_ep.h new file +45 -0
+        *
+        * @dev:        device to set
+        * @func_num:   Function to set
+        * @bar:        bar data
+        * @return 0 if OK, -ve on error
+        */
+       int     (*set_bar)(struct udevice *dev, u8 func_num,
+                          struct pci_bar *bar);
+       /**
+        * clear_bar() - clear BAR (Base Address Register)
+        *
+        * @dev:        device to clear
+        * @func_num:   Function to clear
+        * @bar:        bar number
+        */
+       void    (*clear_bar)(struct udevice *dev, u8 func_num,
+                            enum pci_barno bar);
+       /**
+        * map_addr() - map CPU address to PCI address
+        *
+        * outband region is used in order to generate PCI read/write
+        * transaction from local memory/write.
+        *
+        * @dev:        device to set
+        * @func_num:   Function to set
+        * @addr:       local physical address base
+        * @pci_addr:   pci address to translate to
+        * @size:       region size
+        * @return 0 if OK, -ve on error
+        */
+       int     (*map_addr)(struct udevice *dev, u8 func_num,
+                           phys_addr_t addr, u64 pci_addr, size_t size);
+       /**
+        * unmap_addr() - unmap CPU address to PCI address
+        *
+        * unmap previously mapped region.
+        *
+        * @dev:        device to set
+        * @func_num:   Function to set
+        * @addr:       local physical address base
+        */
+       void    (*unmap_addr)(struct udevice *dev, u8 func_num,
+                             phys_addr_t addr);
+       /**
+        * set_msi() - set msi capability property

include/pci_ep.h

Quoted 10 lines
include/pci_ep.h new file +10 -0
+        *
+        * set the number of required MSI vectors the device
+        * needs for operation.
+        *
+        * @dev:        device to set
+        * @func_num:   Function to set
+        * @interrupts: required interrupts count
+        * @return 0 if OK, -ve on error
+        */
+       int     (*set_msi)(struct udevice *dev, u8 func_num, u8

interrupts);

include/pci_ep.h

Quoted 3 lines
include/pci_ep.h new file +3 -0
+
+       /**
+        * get_msi() - get the number of MSI interrupts allocated by the

host.

include/pci_ep.h

Quoted 7 lines
include/pci_ep.h new file +7 -0
+        *
+        * Read the Multiple Message Enable bitfield from
+        * Message control register.
+        *
+        * @dev:        device to use
+        * @func_num:   Function to use
+        * @return msi count if OK, -EINVAL if msi were not enabled at

host.

include/pci_ep.h line 189

Quoted 12 lines
include/pci_ep.h new file +12 -0
+        */
+       int     (*get_msi)(struct udevice *dev, u8 func_num);
+
+       /**
+        * set_msix() - set msix capability property
+        *
+        * set the number of required MSIx vectors the device
+        * needs for operation.
+        *
+        * @dev:        device to set
+        * @func_num:   Function to set
+        * @interrupts: required interrupts count
Quoted 2 lines

This is too vague, please expand it.

You're referring to the set_msix() or the whole section, can you elaborate ?

Quoted 30 lines

include/pci_ep.h

Quoted 3 lines
include/pci_ep.h new file +3 -0
+        * @return 0 if OK, -ve on error
+        */
+       int     (*set_msix)(struct udevice *dev, u8 func_num, u16

interrupts);

include/pci_ep.h

Quoted 3 lines
include/pci_ep.h new file +3 -0
+
+       /**
+        * get_msix() - get the number of MSIx interrupts allocated by

the host.

include/pci_ep.h

Quoted 7 lines
include/pci_ep.h new file +7 -0
+        *
+        * Read the Multiple Message Enable bitfield from
+        * Message control register.
+        *
+        * @dev:        device to use
+        * @func_num:   Function to use
+        * @return msi count if OK, -EINVAL if msi were not enabled at

host.

Quoted 4 lines
include/pci_ep.h new file +4 -0
+        */
+       int     (*get_msix)(struct udevice *dev, u8 func_num);
+
+       /**

include/pci_ep.h

Quoted 10 lines
include/pci_ep.h new file +10 -0
+        * raise_irq() - raise a legacy, MSI or MSI-X interrupt
+        *
+        * @dev:        device to set
+        * @func_num:   Function to set
+        * @type:       type of irq to send
+        * @interrupt_num: interrupt vector to use
+        * @return 0 if OK, -ve on error
+        */
+       int     (*raise_irq)(struct udevice *dev, u8 func_num,
+                            enum pci_ep_irq_type type, u16
Quoted 7 lines

interrupt_num);

include/pci_ep.h line 219

Quoted 3 lines
include/pci_ep.h new file +3 -0
+       /**
+        * start() - start the PCI link
+        *

Needs more docs. What is the effect of this?

OK, I'll elaborate more.

Quoted 172 lines

include/pci_ep.h line 226

Quoted 7 lines
include/pci_ep.h new file +7 -0
+        * @dev:        device to set
+        * @return 0 if OK, -ve on error
+        */
+       int     (*start)(struct udevice *dev);
+
+       /**
+        * stop() - stop the PCI link

Needs more docs. What is the effect of this?

include/pci_ep.h

Quoted 7 lines
include/pci_ep.h new file +7 -0
+        *
+        * @dev:        device to set
+        */
+       void    (*stop)(struct udevice *dev);
+};
+
+#define pci_ep_get_ops(dev)    ((struct dm_pci_ep_ops

*)(dev)->driver->ops)

Quoted 21 lines
include/pci_ep.h new file +21 -0
+
+/**
+ * pci_ep_write_header() - Write a PCI configuration space header
+ *
+ * @dev:       device to write to
+ * @func_num:  Function to fill
+ * @hdr:       header to write
+ * @return 0 if OK, -ve on error
+ */
+int pci_ep_write_header(struct udevice *dev, u8 func_num,
+                       struct pci_ep_header *hdr);
+
+/**
+ * dm_pci_ep_read_header() - Read a PCI configuration space header
+ *
+ * @dev:       device to write to
+ * @func_num:  Function to fill
+ * @hdr:       header to read to
+ * @return 0 if OK, -ve on error
+ */
+int pci_ep_read_header(struct udevice *dev, u8 func_num,

include/pci_ep.h

Quoted 10 lines
include/pci_ep.h new file +10 -0
+                      struct pci_ep_header *hdr);
+/**
+ * pci_ep_set_bar() - set BAR (Base Address Register)
+ *
+ * @dev:       device to set
+ * @func_num:  Function to set
+ * @bar:       bar data
+ * @return 0 if OK, -ve on error
+ */
+int pci_ep_set_bar(struct udevice *dev, u8 func_num, struct pci_bar

*bar);

include/pci_ep.h

Quoted 9 lines
include/pci_ep.h new file +9 -0
+
+/**
+ * pci_ep_clear_bar() - clear BAR (Base Address Register)
+ *
+ * @dev:       device to clear
+ * @func_num:  Function to clear
+ * @bar:       bar number
+ */
+void pci_ep_clear_bar(struct udevice *dev, u8 func_num, enum pci_barno

bar);

Quoted 15 lines
include/pci_ep.h new file +15 -0
+/**
+ * pci_ep_map_addr() - map CPU address to PCI address
+ *
+ * outband region is used in order to generate PCI read/write
+ * transaction from local memory/write.
+ *
+ * @dev:       device to set
+ * @func_num:  Function to set
+ * @addr:      local physical address base
+ * @pci_addr:  pci address to translate to
+ * @size:      region size
+ * @return 0 if OK, -ve on error
+ */
+int pci_ep_map_addr(struct udevice *dev, u8 func_num, phys_addr_t addr,
+                   u64 pci_addr, size_t size);

include/pci_ep.h

Quoted 10 lines
include/pci_ep.h new file +10 -0
+/**
+ * pci_ep_unmap_addr() - unmap CPU address to PCI address
+ *
+ * unmap previously mapped region.
+ *
+ * @dev:       device to set
+ * @func_num:  Function to set
+ * @addr:      local physical address base
+ */
+void pci_ep_unmap_addr(struct udevice *dev, u8 func_num, phys_addr_t

addr);

Quoted 5 lines
include/pci_ep.h new file +5 -0
+/**
+ * pci_ep_set_msi() - set msi capability property
+ *
+ * set the number of required MSI vectors the device
+ * needs for operation.

include/pci_ep.h

Quoted 10 lines
include/pci_ep.h new file +10 -0
+ *
+ * @dev:       device to set
+ * @func_num:  Function to set
+ * @interrupts:        required interrupts count
+ * @return 0 if OK, -ve on error
+ */
+int pci_ep_set_msi(struct udevice *dev, u8 func_num, u8 interrupts);
+
+/**
+ * pci_ep_get_msi() - get the number of MSI interrupts allocated by the

host.

Quoted 15 lines
include/pci_ep.h new file +15 -0
+ *
+ * Read the Multiple Message Enable bitfield from
+ * Message control register.
+ *
+ * @dev:       device to use
+ * @func_num:  Function to use
+ * @return msi count if OK, -EINVAL if msi were not enabled at host.
+ */
+int pci_ep_get_msi(struct udevice *dev, u8 func_num);
+
+/**
+ * pci_ep_set_msix() - set msi capability property
+ *
+ * set the number of required MSIx vectors the device
+ * needs for operation.

include/pci_ep.h

Quoted 10 lines
include/pci_ep.h new file +10 -0
+ *
+ * @dev:       device to set
+ * @func_num:  Function to set
+ * @interrupts:        required interrupts count
+ * @return 0 if OK, -ve on error
+ */
+int pci_ep_set_msix(struct udevice *dev, u8 func_num, u16 interrupts);
+
+/**
+ * pci_ep_get_msix() - get the number of MSIx interrupts allocated by

the host.

Quoted 38 lines
include/pci_ep.h new file +37 -1
+ *
+ * Read the Multiple Message Enable bitfield from
+ * Message control register.
+ *
+ * @dev:       device to use
+ * @func_num:  Function to use
+ * @return msi count if OK, -EINVAL if msi were not enabled at host.
+ */
+int pci_ep_get_msix(struct udevice *dev, u8 func_num);
+
+/**
+ * pci_ep_raise_irq() - raise a legacy, MSI or MSI-X interrupt
+ *
+ * @dev:       device to set
+ * @func_num:  Function to set
+ * @type:      type of irq to send
+ * @interrupt_num: interrupt vector to use
+ * @return 0 if OK, -ve on error
+ */
+int pci_ep_raise_irq(struct udevice *dev, u8 func_num,
+                    enum pci_ep_irq_type type, u16 interrupt_num);
+/**
+ * pci_ep_start() - start the PCI link
+ *
+ * @dev:       device to set
+ * @return 0 if OK, -ve on error
+ */
+int pci_ep_start(struct udevice *dev);
+
+/**
+ * pci_ep_stop() - stop the PCI link
+ *
+ * @dev:       device to set
+ */
+void pci_ep_stop(struct udevice *dev);
+
+#endif
--
Quoted 2 lines

2.21.0

Regards,
Simon

#4 lore raw

PATCH 1/3 drivers: pci_ep: Introduce UCLASS_PCI_EP uclass

HI Ramon,

Quoted 40 lines · Ramon Fried

Hi Simon,
Thanks for the review.
please see inline, I have few questions/suggestions regarding your notes.

Thanks,
Ramon.

Quoted 30 lines · Simon Glass

Hi Ramon,

Quoted 25 lines · Ramon Fried

Introduce new UCLASS_PCI_EP class for handling PCI endpoint devices, allowing to set various attributes of the PCI endpoint device, such as:
* configuration space header
* BAR definitions
* outband memory mapping
* start/stop PCI link

---

 drivers/Kconfig                      |   2 +
 drivers/Makefile                     |   1 +
 drivers/pci_endpoint/Kconfig         |  16 ++
 drivers/pci_endpoint/Makefile        |   6 +
 drivers/pci_endpoint/pci_ep-uclass.c | 192 ++++++++++++++
 include/dm/uclass-id.h               |   1 +
 include/pci_ep.h                     | 375 +++++++++++++++++++++++++++
 7 files changed, 593 insertions(+)
 create mode 100644 drivers/pci_endpoint/Kconfig
 create mode 100644 drivers/pci_endpoint/Makefile
 create mode 100644 drivers/pci_endpoint/pci_ep-uclass.c
 create mode 100644 include/pci_ep.h

[..]

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

+int dm_pci_ep_set_bar(struct udevice *dev, u8 func_no,

Please use uint for func_no. There is no need to limit it to 8 bits, and this may not be efficient for non-8-bit machines. Please fix globally.

I tried to keep the API as similar as it can to the Linux API.
I can change it, no problem, but IMHO I think it's better to keep it similar.

Hmm, why?

Quoted 6 lines
Quoted 3 lines

Perhaps you should declare a 'mask' variable? In any case, it is confusing for the same variable to have two different meanings, and it does not save code at run-time.

as before, this is practically a copy-paste from Linux, I can change it, but I think it's clearer if the code looks the same as in Linux, might be easier the port patches like that.

It's a minor thing so you can keep the linux code if you like.

[..]

Quoted 18 lines
Quoted 9 lines
Quoted 9 lines
drivers/pci_endpoint/pci_ep-uclass.c new file +9 -0
+int dm_pci_ep_get_msix(struct udevice *dev, u8 func_no)
+{
+       struct dm_pci_ep_ops *ops = pci_ep_get_ops(dev);
+       int interrupt;
+
+       if (!ops->get_msix)
+               return -ENODEV;
+
+       interrupt = ops->get_msix(dev, func_no);
Quoted 8 lines

drivers/pci_endpoint/pci_ep-uclass.c line 154

Quoted 5 lines
drivers/pci_endpoint/pci_ep-uclass.c new file +5 -0
+
+       if (interrupt < 0)
+               return 0;
+
+       return interrupt + 1;

It's odd that your uclass function returns a different value from your driver function. I think that will be confusing. Is it necessary?

Quoted 2 lines

As you can understand from the code, 0 means 1 interrupt. basically the driver functions just return the msix field in the PCI registers, The translation to real number is done by the uclass wrapper.

OK, but why? Why not use the same return value for the drive methods and the uclass? If you are using a different API, then please rename one of the functions.

[..]

Quoted 4 lines
Quoted 2 lines

What is a DWORD? I think i is 32-bits, so just say that, since WORD is a confusing term on a non-16-bit machine.

hehe, I presume it's just a copy-paste from the PCI spec. I'll check, if so, I'll prefer to keep the original naming.

OK, then how about adding the length as well, since DWORD is pretty obscure these days.

Quoted 20 lines
Quoted 20 lines
Quoted 19 lines

+ * @subsys_vendor_id: vendor of the add-in card or subsystem
+ * @subsys_id: id specific to vendor
+ * @interrupt_pin: interrupt pin the device (or device function) uses
+ */

include/pci_ep.h new file +11 -0
+struct pci_ep_header {
+       u16     vendorid;
+       u16     deviceid;
+       u8      revid;
+       u8      progif_code;
+       u8      subclass_code;
+       u8      baseclass_code;
+       u8      cache_line_size;
+       u16     subsys_vendor_id;
+       u16     subsys_id;
+       enum pci_interrupt_pin interrupt_pin;
Quoted 6 lines
Quoted 2 lines

Shouldn't that be a u16 or something? The enum does not have a defined size, I think.

well, there can be only 4 different interrupt pins, so it doesn't matter as long as the rage is checked.

My concern is that if this is a hardware-mapped register, then the enum could be any size, and may not map over the correct bits.

If this is not a hardware-mapped register, then OK.

[..]

Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
include/pci_ep.h new file +9 -0
+       /**
+        * set_msix() - set msix capability property
+        *
+        * set the number of required MSIx vectors the device
+        * needs for operation.
+        *
+        * @dev:        device to set
+        * @func_num:   Function to set
+        * @interrupts: required interrupts count
Quoted 4 lines
Quoted 1 line

This is too vague, please expand it.

You're referring to the set_msix() or the whole section, can you elaborate ?

I mean the interrupts line. Can you given example values perhaps? Does it mean 'number of interrupts required to be alllocated' or something like that?

[..]

Regards,
Simon

#5 lore raw

PATCH 1/3 drivers: pci_ep: Introduce UCLASS_PCI_EP uclass

Quoted 53 lines · Simon Glass

HI Ramon,

Quoted 34 lines · Ramon Fried

Hi Simon,
Thanks for the review.
please see inline, I have few questions/suggestions regarding your notes.

Thanks,
Ramon.

Quoted 24 lines · Simon Glass

Hi Ramon,

Quoted 20 lines · Ramon Fried

Introduce new UCLASS_PCI_EP class for handling PCI endpoint devices, allowing to set various attributes of the PCI endpoint device, such as:
* configuration space header
* BAR definitions
* outband memory mapping
* start/stop PCI link

---

 drivers/Kconfig                      |   2 +
 drivers/Makefile                     |   1 +
 drivers/pci_endpoint/Kconfig         |  16 ++
 drivers/pci_endpoint/Makefile        |   6 +
 drivers/pci_endpoint/pci_ep-uclass.c | 192 ++++++++++++++
 include/dm/uclass-id.h               |   1 +
 include/pci_ep.h                     | 375

+++++++++++++++++++++++++++

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
 7 files changed, 593 insertions(+)
 create mode 100644 drivers/pci_endpoint/Kconfig
 create mode 100644 drivers/pci_endpoint/Makefile
 create mode 100644 drivers/pci_endpoint/pci_ep-uclass.c
 create mode 100644 include/pci_ep.h

[..]

Quoted 6 lines
Quoted 5 lines
Quoted 1 line

+int dm_pci_ep_set_bar(struct udevice *dev, u8 func_no,

Please use uint for func_no. There is no need to limit it to 8 bits, and this may not be efficient for non-8-bit machines. Please fix globally.

Quoted 6 lines
Quoted 2 lines

I tried to keep the API as similar as it can to the Linux API. I can change it, no problem, but IMHO I think it's better to keep it

similar.

Hmm, why?

Nevermind, I already changed my mind :)

Quoted 31 lines
Quoted 6 lines
Quoted 4 lines

Perhaps you should declare a 'mask' variable? In any case, it is confusing for the same variable to have two different meanings, and it does not save code at run-time.

as before, this is practically a copy-paste from Linux, I can change it,

but I think it's clearer if the code looks the same as in Linux,

Quoted 1 line

might be easier the port patches like that.

It's a minor thing so you can keep the linux code if you like.

[..]

Quoted 18 lines
Quoted 17 lines
Quoted 4 lines
drivers/pci_endpoint/pci_ep-uclass.c new file +4 -0
+int dm_pci_ep_get_msix(struct udevice *dev, u8 func_no)
+{
+       struct dm_pci_ep_ops *ops = pci_ep_get_ops(dev);
+       int interrupt;

drivers/pci_endpoint/pci_ep-uclass.c line 154

Quoted 10 lines
drivers/pci_endpoint/pci_ep-uclass.c new file +10 -0
+
+       if (!ops->get_msix)
+               return -ENODEV;
+
+       interrupt = ops->get_msix(dev, func_no);
+
+       if (interrupt < 0)
+               return 0;
+
+       return interrupt + 1;

It's odd that your uclass function returns a different value from your driver function. I think that will be confusing. Is it necessary?

Quoted 8 lines
Quoted 1 line

As you can understand from the code, 0 means 1 interrupt. basically the

driver functions just return the msix field in the PCI registers,

Quoted 1 line

The translation to real number is done by the uclass wrapper.

OK, but why? Why not use the same return value for the drive methods and the uclass? If you are using a different API, then please rename one of the functions.

I'm here advocating for the Linux implementation, you're right, I'll change it.

Quoted 12 lines

[..]

Quoted 4 lines
Quoted 2 lines

What is a DWORD? I think i is 32-bits, so just say that, since WORD is a confusing term on a non-16-bit machine.

hehe, I presume it's just a copy-paste from the PCI spec. I'll check, if

so, I'll prefer to keep the original naming.

OK, then how about adding the length as well, since DWORD is pretty obscure these days.

OK, will do.

Quoted 25 lines
Quoted 21 lines
Quoted 21 lines
Quoted 20 lines

+ * @subsys_vendor_id: vendor of the add-in card or subsystem
+ * @subsys_id: id specific to vendor
+ * @interrupt_pin: interrupt pin the device (or device function) uses
+ */

include/pci_ep.h new file +11 -0
+struct pci_ep_header {
+       u16     vendorid;
+       u16     deviceid;
+       u8      revid;
+       u8      progif_code;
+       u8      subclass_code;
+       u8      baseclass_code;
+       u8      cache_line_size;
+       u16     subsys_vendor_id;
+       u16     subsys_id;
+       enum pci_interrupt_pin interrupt_pin;
Quoted 4 lines
Quoted 2 lines

Shouldn't that be a u16 or something? The enum does not have a defined size, I think.

well, there can be only 4 different interrupt pins, so it doesn't matter

Quoted 9 lines

as long as the rage

Quoted 2 lines

is checked.

My concern is that if this is a hardware-mapped register, then the enum could be any size, and may not map over the correct bits.

If this is not a hardware-mapped register, then OK.

Not mapped, phew.

Quoted 13 lines

[..]

Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
include/pci_ep.h new file +9 -0
+       /**
+        * set_msix() - set msix capability property
+        *
+        * set the number of required MSIx vectors the device
+        * needs for operation.
+        *
+        * @dev:        device to set
+        * @func_num:   Function to set
+        * @interrupts: required interrupts count
Quoted 9 lines
Quoted 4 lines
Quoted 1 line

This is too vague, please expand it.

You're referring to the set_msix() or the whole section, can you elaborate ?

I mean the interrupts line. Can you given example values perhaps? Does it mean 'number of interrupts required to be alllocated' or something like that?

It's basically a request from the endpoint, it might not be fully fulfilled by the host,
the endpoint needs to check later using get_msix() how many interrupts the host allocated for him.
I'll add documentation for that.

Quoted 6 lines

[..]

Regards,
Simon

#6 lore raw

PATCH 2/3 pci: pci.h: add missing maskbit

In reply to Ramon Fried

PCI_MSI_FLAGS_MASKBIT was missing from include file, add it.

---

 include/pci.h | 1 +
 1 file changed, 1 insertion(+)
include/pci.h +1 -0
index 5fb212cab1..508f7bca81 100644
@@ -405,6 +405,7 @@
 #define  PCI_MSI_FLAGS_QSIZE	0x70	/* Message queue size configured */
 #define  PCI_MSI_FLAGS_QMASK	0x0e	/* Maximum queue size available */
 #define  PCI_MSI_FLAGS_ENABLE	0x01	/* MSI feature enabled */
+#define  PCI_MSI_FLAGS_MASKBIT	0x0100	/* Per-vector masking capable */
 #define PCI_MSI_RFU		3	/* Rest of capability flags */
 #define PCI_MSI_ADDRESS_LO	4	/* Lower 32 bits */
 #define PCI_MSI_ADDRESS_HI	8	/* Upper 32 bits (if PCI_MSI_FLAGS_64BIT set) */
Signature · 1 line
2.21.0
#9 lore raw

PATCH 3/3 pci_ep: add Cadence PCIe endpoint driver

In reply to Ramon Fried

Add Cadence PCIe endpoint driver supporting configuration of header, bars and MSI for device.

---

 .../pci_endpoint/cdns,cdns-pcie-ep.txt        |  18 +
 drivers/pci_endpoint/Kconfig                  |   9 +
 drivers/pci_endpoint/Makefile                 |   1 +
 drivers/pci_endpoint/pcie-cadence-ep.c        | 177 ++++++++++
 drivers/pci_endpoint/pcie-cadence.h           | 309 ++++++++++++++++++
 5 files changed, 514 insertions(+)
 create mode 100644 doc/device-tree-bindings/pci_endpoint/cdns,cdns-pcie-ep.txt
 create mode 100644 drivers/pci_endpoint/pcie-cadence-ep.c
 create mode 100644 drivers/pci_endpoint/pcie-cadence.h
doc/device-tree-bindings/pci_endpoint/cdns,cdns-pcie-ep.txt new file +18 -0
new file mode 100644
index 0000000000..7705430559
@@ -0,0 +1,18 @@
+* Cadence PCIe endpoint controller
+
+Required properties:
+- compatible: Should contain "cdns,cdns-pcie-ep" to identify the IP used.
+- reg: Should contain the controller register base address.
+
+Optional properties:
+- max-functions: Maximum number of functions that can be configured (default 1).
+- cdns,max-outbound-regions: Set to maximum number of outbound regions (default 8)
+
+Example:
+
+pcie_ep at fc000000 {
+	compatible = "cdns,cdns-pcie-ep";
+	reg = <0x0 0xfc000000 0x0 0x01000000>;
+	cdns,max-outbound-regions = <16>;
+	max-functions = /bits/ 8 <8>;
+};
drivers/pci_endpoint/Kconfig +9 -0
index 2c0a399a08..d4cf4227df 100644
@@ -13,4 +13,13 @@ config PCI_ENDPOINT
 	   endpoint. This should be enabled if the platform has a PCI
 	   controller that can operate in endpoint mode.
 
+config PCIE_CADENCE_EP
+	bool "Cadence PCIe endpoint controller"
+	depends on OF
+	depends on PCI_ENDPOINT
+	help
+	  Say Y here if you want to support the Cadence PCIe  controller in
+	  endpoint mode. This PCIe controller may be embedded into many
+	  different vendors SoCs.
+
 endmenu
drivers/pci_endpoint/Makefile +1 -0
index 80a1066925..0a849deb19 100644
@@ -4,3 +4,4 @@
 # Ramon Fried <[email protected]>
 
 obj-y += pci_ep-uclass.o
+obj-$(CONFIG_PCIE_CADENCE_EP) += pcie-cadence-ep.o
drivers/pci_endpoint/pcie-cadence-ep.c new file +177 -0

177 lines not on this page · raw message

drivers/pci_endpoint/pcie-cadence.h new file +309 -0

309 lines not on this page · raw message

Signature · 1 line
2.21.0
#10 lore raw

PATCH 3/3 pci_ep: add Cadence PCIe endpoint driver

Quoted 7 lines · Ramon Fried

Add Cadence PCIe endpoint driver supporting configuration of header, bars and MSI for device.

---

Quoted 9 lines
 .../pci_endpoint/cdns,cdns-pcie-ep.txt        |  18 +
 drivers/pci_endpoint/Kconfig                  |   9 +
 drivers/pci_endpoint/Makefile                 |   1 +
 drivers/pci_endpoint/pcie-cadence-ep.c        | 177 ++++++++++
 drivers/pci_endpoint/pcie-cadence.h           | 309 ++++++++++++++++++
 5 files changed, 514 insertions(+)
 create mode 100644 doc/device-tree-bindings/pci_endpoint/cdns,cdns-pcie-ep.txt
 create mode 100644 drivers/pci_endpoint/pcie-cadence-ep.c
 create mode 100644 drivers/pci_endpoint/pcie-cadence.h

Looks like tidy code. I'll await the updated uclass patch.

- Simon