Received: by 2002:ab2:1e85:0:b0:1fd:da4e:a717 with SMTP id i5csp78676lqa; Thu, 13 Jun 2024 01:33:19 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUaEbdyZwmcT/9D2hsx+t2bxcGCKpWxy8u+Mu0bl8+YPoemccX5xCnsuYZuYfBMGTfYYAywXlumSFo88GkfLgcUucEhPNqLR8icm1Dhmw== X-Google-Smtp-Source: AGHT+IHvHPRnED4rIUGqXzBcp/kLDCXR9iPM+AAUM+kAaJ3oN0PjyIgPJZwqHcuG562BSL/xjjgN X-Received: by 2002:a17:907:9722:b0:a68:fcc9:6c1c with SMTP id a640c23a62f3a-a6f47c26f3fmr374603466b.0.1718267599395; Thu, 13 Jun 2024 01:33:19 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1718267599; cv=pass; d=google.com; s=arc-20160816; b=kbCgWjvAq4T9ovsfOruEmPvc0SEA7oQxIvYTnCchujoSsF1ZHYI4dc16qEMidRMahH d+Ji5GsiZdHU27KkGJynJiFYTxWXLVMXDTeFnltyaUDKWK4CLBcxK72idLuPUl5PM7wm nLjvUnncSczAyxa0XmnQZeTNYnnE2K5X8nfyB9QviLSL+NV7v1w8J3biSOPFw1VG8pzr /2nm4iRmk9bNh4Dnu6Vo3mgezXpSXL6pmBJ+59ia8Qgv3XKh+K8nHXRG/7AQaPSTZwSq Mv9GA4Md4GzdUupeO/FoiSxtwJUWvQ30P/yI+3Ug3tGB9TDH6n/mabGkdJIVQZuPvzM2 vWdw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=oInlVLh5qFDI2BwLJbuQmWN5SxzJtVR3488O4vacFUM=; fh=ggzvYjOt5PEH3wP+YY+NtV+tqDBSLt+G9bD5UH+X8A4=; b=h/FDKSInA88erroeHvPgcRDVSm4HGONYkZHc48PRXWt40k0Q9jyQrDY3U7l4LohO3O SZn3dn5YQd2eG+vOPgp/cDM8Qlvy0esB9YPGfTcfasS23BxN8WX+p9EL2CuEic2bSODF d5IJfRD5e2WMueaDRL+zfyH/nPO470bhd96WRomdNVZrg5TFBMb+eSk1PGZ1C6eppAvL QrttLT9QUjF7j5QXAWbkl2/qMZ89kIvu62ZaxxFxrqsjJLHFA3eflAIHCz6l7MB2VWNb 4QnHbYi/hiMw7EZWQMTDY4+IuqQtIpBwUpogJZAxMWVaqJfOmbKu5KT0qqiI3tq85223 ZPJg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@habana.ai header.s=default header.b=PYK4Tch2; arc=pass (i=1 spf=pass spfdomain=habana.ai dkim=pass dkdomain=habana.ai dmarc=pass fromdomain=habana.ai); spf=pass (google.com: domain of linux-kernel+bounces-212821-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-212821-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=habana.ai Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id a640c23a62f3a-a6f56df71a5si46174466b.631.2024.06.13.01.33.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Jun 2024 01:33:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-212821-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@habana.ai header.s=default header.b=PYK4Tch2; arc=pass (i=1 spf=pass spfdomain=habana.ai dkim=pass dkdomain=habana.ai dmarc=pass fromdomain=habana.ai); spf=pass (google.com: domain of linux-kernel+bounces-212821-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-212821-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=habana.ai Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 8AB9D1F212B5 for ; Thu, 13 Jun 2024 08:33:15 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 39502143725; Thu, 13 Jun 2024 08:29:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=habana.ai header.i=@habana.ai header.b="PYK4Tch2" Received: from mail02.habana.ai (habanamailrelay.habana.ai [213.57.90.13]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AB8CF39FCE; Thu, 13 Jun 2024 08:29:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=213.57.90.13 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718267343; cv=none; b=X23DfzowqQGU9KZ7ozyC1vKaMaP6FN4je8mU/BHS5TbadErLwrnJb9nmS64XuZffZL4vIFmfunZwaV45M3JYlhAmOSJh0WyzVKA1nwWWOVJ5rMVfd28eh3E7oNzKD6XLVpQtwW3wiyk44ltJMX8LIACNz6LH+pOKB4vgLG6B7D4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718267343; c=relaxed/simple; bh=IO5pLkJG81kMm9PCrr62ntV88bUeSIGslJaU4y/h6v8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=UTPlL/8367nMz1lnekiTf6QGXYbtNJRAZn7dwB1p/I0gHTBox15ACtcGwOzLSq2kamtRLaWoOpt/FrC12RfVGnRXNlxGHvmQ3weMJRKHhw+rW+RW8k9CYJXa4sp4YuYJw6dxGt6IkWxDGcYiLQIeSwwFtzvIaKedTu6zCJkzHgs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=habana.ai; spf=pass smtp.mailfrom=habana.ai; dkim=pass (2048-bit key) header.d=habana.ai header.i=@habana.ai header.b=PYK4Tch2; arc=none smtp.client-ip=213.57.90.13 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=habana.ai Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=habana.ai Received: internal info suppressed DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=habana.ai; s=default; t=1718266947; bh=IO5pLkJG81kMm9PCrr62ntV88bUeSIGslJaU4y/h6v8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PYK4Tch2K9/4CKdlzVtqtx4OZfY9PfJ/zXpo08p5hBAtFZh0DmLcFCUQIpKbESdAa 3ugep9B5qn0OBHP8sB5XDaDqt6NqUCIRnnlCRhIEJA69grWCXgdlTM06KvzPCPIxm5 ePOWS4zK203v3PkTdmKKnc6wktddo1vkydk1ljN57rMbn9X4fa18HoYGB8dol+VMO4 6LmW0VWjgAcLQIpShshWMSDEdfmFw8+IBlOHvIJBEe8GLIEFfuHo6aGiXsQh90rxef IIQrX6aA63Kuf4DVs3C6GcPh/zCWdFfp03zZFN1roNyBtTX2srGgRepwrJw+cKm1Le Y0j2usZotT+6A== Received: from oshpigelman-vm-u22.habana-labs.com (localhost [127.0.0.1]) by oshpigelman-vm-u22.habana-labs.com (8.15.2/8.15.2/Debian-22ubuntu3) with ESMTP id 45D8M8hX1440009; Thu, 13 Jun 2024 11:22:08 +0300 From: Omer Shpigelman To: linux-kernel@vger.kernel.org, linux-rdma@vger.kernel.org, netdev@vger.kernel.org, dri-devel@lists.freedesktop.org Cc: ogabbay@kernel.org, oshpigelman@habana.ai, zyehudai@habana.ai Subject: [PATCH 02/15] net: hbl_cn: memory manager component Date: Thu, 13 Jun 2024 11:21:55 +0300 Message-Id: <20240613082208.1439968-3-oshpigelman@habana.ai> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240613082208.1439968-1-oshpigelman@habana.ai> References: <20240613082208.1439968-1-oshpigelman@habana.ai> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Add a common memory manager which handles allocation and mapping. It manages physical/virtual memory on host/device. Signed-off-by: Omer Shpigelman Co-developed-by: Abhilash K V Signed-off-by: Abhilash K V Co-developed-by: Andrey Agranovich Signed-off-by: Andrey Agranovich Co-developed-by: Bharat Jauhari Signed-off-by: Bharat Jauhari Co-developed-by: David Meriin Signed-off-by: David Meriin Co-developed-by: Sagiv Ozeri Signed-off-by: Sagiv Ozeri Co-developed-by: Zvika Yehudai Signed-off-by: Zvika Yehudai --- .../intel/hbl_cn/common/hbl_cn_memory.c | 325 +++++++++++++++++- 1 file changed, 322 insertions(+), 3 deletions(-) diff --git a/drivers/net/ethernet/intel/hbl_cn/common/hbl_cn_memory.c b/drivers/net/ethernet/intel/hbl_cn/common/hbl_cn_memory.c index 93c97fad6a20..878ecba66aa3 100644 --- a/drivers/net/ethernet/intel/hbl_cn/common/hbl_cn_memory.c +++ b/drivers/net/ethernet/intel/hbl_cn/common/hbl_cn_memory.c @@ -4,37 +4,356 @@ * All Rights Reserved. */ +#include #include "hbl_cn.h" -int hbl_cn_mem_alloc(struct hbl_cn_ctx *ctx, struct hbl_cn_mem_data *mem_data) +static int hbl_cn_map_vmalloc_range(struct hbl_cn_ctx *ctx, u64 vmalloc_va, u64 device_va, + u64 size) +{ + struct hbl_cn_aux_ops *aux_ops; + struct hbl_aux_dev *aux_dev; + + aux_dev = ctx->hdev->cn_aux_dev; + aux_ops = aux_dev->aux_ops; + + return aux_ops->vm_dev_mmu_map(aux_dev, ctx->driver_vm_info.vm_handle, HBL_CN_MEM_TYPE_HOST, + vmalloc_va, device_va, size); +} + +static void hbl_cn_unmap_vmalloc_range(struct hbl_cn_ctx *ctx, u64 device_va, u64 size) +{ + struct hbl_cn_aux_ops *aux_ops; + struct hbl_aux_dev *aux_dev; + + aux_dev = ctx->hdev->cn_aux_dev; + aux_ops = aux_dev->aux_ops; + + aux_ops->vm_dev_mmu_unmap(aux_dev, ctx->driver_vm_info.vm_handle, device_va, size); +} + +static int alloc_mem(struct hbl_cn_mem_buf *buf, gfp_t gfp, struct hbl_cn_mem_data *mem_data) +{ + u64 device_addr, size = mem_data->size; + struct hbl_cn_ctx *ctx = buf->ctx; + u32 mem_id = mem_data->mem_id; + struct hbl_cn_device *hdev; + void *p = NULL; + + hdev = ctx->hdev; + + switch (mem_id) { + case HBL_CN_DRV_MEM_HOST_DMA_COHERENT: + if (get_order(size) > MAX_PAGE_ORDER) { + dev_err(hdev->dev, "memory size 0x%llx must be less than 0x%lx\n", size, + 1UL << (PAGE_SHIFT + MAX_PAGE_ORDER)); + return -ENOMEM; + } + + p = hbl_cn_dma_alloc_coherent(hdev, size, &buf->bus_address, GFP_USER | __GFP_ZERO); + if (!p) { + dev_err(hdev->dev, + "failed to allocate 0x%llx of dma memory for the NIC\n", size); + return -ENOMEM; + } + + break; + case HBL_CN_DRV_MEM_HOST_VIRTUAL: + p = vmalloc_user(size); + if (!p) { + dev_err(hdev->dev, "failed to allocate vmalloc memory, size 0x%llx\n", + size); + return -ENOMEM; + } + + break; + case HBL_CN_DRV_MEM_HOST_MAP_ONLY: + p = mem_data->in.host_map_data.kernel_address; + buf->bus_address = mem_data->in.host_map_data.bus_address; + break; + case HBL_CN_DRV_MEM_DEVICE: + if (!hdev->wq_arrays_pool_enable) { + dev_err(hdev->dev, "No WQ arrays pool support for device memory\n"); + return -EOPNOTSUPP; + } + + device_addr = (u64)gen_pool_alloc(hdev->wq_arrays_pool, size); + if (!device_addr) { + dev_err(hdev->dev, "Failed to allocate device memory, size 0x%llx\n", size); + return -ENOMEM; + } + + buf->device_addr = device_addr; + break; + default: + dev_err(hdev->dev, "Invalid mem_id %d\n", mem_id); + return -EINVAL; + } + + buf->kernel_address = p; + buf->mappable_size = size; + + return 0; +} + +static int map_mem(struct hbl_cn_mem_buf *buf, struct hbl_cn_mem_data *mem_data) +{ + struct hbl_cn_ctx *ctx = buf->ctx; + struct hbl_cn_device *hdev; + int rc; + + hdev = ctx->hdev; + + if (mem_data->mem_id == HBL_CN_DRV_MEM_HOST_DMA_COHERENT) { + dev_err(hdev->dev, "Mapping DMA coherent host memory is not yet supported\n"); + return -EPERM; + } + + rc = hbl_cn_map_vmalloc_range(ctx, (u64)buf->kernel_address, mem_data->device_va, + buf->mappable_size); + if (rc) + return rc; + + buf->device_va = mem_data->device_va; + + return 0; +} + +static void mem_do_release(struct hbl_cn_device *hdev, struct hbl_cn_mem_buf *buf) +{ + struct hbl_cn_asic_funcs *asic_funcs = hdev->asic_funcs; + + if (buf->mem_id == HBL_CN_DRV_MEM_HOST_DMA_COHERENT) + asic_funcs->dma_free_coherent(hdev, buf->mappable_size, buf->kernel_address, + buf->bus_address); + else if (buf->mem_id == HBL_CN_DRV_MEM_HOST_VIRTUAL) + vfree(buf->kernel_address); + else if (buf->mem_id == HBL_CN_DRV_MEM_DEVICE) + gen_pool_free(hdev->wq_arrays_pool, buf->device_addr, buf->mappable_size); +} + +static int __cn_mem_buf_alloc(struct hbl_cn_mem_buf *buf, gfp_t gfp, + struct hbl_cn_mem_data *mem_data) +{ + struct hbl_cn_ctx *ctx = buf->ctx; + struct hbl_cn_device *hdev; + int rc; + + hdev = ctx->hdev; + + if (mem_data->mem_id != HBL_CN_DRV_MEM_DEVICE) + mem_data->size = PAGE_ALIGN(mem_data->size); + + rc = alloc_mem(buf, gfp, mem_data); + if (rc) + return rc; + + if (mem_data->device_va) { + mem_data->device_va = PAGE_ALIGN(mem_data->device_va); + rc = map_mem(buf, mem_data); + if (rc) + goto release_mem; + } + + return 0; + +release_mem: + mem_do_release(hdev, buf); + return rc; +} + +static struct hbl_cn_mem_buf *cn_mem_buf_alloc(struct hbl_cn_ctx *ctx, gfp_t gfp, + struct hbl_cn_mem_data *mem_data) +{ + struct xa_limit id_limit = XA_LIMIT(1, INT_MAX); + struct hbl_cn_device *hdev = ctx->hdev; + struct hbl_cn_mem_buf *buf; + int rc; + u32 id; + + buf = kzalloc(sizeof(*buf), gfp); + if (!buf) + return NULL; + + rc = xa_alloc(&hdev->mem_ids, &id, buf, id_limit, GFP_ATOMIC); + if (rc) { + dev_err(hdev->dev, "Failed to allocate xarray for a new buffer, rc=%d\n", rc); + goto free_buf; + } + + buf->ctx = ctx; + buf->mem_id = mem_data->mem_id; + + buf->handle = (((u64)id | hdev->mmap_type_flag) << PAGE_SHIFT); + kref_init(&buf->refcount); + + rc = __cn_mem_buf_alloc(buf, gfp, mem_data); + if (rc) + goto remove_xa; + + return buf; + +remove_xa: + xa_erase(&hdev->mem_ids, lower_32_bits(buf->handle >> PAGE_SHIFT)); +free_buf: + kfree(buf); + return NULL; +} + +static int cn_mem_alloc(struct hbl_cn_ctx *ctx, struct hbl_cn_mem_data *mem_data) { + struct hbl_cn_mem_buf *buf; + + buf = cn_mem_buf_alloc(ctx, GFP_KERNEL, mem_data); + if (!buf) + return -ENOMEM; + + mem_data->handle = buf->handle; + + if (mem_data->mem_id == HBL_CN_DRV_MEM_HOST_DMA_COHERENT) + mem_data->addr = (u64)buf->bus_address; + else if (mem_data->mem_id == HBL_CN_DRV_MEM_HOST_VIRTUAL) + mem_data->addr = (u64)buf->kernel_address; + else if (mem_data->mem_id == HBL_CN_DRV_MEM_DEVICE) + mem_data->addr = (u64)buf->device_addr; + return 0; } +int hbl_cn_mem_alloc(struct hbl_cn_ctx *ctx, struct hbl_cn_mem_data *mem_data) +{ + struct hbl_cn_device *hdev = ctx->hdev; + int rc; + + switch (mem_data->mem_id) { + case HBL_CN_DRV_MEM_HOST_DMA_COHERENT: + case HBL_CN_DRV_MEM_HOST_VIRTUAL: + case HBL_CN_DRV_MEM_HOST_MAP_ONLY: + case HBL_CN_DRV_MEM_DEVICE: + rc = cn_mem_alloc(ctx, mem_data); + break; + default: + dev_dbg(hdev->dev, "Invalid mem_id %d\n", mem_data->mem_id); + rc = -EINVAL; + break; + } + + return rc; +} + +static void cn_mem_buf_destroy(struct hbl_cn_mem_buf *buf) +{ + if (buf->device_va) + hbl_cn_unmap_vmalloc_range(buf->ctx, buf->device_va, buf->mappable_size); + + mem_do_release(buf->ctx->hdev, buf); + + kfree(buf); +} + int hbl_cn_mem_destroy(struct hbl_cn_device *hdev, u64 handle) { + struct hbl_cn_mem_buf *buf; + int rc; + + buf = hbl_cn_mem_buf_get(hdev, handle); + if (!buf) { + dev_dbg(hdev->dev, "Memory destroy failed, no match for handle 0x%llx\n", handle); + return -EINVAL; + } + + rc = atomic_cmpxchg(&buf->is_destroyed, 0, 1); + hbl_cn_mem_buf_put(buf); + if (rc) { + dev_dbg(hdev->dev, "Memory destroy failed, handle 0x%llx was already destroyed\n", + handle); + return -EINVAL; + } + + rc = hbl_cn_mem_buf_put_handle(hdev, handle); + if (rc < 0) + return rc; + + if (rc == 0) + dev_dbg(hdev->dev, "Handle 0x%llx is destroyed while still in use\n", handle); + return 0; } +static void cn_mem_buf_release(struct kref *kref) +{ + struct hbl_cn_mem_buf *buf = container_of(kref, struct hbl_cn_mem_buf, refcount); + struct hbl_cn_device *hdev = buf->ctx->hdev; + + xa_erase(&hdev->mem_ids, lower_32_bits(buf->handle >> PAGE_SHIFT)); + + cn_mem_buf_destroy(buf); +} + struct hbl_cn_mem_buf *hbl_cn_mem_buf_get(struct hbl_cn_device *hdev, u64 handle) { - return NULL; + struct hbl_cn_mem_buf *buf; + + xa_lock(&hdev->mem_ids); + buf = xa_load(&hdev->mem_ids, lower_32_bits(handle >> PAGE_SHIFT)); + if (!buf) { + xa_unlock(&hdev->mem_ids); + dev_dbg(hdev->dev, "Buff get failed, no match to handle %#llx\n", handle); + return NULL; + } + + kref_get(&buf->refcount); + xa_unlock(&hdev->mem_ids); + + return buf; } int hbl_cn_mem_buf_put(struct hbl_cn_mem_buf *buf) { - return 0; + return kref_put(&buf->refcount, cn_mem_buf_release); +} + +static void cn_mem_buf_remove_xa_locked(struct kref *kref) +{ + struct hbl_cn_mem_buf *buf = container_of(kref, struct hbl_cn_mem_buf, refcount); + + __xa_erase(&buf->ctx->hdev->mem_ids, lower_32_bits(buf->handle >> PAGE_SHIFT)); } int hbl_cn_mem_buf_put_handle(struct hbl_cn_device *hdev, u64 handle) { + struct hbl_cn_mem_buf *buf; + + xa_lock(&hdev->mem_ids); + buf = xa_load(&hdev->mem_ids, lower_32_bits(handle >> PAGE_SHIFT)); + if (!buf) { + xa_unlock(&hdev->mem_ids); + dev_dbg(hdev->dev, "Buff put failed, no match to handle %#llx\n", handle); + return -EINVAL; + } + + if (kref_put(&buf->refcount, cn_mem_buf_remove_xa_locked)) { + xa_unlock(&hdev->mem_ids); + cn_mem_buf_destroy(buf); + return 1; + } + + xa_unlock(&hdev->mem_ids); return 0; } void hbl_cn_mem_init(struct hbl_cn_device *hdev) { + xa_init_flags(&hdev->mem_ids, XA_FLAGS_ALLOC); } void hbl_cn_mem_fini(struct hbl_cn_device *hdev) { + struct xarray *mem_ids; + + mem_ids = &hdev->mem_ids; + + if (!xa_empty(mem_ids)) + dev_crit(hdev->dev, "memory manager is destroyed while not empty!\n"); + + xa_destroy(mem_ids); } -- 2.34.1