From patchwork Sat Sep 18 02:24:37 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chenbo Xia X-Patchwork-Id: 99251 X-Patchwork-Delegate: david.marchand@redhat.com Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 41FAEA0C47; Sat, 18 Sep 2021 04:39:11 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id F2EA1410EB; Sat, 18 Sep 2021 04:39:10 +0200 (CEST) Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by mails.dpdk.org (Postfix) with ESMTP id 2AADE4003D for ; Sat, 18 Sep 2021 04:39:08 +0200 (CEST) X-IronPort-AV: E=McAfee;i="6200,9189,10110"; a="222563589" X-IronPort-AV: E=Sophos;i="5.85,303,1624345200"; d="scan'208";a="222563589" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Sep 2021 19:39:08 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.85,303,1624345200"; d="scan'208";a="546751614" Received: from npg-dpdk-virtio-xiachenbo-nw.sh.intel.com ([10.67.119.53]) by FMSMGA003.fm.intel.com with ESMTP; 17 Sep 2021 19:39:07 -0700 From: Chenbo Xia To: dev@dpdk.org, david.marchand@redhat.com Cc: Ray Kinsella Date: Sat, 18 Sep 2021 10:24:37 +0800 Message-Id: <20210918022443.12719-2-chenbo.xia@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210918022443.12719-1-chenbo.xia@intel.com> References: <20210910022402.26620-1-chenbo.xia@intel.com> <20210918022443.12719-1-chenbo.xia@intel.com> Subject: [dpdk-dev] [PATCH v2 1/7] bus/pci: add new memory resource access APIs X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" Some applications wants to access PCI memory resource. Currently applications use struct rte_pci_device to access it. Since the structure will be made internal later, this patch adds two APIs for memory resource access. Signed-off-by: Chenbo Xia Acked-by: Ray Kinsella --- doc/guides/rel_notes/release_21_11.rst | 6 ++ drivers/bus/pci/pci_common.c | 78 ++++++++++++++++++++++++++ drivers/bus/pci/rte_bus_pci.h | 36 ++++++++++++ drivers/bus/pci/version.map | 4 ++ 4 files changed, 124 insertions(+) diff --git a/doc/guides/rel_notes/release_21_11.rst b/doc/guides/rel_notes/release_21_11.rst index 1d56fa9bf2..ce3f554e10 100644 --- a/doc/guides/rel_notes/release_21_11.rst +++ b/doc/guides/rel_notes/release_21_11.rst @@ -55,6 +55,12 @@ New Features Also, make sure to start the actual text at the margin. ======================================================= +* **Added new memory resource read/write APIs in PCI bus.** + + Added new memory resource read/write APIs ``rte_pci_mem_rd32`` and + ``rte_pci_mem_wr32`` for applications to read/write PCI memory + resource. + * **Enabled new devargs parser.** * Enabled devargs syntax diff --git a/drivers/bus/pci/pci_common.c b/drivers/bus/pci/pci_common.c index 3406e03b29..5bc7c8e2c7 100644 --- a/drivers/bus/pci/pci_common.c +++ b/drivers/bus/pci/pci_common.c @@ -25,6 +25,7 @@ #include #include #include +#include #include "private.h" @@ -777,6 +778,83 @@ rte_pci_set_bus_master(struct rte_pci_device *dev, bool enable) return 0; } +static void * +get_pci_mem_addr(const char *name, uint16_t idx, uint64_t offset) +{ + struct rte_pci_device *dev = NULL; + struct rte_pci_addr addr = {0}; + struct rte_mem_resource *res = NULL; + bool found = false; + + if (rte_pci_addr_parse(name, &addr)) { + RTE_LOG(ERR, EAL, "Wrong name format of PCI device (%s)", name); + return NULL; + } + + FOREACH_DEVICE_ON_PCIBUS(dev) { + if (rte_pci_addr_cmp(&dev->addr, &addr)) { + continue; + } else { + found = true; + break; + } + } + + if (!found) { + RTE_LOG(ERR, EAL, "Can not find the device (%s)", name); + return NULL; + } + + res = &dev->mem_resource[idx]; + if (idx >= PCI_MAX_RESOURCE || res->len == 0 || res->addr == NULL) { + RTE_LOG(ERR, EAL, "Invalid index of a mapped memory resourse"); + return NULL; + } + + if (offset + 4 > res->len) { + RTE_LOG(ERR, EAL, "Invalid offset of a memory resourse"); + return NULL; + } + + return (void *)((char *)res->addr + offset); +} + +int +rte_pci_mem_rd32(const char *name, uint16_t idx, uint32_t *data, uint64_t offset) +{ + void *reg_addr = NULL; + + if (data == NULL) { + RTE_LOG(ERR, EAL, "NULL data buffer for PCI memory access"); + return -EINVAL; + } + + reg_addr = get_pci_mem_addr(name, idx, offset); + if (reg_addr == NULL) + return -EINVAL; + + *data = rte_read32(reg_addr); + return 0; +} + +int +rte_pci_mem_wr32(const char *name, uint16_t idx, const uint32_t *data, uint64_t offset) +{ + void *reg_addr = NULL; + + if (data == NULL) { + RTE_LOG(ERR, EAL, "NULL data buffer for PCI memory access"); + return -EINVAL; + } + + reg_addr = get_pci_mem_addr(name, idx, offset); + if (reg_addr == NULL) + return -EINVAL; + + rte_write32(*data, reg_addr); + return 0; +} + struct rte_pci_bus rte_pci_bus = { .bus = { .scan = rte_pci_scan, diff --git a/drivers/bus/pci/rte_bus_pci.h b/drivers/bus/pci/rte_bus_pci.h index 583470e831..21d9dd4289 100644 --- a/drivers/bus/pci/rte_bus_pci.h +++ b/drivers/bus/pci/rte_bus_pci.h @@ -392,6 +392,42 @@ void rte_pci_ioport_read(struct rte_pci_ioport *p, void rte_pci_ioport_write(struct rte_pci_ioport *p, const void *data, size_t len, off_t offset); +/** + * Read 4 bytes from PCI memory resource. + * + * @param name + * PCI device name (e.g., 0000:18:00.0). + * @param idx + * Memory resource index. + * @param data + * Data buffer where the bytes should be read into. + * @param offset + * The offset into the PCI memory resource. + * @return + * 0 on success, negative value on error. + */ +__rte_experimental +int +rte_pci_mem_rd32(const char *name, uint16_t idx, uint32_t *data, uint64_t offset); + +/** + * Write 4 bytes to PCI memory resource. + * + * @param name + * PCI device name (e.g., 0000:18:00.0). + * @param idx + * Memory resource index. + * @param data + * Buffer of data that should be written to PCI memory. + * @param offset + * The offset into the PCI memory resource. + * @return + * 0 on success, negative value on error. + */ +__rte_experimental +int +rte_pci_mem_wr32(const char *name, uint16_t idx, const uint32_t *data, uint64_t offset); + #ifdef __cplusplus } #endif diff --git a/drivers/bus/pci/version.map b/drivers/bus/pci/version.map index aa56439c2b..01ec836559 100644 --- a/drivers/bus/pci/version.map +++ b/drivers/bus/pci/version.map @@ -24,4 +24,8 @@ EXPERIMENTAL { # added in 21.08 rte_pci_set_bus_master; + + # added in 21.11 + rte_pci_mem_rd32; + rte_pci_mem_wr32; };