From patchwork Fri Jul 27 14:47:42 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Reza Arbab X-Patchwork-Id: 950223 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [203.11.71.2]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 41cX153VN9z9ryl for ; Sat, 28 Jul 2018 00:49:29 +1000 (AEST) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=linux.ibm.com Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 41cX151XHXzF0pD for ; Sat, 28 Jul 2018 00:49:29 +1000 (AEST) Authentication-Results: lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=linux.ibm.com X-Original-To: skiboot@lists.ozlabs.org Delivered-To: skiboot@lists.ozlabs.org Authentication-Results: lists.ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=linux.ibm.com (client-ip=148.163.156.1; helo=mx0a-001b2d01.pphosted.com; envelope-from=arbab@linux.ibm.com; receiver=) Authentication-Results: lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=linux.ibm.com Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 41cWzC0jRszF0gv for ; Sat, 28 Jul 2018 00:47:50 +1000 (AEST) Received: from pps.filterd (m0098396.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.22/8.16.0.22) with SMTP id w6REiRJQ040048 for ; Fri, 27 Jul 2018 10:47:48 -0400 Received: from e17.ny.us.ibm.com (e17.ny.us.ibm.com [129.33.205.207]) by mx0a-001b2d01.pphosted.com with ESMTP id 2kg41dv0rs-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Fri, 27 Jul 2018 10:47:47 -0400 Received: from localhost by e17.ny.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Fri, 27 Jul 2018 10:47:46 -0400 Received: from b01cxnp22033.gho.pok.ibm.com (9.57.198.23) by e17.ny.us.ibm.com (146.89.104.204) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; (version=TLSv1/SSLv3 cipher=AES256-GCM-SHA384 bits=256/256) Fri, 27 Jul 2018 10:47:45 -0400 Received: from b01ledav003.gho.pok.ibm.com (b01ledav003.gho.pok.ibm.com [9.57.199.108]) by b01cxnp22033.gho.pok.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id w6RElijv9371994 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Fri, 27 Jul 2018 14:47:44 GMT Received: from b01ledav003.gho.pok.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 5F7A8B2066; Fri, 27 Jul 2018 10:47:23 -0400 (EDT) Received: from b01ledav003.gho.pok.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 3A078B2064; Fri, 27 Jul 2018 10:47:23 -0400 (EDT) Received: from arbab-laptop.localdomain (unknown [9.53.92.213]) by b01ledav003.gho.pok.ibm.com (Postfix) with ESMTP; Fri, 27 Jul 2018 10:47:23 -0400 (EDT) Received: by arbab-laptop.localdomain (Postfix, from userid 152845) id C035D46061F; Fri, 27 Jul 2018 09:47:42 -0500 (CDT) From: Reza Arbab To: skiboot@lists.ozlabs.org Date: Fri, 27 Jul 2018 09:47:42 -0500 X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1532702862-22751-1-git-send-email-arbab@linux.ibm.com> References: <1532702862-22751-1-git-send-email-arbab@linux.ibm.com> X-TM-AS-GCONF: 00 x-cbid: 18072714-0040-0000-0000-00000454B460 X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00009438; HX=3.00000241; KW=3.00000007; PH=3.00000004; SC=3.00000266; SDB=6.01066904; UDB=6.00548184; IPR=6.00844770; MB=3.00022354; MTD=3.00000008; XFM=3.00000015; UTC=2018-07-27 14:47:46 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 18072714-0041-0000-0000-0000085AEBD1 Message-Id: <1532702862-22751-7-git-send-email-arbab@linux.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:, , definitions=2018-07-27_06:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=1 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1806210000 definitions=main-1807270152 Subject: [Skiboot] [PATCH 6/6] npu2: Add support for relaxed-ordering mode X-BeenThere: skiboot@lists.ozlabs.org X-Mailman-Version: 2.1.27 Precedence: list List-Id: Mailing list for skiboot development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Alistair Popple MIME-Version: 1.0 Errors-To: skiboot-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Skiboot" Some device drivers support out of order access to GPU memory. This does not affect the CPU view of memory but it does affect the GPU view of memory. It should only be enabled once the GPU driver has requested it to be enabled. Current hardware only allows it to be enabled per PCIe root port, therefore all devices on a given PCIe root port must be explicitly enabled for relaxed-ordering prior to it actually being enabled. Signed-off-by: Alistair Popple Signed-off-by: Reza Arbab Reviewed-by: Alistair Popple --- hw/npu2.c | 273 +++++++++++++++++++++++++++++++++++++++++++++++++++- include/npu2-regs.h | 21 +++- include/opal-api.h | 4 +- include/pci.h | 8 ++ include/phb4.h | 3 + 5 files changed, 303 insertions(+), 6 deletions(-) diff --git a/hw/npu2.c b/hw/npu2.c index 9748536..6ff6e4d 100644 --- a/hw/npu2.c +++ b/hw/npu2.c @@ -38,6 +38,7 @@ #include #include #include +#include #define NPU2_IRQ_BASE_SHIFT 13 #define NPU2_N_DL_IRQS 23 @@ -919,12 +920,17 @@ static void npu2_hw_init(struct npu2 *p) } /* Static initialization of every relaxed-ordering cfg[2] register */ - val = NPU2_RELAXED_ORDERING_CMD_CL_RD_NC_F0 | + val = NPU2_RELAXED_ORDERING_CMD_CL_DMA_W | + NPU2_RELAXED_ORDERING_CMD_CL_DMA_W_HP | + NPU2_RELAXED_ORDERING_CMD_CL_DMA_INJ | + NPU2_RELAXED_ORDERING_CMD_PR_DMA_INJ | + NPU2_RELAXED_ORDERING_CMD_DMA_PR_W | + NPU2_RELAXED_ORDERING_CMD_CL_RD_NC_F0 | NPU2_RELAXED_ORDERING_SOURCE4_RDENA; for (s = NPU2_STACK_STCK_0; s <= NPU2_STACK_STCK_2; s++) { for (b = NPU2_BLOCK_SM_0; b <= NPU2_BLOCK_SM_3; b++) { - reg = NPU2_REG_OFFSET(s, b, NPU2_RELAXED_ORDERING_CFG2); + reg = NPU2_REG_OFFSET(s, b, NPU2_RELAXED_ORDERING_CFG(2)); npu2_write(p, reg, val); } } @@ -2253,3 +2259,266 @@ out: return rc; } opal_call(OPAL_NPU_MAP_LPAR, opal_npu_map_lpar, 4); + +static inline uint32_t npu2_relaxed_ordering_source_grpchp(uint32_t gcid) +{ + /* Repack 0bGGGGCCC to 0bGGCC */ + return ((gcid & 0x18) >> 1) | (gcid & 0x3); +} + +static uint64_t npu2_relaxed_ordering_cfg_read(struct npu2_dev *ndev, int n) +{ + uint64_t reg = NPU2_SM_REG_OFFSET(ndev, 0, NPU2_RELAXED_ORDERING_CFG(n)); + + return npu2_read(ndev->npu, reg); +} + +static void npu2_relaxed_ordering_cfg_write(struct npu2_dev *ndev, int n, + uint64_t val) +{ + uint64_t reg; + int sm; + + /* Set every register on our stack */ + for (sm = NPU2_BLOCK_SM_0; sm <= NPU2_BLOCK_SM_3; sm++) { + reg = NPU2_SM_REG_OFFSET(ndev, sm, NPU2_RELAXED_ORDERING_CFG(n)); + npu2_write(ndev->npu, reg, val); + } +} + +/* + * Parse the value of a relaxed ordering config register. Returns SOURCE0 or + * SOURCE1 register mask if relaxed ordering is set for the given chip/pec. + * Returns 0 if unset. + */ +static uint64_t npu2_relaxed_ordering_cfg_enabled(uint64_t val, uint32_t gcid, + int pec) +{ + uint32_t src, grpchp; + uint64_t mask; + int i; + + for (i = 0; i < 2; i++) { + mask = NPU2_RELAXED_ORDERING_SOURCE(i); + src = GETFIELD(mask, val); + + if (!GETFIELD(NPU2_RELAXED_ORDERING_SOURCE_ENA, src)) + continue; + + if (GETFIELD(NPU2_RELAXED_ORDERING_SOURCE_PECSEL, src) != pec) + continue; + + grpchp = GETFIELD(NPU2_RELAXED_ORDERING_SOURCE_GRPCHP, src); + if (grpchp == npu2_relaxed_ordering_source_grpchp(gcid)) + return mask; + + if (grpchp == 0xf) /* match all */ + return mask; + } + + return 0; +} + +static int npu2_enable_relaxed_ordering(struct npu2_dev *ndev, uint32_t gcid, + int pec) +{ + uint64_t val, mask; + uint32_t src; + int rc = OPAL_RESOURCE; + int i; + + NPU2DEVINF(ndev, "Enabling relaxed ordering for PEC %d on chip %d\n", pec, gcid); + lock(&ndev->npu->lock); + + for (i = 0; i < 2; i++) { + val = npu2_relaxed_ordering_cfg_read(ndev, i); + if (!npu2_relaxed_ordering_cfg_enabled(val, gcid, pec)) + continue; + + /* Already enabled */ + rc = OPAL_SUCCESS; + goto out; + } + + src = NPU2_RELAXED_ORDERING_SOURCE_WRENA | + NPU2_RELAXED_ORDERING_SOURCE_RDENA; + src = SETFIELD(NPU2_RELAXED_ORDERING_SOURCE_PECSEL, src, pec); + src = SETFIELD(NPU2_RELAXED_ORDERING_SOURCE_GRPCHP, src, + npu2_relaxed_ordering_source_grpchp(gcid)); + src = SETFIELD(NPU2_RELAXED_ORDERING_SOURCE_WRMIN, src, 0); + src = SETFIELD(NPU2_RELAXED_ORDERING_SOURCE_WRMAX, src, 23); + src = SETFIELD(NPU2_RELAXED_ORDERING_SOURCE_RDMIN, src, 0); + src = SETFIELD(NPU2_RELAXED_ORDERING_SOURCE_RDMAX, src, 47); + + /* Find somewhere to write this config */ + for (i = 0; i < 2; i++) { + val = npu2_relaxed_ordering_cfg_read(ndev, i); + + if (!GETFIELD(NPU2_RELAXED_ORDERING_SOURCE_ENA << 32, val)) + mask = NPU2_RELAXED_ORDERING_SOURCE(0); + else if (!GETFIELD(NPU2_RELAXED_ORDERING_SOURCE_ENA, val)) + mask = NPU2_RELAXED_ORDERING_SOURCE(1); + else + continue; + + val = SETFIELD(mask, val, src); + npu2_relaxed_ordering_cfg_write(ndev, i, val); + + rc = OPAL_SUCCESS; + break; + } + +out: + unlock(&ndev->npu->lock); + return rc; +} + +static void npu2_disable_relaxed_ordering(struct npu2_dev *ndev, uint32_t gcid, + int pec) +{ + uint64_t val, mask; + int i; + + NPU2DEVINF(ndev, "Disabling relaxed ordering for PEC %d on chip %d\n", pec, gcid); + lock(&ndev->npu->lock); + + for (i = 0; i < 2; i++) { + val = npu2_relaxed_ordering_cfg_read(ndev, i); + + mask = npu2_relaxed_ordering_cfg_enabled(val, gcid, pec); + if (!mask) + continue; + + val = SETFIELD(mask, val, 0); + npu2_relaxed_ordering_cfg_write(ndev, i, val); + } + + unlock(&ndev->npu->lock); +} + +/* + * Enable or disable relaxed ordering on all nvlinks on a given NPU. May leave + * relaxed ordering partially enabled if there are insufficient HW resources to + * enable it on all links. + */ +static int npu2_set_relaxed_ordering(uint32_t gcid, int pec, bool enable) +{ + int rc = OPAL_SUCCESS; + struct phb *phb; + struct npu2 *npu; + struct npu2_dev *ndev; + + for_each_phb(phb) { + if (phb->phb_type != phb_type_npu_v2) + continue; + + npu = phb_to_npu2_nvlink(phb); + for (int i = 0; i < npu->total_devices; i++) { + ndev = &npu->devices[i]; + if (enable) + rc = npu2_enable_relaxed_ordering(ndev, gcid, pec); + else + npu2_disable_relaxed_ordering(ndev, gcid, pec); + + if (rc != OPAL_SUCCESS) { + NPU2DEVINF(ndev, "Insufficient resources to activate relaxed ordering mode\n"); + return OPAL_RESOURCE; + } + } + } + + return OPAL_SUCCESS; +} + +static int npu2_check_relaxed_ordering(struct phb *phb __unused, + struct pci_device *pd, void *enable) +{ + /* + * IBM PCIe bridge devices (ie. the root ports) can always allow relaxed + * ordering + */ + if (pd->vdid == 0x04c11014) + pd->allow_relaxed_ordering = true; + + PCINOTICE(phb, pd->bdfn, "Checking relaxed ordering config\n"); + if (pd->allow_relaxed_ordering) + return 0; + + PCINOTICE(phb, pd->bdfn, "Relaxed ordering not allowed\n"); + *(bool *) enable = false; + + return 1; +} + +static int64_t opal_npu_set_relaxed_order(uint64_t phb_id, uint16_t bdfn, + bool request_enabled) +{ + struct phb *phb = pci_get_phb(phb_id); + struct phb4 *phb4; + uint32_t chip_id, pec; + struct pci_device *pd; + bool enable = true; + + if (!phb || phb->phb_type != phb_type_pcie_v4) + return OPAL_PARAMETER; + + phb4 = phb_to_phb4(phb); + pec = phb4->pec; + chip_id = phb4->chip_id; + + /* Can chip_id be packed into NPU2_RELAXED_ORDERING_SOURCE_GRPCHP? */ + if (chip_id & 0x64) + return OPAL_PARAMETER; + + pd = pci_find_dev(phb, bdfn); + if (!pd) + return OPAL_PARAMETER; + + /* + * Not changing state, so no need to rescan PHB devices to determine if + * we need to enable/disable it + */ + if (pd->allow_relaxed_ordering == request_enabled) + return OPAL_SUCCESS; + + pd->allow_relaxed_ordering = request_enabled; + + /* + * Walk all devices on this PHB to ensure they all support relaxed + * ordering + */ + pci_walk_dev(phb, NULL, npu2_check_relaxed_ordering, &enable); + + if (request_enabled && !enable) { + /* + * Not all devices on this PHB support relaxed-ordering + * mode so we can't enable it as requested + */ + prlog(PR_INFO, "Cannot set relaxed ordering for PEC %d on chip %d\n", + pec, chip_id); + return OPAL_CONSTRAINED; + } + + if (npu2_set_relaxed_ordering(chip_id, pec, request_enabled) != OPAL_SUCCESS) { + npu2_set_relaxed_ordering(chip_id, pec, false); + return OPAL_RESOURCE; + } + + phb4->ro_state = request_enabled; + return OPAL_SUCCESS; +} +opal_call(OPAL_NPU_SET_RELAXED_ORDER, opal_npu_set_relaxed_order, 3); + +static int64_t opal_npu_get_relaxed_order(uint64_t phb_id, + uint16_t bdfn __unused) +{ + struct phb *phb = pci_get_phb(phb_id); + struct phb4 *phb4; + + if (!phb || phb->phb_type != phb_type_pcie_v4) + return OPAL_PARAMETER; + + phb4 = phb_to_phb4(phb); + return phb4->ro_state; +} +opal_call(OPAL_NPU_GET_RELAXED_ORDER, opal_npu_get_relaxed_order, 2); diff --git a/include/npu2-regs.h b/include/npu2-regs.h index d9db988..6bd77e4 100644 --- a/include/npu2-regs.h +++ b/include/npu2-regs.h @@ -152,9 +152,24 @@ void npu2_scom_write(uint64_t gcid, uint64_t scom_base, #define NPU2_LOW_WATER_MARKS 0x040 #define NPU2_LOW_WATER_MARKS_ENABLE_MACHINE_ALLOC PPC_BIT(51) #define NPU2_HIGH_WATER_MARKS 0x048 -#define NPU2_RELAXED_ORDERING_CFG0 0x050 -#define NPU2_RELAXED_ORDERING_CFG1 0x058 -#define NPU2_RELAXED_ORDERING_CFG2 0x060 +#define NPU2_RELAXED_ORDERING_CFG(n) (0x050 + (n)*8) +#define NPU2_RELAXED_ORDERING_SOURCE(n) (PPC_BITMASK(0,31) >> ((n)*32)) +#define NPU2_RELAXED_ORDERING_SOURCE_ENA PPC_BITMASK32(0,3) +#define NPU2_RELAXED_ORDERING_SOURCE_WRENA PPC_BIT32(0) +#define NPU2_RELAXED_ORDERING_SOURCE_RDENA PPC_BIT32(1) +#define NPU2_RELAXED_ORDERING_SOURCE_AWENA PPC_BIT32(2) +#define NPU2_RELAXED_ORDERING_SOURCE_ARENA PPC_BIT32(3) +#define NPU2_RELAXED_ORDERING_SOURCE_PECSEL PPC_BITMASK32(4,5) +#define NPU2_RELAXED_ORDERING_SOURCE_GRPCHP PPC_BITMASK32(6,9) +#define NPU2_RELAXED_ORDERING_SOURCE_WRMIN PPC_BITMASK32(10,14) +#define NPU2_RELAXED_ORDERING_SOURCE_WRMAX PPC_BITMASK32(15,19) +#define NPU2_RELAXED_ORDERING_SOURCE_RDMIN PPC_BITMASK32(20,25) +#define NPU2_RELAXED_ORDERING_SOURCE_RDMAX PPC_BITMASK32(26,31) +#define NPU2_RELAXED_ORDERING_CMD_CL_DMA_W PPC_BIT(0) +#define NPU2_RELAXED_ORDERING_CMD_CL_DMA_W_HP PPC_BIT(1) +#define NPU2_RELAXED_ORDERING_CMD_CL_DMA_INJ PPC_BIT(2) +#define NPU2_RELAXED_ORDERING_CMD_PR_DMA_INJ PPC_BIT(3) +#define NPU2_RELAXED_ORDERING_CMD_DMA_PR_W PPC_BIT(4) #define NPU2_RELAXED_ORDERING_CMD_CL_RD_NC_F0 PPC_BIT(5) #define NPU2_RELAXED_ORDERING_SOURCE4_RDENA PPC_BIT(29) #define NPU2_NTL0_BAR 0x068 diff --git a/include/opal-api.h b/include/opal-api.h index f766dce..5f397c8 100644 --- a/include/opal-api.h +++ b/include/opal-api.h @@ -224,7 +224,9 @@ #define OPAL_PCI_SET_PBCQ_TUNNEL_BAR 165 #define OPAL_HANDLE_HMI2 166 #define OPAL_NX_COPROC_INIT 167 -#define OPAL_LAST 167 +#define OPAL_NPU_SET_RELAXED_ORDER 168 +#define OPAL_NPU_GET_RELAXED_ORDER 169 +#define OPAL_LAST 169 #define QUIESCE_HOLD 1 /* Spin all calls at entry */ #define QUIESCE_REJECT 2 /* Fail all calls with OPAL_BUSY */ diff --git a/include/pci.h b/include/pci.h index 19fe9c0..1d1cec7 100644 --- a/include/pci.h +++ b/include/pci.h @@ -111,6 +111,14 @@ struct pci_device { uint32_t pcrf_end; struct list_head pcrf; + /* + * Relaxed ordering is a feature which allows PCIe devices accessing GPU + * memory to bypass the normal PCIe ordering rules to increase + * performance. It is enabled on a per-PEC basis so every device on a + * PEC must support it before we can enable it. + */ + bool allow_relaxed_ordering; + struct dt_node *dn; struct pci_slot *slot; struct pci_device *parent; diff --git a/include/phb4.h b/include/phb4.h index 3186dca..d78bc31 100644 --- a/include/phb4.h +++ b/include/phb4.h @@ -227,6 +227,9 @@ struct phb4 { /* Cache some RC registers that need to be emulated */ uint32_t rc_cache[4]; + /* Current NPU2 relaxed ordering state */ + bool ro_state; + struct phb phb; };