From nobody Sun Apr 5 13:05:00 2026 Received: from CH4PR04CU002.outbound.protection.outlook.com (mail-northcentralusazon11013012.outbound.protection.outlook.com [40.107.201.12]) (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 CEAEA3DBD5E for ; Tue, 24 Mar 2026 16:32:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.201.12 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774369930; cv=fail; b=aUhaRfUD/w/5pPXjlnyFX3xXwRLKtwtw1ZvK2/gJfAF4VKutAElKuLT4zYfyEZw8cpzvgqhEFEUYSYzJL/WfODj35kp7FhrDjaS5rNpuVqHtviBhoynwX1Z87oVieGzA3s/KKMUZpMa6t0laN4aUvsgIxI3/3e9sw+KqSh95tDM= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774369930; c=relaxed/simple; bh=IkrIbCixiXecXYBCxclPnRJvWGsMudVylCfnrUkyIDM=; h=From:To:CC:Subject:Date:Message-ID:MIME-Version:Content-Type; b=tRzYNrxX0c/0a7+p+Rb8HcJgY4A8bG3gbHBQnMckQSTSIqZdbPNpsybh+STiLLTD251fW7X1SM+Rx2P408gcdiA7ryutSUxmmoW359LbokP6aeuQX1SyoVxK4Ibsjhfqfj86p4iefsCJWxbpM8kL+wql+SlVZnkULvkirEJW9E4= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=5fqjrCVg; arc=fail smtp.client-ip=40.107.201.12 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="5fqjrCVg" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=usQ1m6yRTGT/tz7ZF3P4mVuy/PXjeC7soXjRRi/m+nEhH5za2TmLrlOwIb6O8TzWkUmWXCMRLo3WI1od7WHQ/bJZnocIX1k+no5K1kpIZtK5rVuRQpF3G5rAmDmHiYildlnP8Q+Wi+0iFy5kdyMqyK3MThlFXS4xmAGqV9dxrEbuS2GO14/QQgh7NHLjFSACROjpXhWDMHY0Kx3FGnvGEg9jMvRbFHkgkV1hxOVgt4eNnKFb3kcaHMWmOFN/c+7NqCbCcXsyAjMVhI+1oSpfjPg51CYuswFznKSZdkIUpL4yu+hVZswaZhHQOkHznUlVUaZTIsC/j09n8LBk8tE/yg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=YkB1FmZYZvQtqsPRZfucaLrnPuqekLPgJD2KhdXtBeM=; b=g6PddpxZ2BVfkYi71UN6AszWzSvgL+gn08RR4Nsf780hfivq9GwB3WnHqUz6VvbxNyVdfT/YizbW/Sk5wFZyCniC9w7IJELRH7WUdaAOlUOEhjgCbaCmaWF7ciXnlrJOJWG6tagVswhLcZwu4Mv0KYKYCUHrEVobwLrpnTBAsxLGcGgMDEbimk434Mjq5bEEQQGqGh/lkOSU7wSdNseNm8vsuCd959WtgumEpFl0yletyRthciNKATfj3Wnp82+KP1VXrAlbBf1RWuD0Mz82TTl5g78xARQ5zhUf2hAVae7jVJt6r+QKJlsHwtZQGk/ao/jPRIA4uhvJRw5Y+oIGVA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=YkB1FmZYZvQtqsPRZfucaLrnPuqekLPgJD2KhdXtBeM=; b=5fqjrCVgrXtKI9Je3N+WlEoPa8mwrAKUZlzhQ4Ul+3nxwgg3YG/tmQjXr3CK5Q3T7YYJPrAN1GULK6cyniyyieoSwvvPii39vKtt2a4s1KpN3ORR7JLiC9W/S6R1EgWjGB19zxV638buvdfEHo5ke3GDL9QihDJiZgTjL9L18/g= Received: from MN0P220CA0024.NAMP220.PROD.OUTLOOK.COM (2603:10b6:208:52e::18) by CH2PR12MB4278.namprd12.prod.outlook.com (2603:10b6:610:ab::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9745.20; Tue, 24 Mar 2026 16:32:01 +0000 Received: from BN2PEPF00004FC0.namprd04.prod.outlook.com (2603:10b6:208:52e:cafe::f) by MN0P220CA0024.outlook.office365.com (2603:10b6:208:52e::18) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.9723.31 via Frontend Transport; Tue, 24 Mar 2026 16:31:50 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=satlexmb07.amd.com; pr=C Received: from satlexmb07.amd.com (165.204.84.17) by BN2PEPF00004FC0.mail.protection.outlook.com (10.167.243.186) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9723.19 via Frontend Transport; Tue, 24 Mar 2026 16:32:01 +0000 Received: from satlexmb08.amd.com (10.181.42.217) by satlexmb07.amd.com (10.181.42.216) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.17; Tue, 24 Mar 2026 11:32:01 -0500 Received: from xsjlizhih51.xilinx.com (10.180.168.240) by satlexmb08.amd.com (10.181.42.217) with Microsoft SMTP Server id 15.2.2562.17 via Frontend Transport; Tue, 24 Mar 2026 11:32:00 -0500 From: Lizhi Hou To: , , , , CC: Max Zhen , , , Lizhi Hou Subject: [PATCH V1] accel/amdxdna: Add per-process BO memory usage query support Date: Tue, 24 Mar 2026 09:31:59 -0700 Message-ID: <20260324163159.2425461-1-lizhi.hou@amd.com> X-Mailer: git-send-email 2.34.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BN2PEPF00004FC0:EE_|CH2PR12MB4278:EE_ X-MS-Office365-Filtering-Correlation-Id: 6191eb1e-8669-4432-cb23-08de89c2e062 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|82310400026|376014|36860700016|1800799024|56012099003|18002099003; X-Microsoft-Antispam-Message-Info: 92OL85xAcOz3j2U+tx9j7mSwf0sriHqelGMTQbXbRRJdSK/F3F6mgWvxfZJ8V/YLZXUMlYXub8LB76Bf/A9nW0DkwMI+CPMet0dfhynM0DJszgf4Ol6hLgeIS6/YkesI0c12HXTprwhWh3m4Nue6l3VGXHn8n55CkH96oyCvwkOY8bJZSdtDnq3hzpCPMbXDbC5yOXbKrck/GqPuab2IrpfIuVkIfs+STxon72ZtX7yTUi9Eav8h2wpawIAGK6YKsydXNQgg8tqBIFAt2upebpT+7dl7OqQL6JqI8oxIsdnwIaRyrz5xDaAh7JcEuD3hbmSxskj3OFmPOsD+z8hDkbwbYPGI0XzgrA7xFggYlhmKaBiMjwphOgi9YpyhHnd9olaO1OfhOj8KmRJiUpVRRWpGxdyIeUjtUXgsCsoLJLyjihs2svdRA9lceJohd/DtLiazCjwjhRZt3MYTR8uYBASV/FwDZXDphC24wm/rmG6qH40rk9xfEgq9a0fLNV9M1EJR8YHwx/RIVC6WmO7/4V6Lbj+STWk5cHzoBJeZtQ4D6sIWDLCbS89o6s8zZtMeIY593SxOI/34ol8uStEw894qjbL/ChaLXuPGBc/t4f1dxllzEBIaI/ujOILETs8S//bTJRj6/ceHczovmsXcwdhQu4LmiIvDNsC4HAdqmoHgoFJh8fbWgxPsIZg684DR/VEOJGrCiNDrEZaynqA7Y2T5SDNPuDVpuFQFk1L5OD9TnkrVMPXXWq92I+wo6a7yjxIcjvT3SxSCIac9WDs6Uw== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:satlexmb07.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(82310400026)(376014)(36860700016)(1800799024)(56012099003)(18002099003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: QrVziY8TDLaTJGdLQuq7TEW8kZkHoJyQjNVI8yQocBJs0QNXhuzXs2EJaqHtsnx6EITTAj7BpKfoZZwZcYhHNg9K9I8Mw8e6GGhxRByahhVsXqn1kg3LxZg2pXEGY+nm/+tjLzNs1J+WWrSyemrRsEZ6q/T/RX18E/3gQEauwX0cWTPpLcEu3V8QmoD2dSqMw1nrI93eCYgKgCEeyVQMm/xnldiX6VNVes0N1eJSgRMBZ0vV3fIfupAlUyQdTPQUxEaUyZtNYm79z/Ccf3W/XvErKe3U8tJEZOc88lHo84uEg9eUIqgS9es43lp3OSEMbtZpq43NCB+GJqa4HIBSW0iMdGXvOezD/n8LhKzDrAF4D3Kf8WyqtgHQaDMywQJ3zvDan3kwgRPH5zMP3VcWJnphO/S89+dC/FT20HQ7Tz7a7KLab+ooOoLb7F6DYD0s X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Mar 2026 16:32:01.4014 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 6191eb1e-8669-4432-cb23-08de89c2e062 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[satlexmb07.amd.com] X-MS-Exchange-CrossTenant-AuthSource: BN2PEPF00004FC0.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH2PR12MB4278 Content-Type: text/plain; charset="utf-8" From: Max Zhen Add support for querying per-process buffer object (BO) memory usage through the amdxdna GET_ARRAY UAPI. Introduce a new query type, DRM_AMDXDNA_BO_USAGE, along with struct amdxdna_drm_bo_usage to report BO memory usage statistics, including heap, total, and internal usage. Track BO memory usage on a per-client basis by maintaining counters in GEM open/close and heap allocation/free paths. This ensures the reported statistics reflect the current memory footprint of each process. Wire the new query into the GET_ARRAY implementation to expose the usage information to userspace. Signed-off-by: Max Zhen Reviewed-by: Lizhi Hou Signed-off-by: Lizhi Hou Reviewed-by: Mario Limonciello (AMD) --- drivers/accel/amdxdna/aie2_pci.c | 4 + drivers/accel/amdxdna/amdxdna_gem.c | 134 ++++++++++++++++++++++-- drivers/accel/amdxdna/amdxdna_gem.h | 7 +- drivers/accel/amdxdna/amdxdna_pci_drv.c | 6 +- drivers/accel/amdxdna/amdxdna_pci_drv.h | 4 + include/uapi/drm/amdxdna_accel.h | 35 +++++++ 6 files changed, 177 insertions(+), 13 deletions(-) diff --git a/drivers/accel/amdxdna/aie2_pci.c b/drivers/accel/amdxdna/aie2_= pci.c index 9e39bfe75971..f1ac4e00bd9f 100644 --- a/drivers/accel/amdxdna/aie2_pci.c +++ b/drivers/accel/amdxdna/aie2_pci.c @@ -865,6 +865,7 @@ static int aie2_hwctx_status_cb(struct amdxdna_hwctx *h= wctx, void *arg) tmp->command_submissions =3D hwctx->priv->seq; tmp->command_completions =3D hwctx->priv->completed; tmp->pasid =3D hwctx->client->pasid; + tmp->heap_usage =3D hwctx->client->heap_usage; tmp->priority =3D hwctx->qos.priority; tmp->gops =3D hwctx->qos.gops; tmp->fps =3D hwctx->qos.fps; @@ -1148,6 +1149,9 @@ static int aie2_get_array(struct amdxdna_client *clie= nt, case DRM_AMDXDNA_HW_LAST_ASYNC_ERR: ret =3D aie2_get_array_async_error(xdna->dev_handle, args); break; + case DRM_AMDXDNA_BO_USAGE: + ret =3D amdxdna_drm_get_bo_usage(&xdna->ddev, args); + break; default: XDNA_ERR(xdna, "Not supported request parameter %u", args->param); ret =3D -EOPNOTSUPP; diff --git a/drivers/accel/amdxdna/amdxdna_gem.c b/drivers/accel/amdxdna/am= dxdna_gem.c index 27712704e42d..238ee244d4a6 100644 --- a/drivers/accel/amdxdna/amdxdna_gem.c +++ b/drivers/accel/amdxdna/amdxdna_gem.c @@ -63,6 +63,8 @@ amdxdna_gem_heap_alloc(struct amdxdna_gem_obj *abo) goto unlock_out; } =20 + client->heap_usage +=3D mem->size; + drm_gem_object_get(to_gobj(heap)); =20 unlock_out: @@ -74,16 +76,17 @@ amdxdna_gem_heap_alloc(struct amdxdna_gem_obj *abo) static void amdxdna_gem_heap_free(struct amdxdna_gem_obj *abo) { + struct amdxdna_client *client =3D abo->client; struct amdxdna_gem_obj *heap; =20 - mutex_lock(&abo->client->mm_lock); + mutex_lock(&client->mm_lock); =20 drm_mm_remove_node(&abo->mm_node); - - heap =3D abo->client->dev_heap; + client->heap_usage -=3D abo->mem.size; + heap =3D client->dev_heap; drm_gem_object_put(to_gobj(heap)); =20 - mutex_unlock(&abo->client->mm_lock); + mutex_unlock(&client->mm_lock); } =20 static struct amdxdna_gem_obj * @@ -102,6 +105,8 @@ amdxdna_gem_create_obj(struct drm_device *dev, size_t s= ize) abo->mem.dma_addr =3D AMDXDNA_INVALID_ADDR; abo->mem.uva =3D AMDXDNA_INVALID_ADDR; abo->mem.size =3D size; + abo->open_ref =3D 0; + abo->internal =3D false; INIT_LIST_HEAD(&abo->mem.umap_list); =20 return abo; @@ -508,13 +513,55 @@ static void amdxdna_imported_obj_free(struct amdxdna_= gem_obj *abo) kfree(abo); } =20 +static inline bool +amdxdna_gem_skip_bo_usage(struct amdxdna_gem_obj *abo) +{ + /* Do not count imported BOs since the buffer is not allocated by us. */ + if (is_import_bo(abo)) + return true; + + /* Already counted as part of HEAP BO */ + if (abo->type =3D=3D AMDXDNA_BO_DEV) + return true; + + return false; +} + +static void +amdxdna_gem_add_bo_usage(struct amdxdna_gem_obj *abo) +{ + struct amdxdna_client *client =3D abo->client; + + if (amdxdna_gem_skip_bo_usage(abo)) + return; + + guard(mutex)(&client->mm_lock); + + client->total_bo_usage +=3D abo->mem.size; + if (abo->internal) + client->total_int_bo_usage +=3D abo->mem.size; +} + +static void +amdxdna_gem_del_bo_usage(struct amdxdna_gem_obj *abo) +{ + struct amdxdna_client *client =3D abo->client; + + if (amdxdna_gem_skip_bo_usage(abo)) + return; + + guard(mutex)(&client->mm_lock); + + client->total_bo_usage -=3D abo->mem.size; + if (abo->internal) + client->total_int_bo_usage -=3D abo->mem.size; +} + static void amdxdna_gem_obj_free(struct drm_gem_object *gobj) { struct amdxdna_dev *xdna =3D to_xdna_dev(gobj->dev); struct amdxdna_gem_obj *abo =3D to_xdna_obj(gobj); =20 - XDNA_DBG(xdna, "BO type %d xdna_addr 0x%llx", abo->type, amdxdna_gem_dev_= addr(abo)); - amdxdna_hmm_unregister(abo, NULL); flush_workqueue(xdna->notifier_wq); =20 @@ -543,9 +590,13 @@ static int amdxdna_gem_obj_open(struct drm_gem_object = *gobj, struct drm_file *fi int ret; =20 guard(mutex)(&abo->lock); + abo->open_ref++; =20 - if (!abo->client) + if (abo->open_ref =3D=3D 1) { + /* Attached to the client when first opened by it. */ abo->client =3D filp->driver_priv; + amdxdna_gem_add_bo_usage(abo); + } if (amdxdna_iova_on(xdna)) { ret =3D amdxdna_iommu_map_bo(xdna, abo); if (ret) @@ -555,6 +606,20 @@ static int amdxdna_gem_obj_open(struct drm_gem_object = *gobj, struct drm_file *fi return 0; } =20 +static void amdxdna_gem_obj_close(struct drm_gem_object *gobj, struct drm_= file *filp) +{ + struct amdxdna_gem_obj *abo =3D to_xdna_obj(gobj); + + guard(mutex)(&abo->lock); + abo->open_ref--; + + if (abo->open_ref =3D=3D 0) { + amdxdna_gem_del_bo_usage(abo); + /* Detach from the client when last closed by it. */ + abo->client =3D NULL; + } +} + static int amdxdna_gem_dev_obj_vmap(struct drm_gem_object *obj, struct ios= ys_map *map) { struct amdxdna_gem_obj *abo =3D to_xdna_obj(obj); @@ -575,6 +640,7 @@ static const struct drm_gem_object_funcs amdxdna_gem_de= v_obj_funcs =3D { static const struct drm_gem_object_funcs amdxdna_gem_shmem_funcs =3D { .free =3D amdxdna_gem_obj_free, .open =3D amdxdna_gem_obj_open, + .close =3D amdxdna_gem_obj_close, .print_info =3D drm_gem_shmem_object_print_info, .pin =3D drm_gem_shmem_object_pin, .unpin =3D drm_gem_shmem_object_unpin, @@ -708,10 +774,13 @@ amdxdna_drm_create_share_bo(struct drm_device *dev, if (IS_ERR(abo)) return ERR_CAST(abo); =20 - if (args->type =3D=3D AMDXDNA_BO_DEV_HEAP) + if (args->type =3D=3D AMDXDNA_BO_DEV_HEAP) { abo->type =3D AMDXDNA_BO_DEV_HEAP; - else + abo->internal =3D true; + } else { abo->type =3D AMDXDNA_BO_SHARE; + abo->internal =3D args->type =3D=3D AMDXDNA_BO_CMD; + } =20 return abo; } @@ -783,6 +852,11 @@ amdxdna_drm_create_dev_bo(struct drm_device *dev, gobj =3D to_gobj(abo); gobj->funcs =3D &amdxdna_gem_dev_obj_funcs; abo->type =3D AMDXDNA_BO_DEV; + abo->internal =3D true; + /* + * DEV BOs cannot be alive when client is gone, it's OK to + * always establish the connection. + */ abo->client =3D client; =20 ret =3D amdxdna_gem_heap_alloc(abo); @@ -826,7 +900,7 @@ int amdxdna_drm_create_bo_ioctl(struct drm_device *dev,= void *data, struct drm_f if (IS_ERR(abo)) return PTR_ERR(abo); =20 - /* ready to publish object to userspace */ + /* Ready to publish object to userspace and count for BO usage. */ ret =3D drm_gem_handle_create(filp, to_gobj(abo), &args->handle); if (ret) { XDNA_ERR(xdna, "Create handle failed"); @@ -986,3 +1060,43 @@ int amdxdna_drm_sync_bo_ioctl(struct drm_device *dev, drm_gem_object_put(gobj); return ret; } + +int amdxdna_drm_get_bo_usage(struct drm_device *dev, struct amdxdna_drm_ge= t_array *args) +{ + size_t min_sz =3D min(args->element_size, sizeof(struct amdxdna_drm_bo_us= age)); + char __user *buf =3D u64_to_user_ptr(args->buffer); + struct amdxdna_dev *xdna =3D to_xdna_dev(dev); + struct amdxdna_client *tmp_client; + struct amdxdna_drm_bo_usage tmp; + + drm_WARN_ON(dev, !mutex_is_locked(&xdna->dev_lock)); + + if (args->num_element !=3D 1) + return -EINVAL; + + if (copy_from_user(&tmp, buf, min_sz)) + return -EFAULT; + + if (!tmp.pid) + return -EINVAL; + + tmp.total_usage =3D 0; + tmp.internal_usage =3D 0; + tmp.heap_usage =3D 0; + + list_for_each_entry(tmp_client, &xdna->client_list, node) { + if (tmp.pid !=3D tmp_client->pid) + continue; + + mutex_lock(&tmp_client->mm_lock); + tmp.total_usage +=3D tmp_client->total_bo_usage; + tmp.internal_usage +=3D tmp_client->total_int_bo_usage; + tmp.heap_usage +=3D tmp_client->heap_usage; + mutex_unlock(&tmp_client->mm_lock); + } + + if (copy_to_user(buf, &tmp, min_sz)) + return -EFAULT; + + return 0; +} diff --git a/drivers/accel/amdxdna/amdxdna_gem.h b/drivers/accel/amdxdna/am= dxdna_gem.h index a77d9344f8a4..4fc48a1189d2 100644 --- a/drivers/accel/amdxdna/amdxdna_gem.h +++ b/drivers/accel/amdxdna/amdxdna_gem.h @@ -41,8 +41,9 @@ struct amdxdna_gem_obj { struct amdxdna_client *client; u8 type; bool pinned; - struct mutex lock; /* Protects: pinned, mem.kva */ + struct mutex lock; /* Protects: pinned, mem.kva, open_ref */ struct amdxdna_mem mem; + int open_ref; =20 /* Below members are initialized when needed */ struct drm_mm mm; /* For AMDXDNA_BO_DEV_HEAP */ @@ -50,6 +51,9 @@ struct amdxdna_gem_obj { u32 assigned_hwctx; struct dma_buf *dma_buf; struct dma_buf_attachment *attach; + + /* True, if BO is managed by XRT, not application */ + bool internal; }; =20 #define to_gobj(obj) (&(obj)->base.base) @@ -98,5 +102,6 @@ void amdxdna_gem_unpin(struct amdxdna_gem_obj *abo); int amdxdna_drm_create_bo_ioctl(struct drm_device *dev, void *data, struct= drm_file *filp); int amdxdna_drm_get_bo_info_ioctl(struct drm_device *dev, void *data, stru= ct drm_file *filp); int amdxdna_drm_sync_bo_ioctl(struct drm_device *dev, void *data, struct d= rm_file *filp); +int amdxdna_drm_get_bo_usage(struct drm_device *dev, struct amdxdna_drm_ge= t_array *args); =20 #endif /* _AMDXDNA_GEM_H_ */ diff --git a/drivers/accel/amdxdna/amdxdna_pci_drv.c b/drivers/accel/amdxdn= a/amdxdna_pci_drv.c index d83be00daf2b..b50a7d1f8a11 100644 --- a/drivers/accel/amdxdna/amdxdna_pci_drv.c +++ b/drivers/accel/amdxdna/amdxdna_pci_drv.c @@ -36,9 +36,10 @@ MODULE_FIRMWARE("amdnpu/17f0_11/npu_7.sbin"); * 0.5: Support getting telemetry data * 0.6: Support preemption * 0.7: Support getting power and utilization data + * 0.8: Support BO usage query */ #define AMDXDNA_DRIVER_MAJOR 0 -#define AMDXDNA_DRIVER_MINOR 7 +#define AMDXDNA_DRIVER_MINOR 8 =20 /* * Bind the driver base on (vendor_id, device_id) pair and later use the @@ -120,11 +121,12 @@ static void amdxdna_client_cleanup(struct amdxdna_cli= ent *client) amdxdna_hwctx_remove_all(client); xa_destroy(&client->hwctx_xa); cleanup_srcu_struct(&client->hwctx_srcu); - mutex_destroy(&client->mm_lock); =20 if (client->dev_heap) drm_gem_object_put(to_gobj(client->dev_heap)); =20 + mutex_destroy(&client->mm_lock); + if (!IS_ERR_OR_NULL(client->sva)) iommu_sva_unbind_device(client->sva); mmdrop(client->mm); diff --git a/drivers/accel/amdxdna/amdxdna_pci_drv.h b/drivers/accel/amdxdn= a/amdxdna_pci_drv.h index e91d14ae5190..0661749917d6 100644 --- a/drivers/accel/amdxdna/amdxdna_pci_drv.h +++ b/drivers/accel/amdxdna/amdxdna_pci_drv.h @@ -138,6 +138,10 @@ struct amdxdna_client { struct iommu_sva *sva; int pasid; struct mm_struct *mm; + + size_t heap_usage; + size_t total_bo_usage; + size_t total_int_bo_usage; }; =20 #define amdxdna_for_each_hwctx(client, hwctx_id, entry) \ diff --git a/include/uapi/drm/amdxdna_accel.h b/include/uapi/drm/amdxdna_ac= cel.h index bddaaaf945cf..61d3686fa3b1 100644 --- a/include/uapi/drm/amdxdna_accel.h +++ b/include/uapi/drm/amdxdna_accel.h @@ -591,8 +591,37 @@ struct amdxdna_async_error { __u64 ex_err_code; }; =20 +/** + * struct amdxdna_drm_bo_usage - all types of BO usage + * BOs managed by XRT/SHIM/driver is counted as internal. + * Others are counted as external which are managed by applications. + * + * Among all types of BOs: + * AMDXDNA_BO_DEV_HEAP - is counted for internal. + * AMDXDNA_BO_SHARE - is counted for external. + * AMDXDNA_BO_CMD - is counted for internal. + * AMDXDNA_BO_DEV - is counted by heap_usage only, not internal + * or external. It does not add to the total memory + * footprint since its mem comes from heap which is + * already counted as internal. + */ +struct amdxdna_drm_bo_usage { + /** @pid: The ID of the process to query from. */ + __s64 pid; + /** @total_usage: Total BO size used by process. */ + __u64 total_usage; + /** @internal_usage: Total internal BO size used by process. */ + __u64 internal_usage; + /** @heap_usage: Total device BO size used by process. */ + __u64 heap_usage; +}; + +/* + * Supported params in struct amdxdna_drm_get_array + */ #define DRM_AMDXDNA_HW_CONTEXT_ALL 0 #define DRM_AMDXDNA_HW_LAST_ASYNC_ERR 2 +#define DRM_AMDXDNA_BO_USAGE 6 =20 /** * struct amdxdna_drm_get_array - Get information array. @@ -605,6 +634,12 @@ struct amdxdna_drm_get_array { * * %DRM_AMDXDNA_HW_CONTEXT_ALL: * Returns all created hardware contexts. + * + * %DRM_AMDXDNA_HW_LAST_ASYNC_ERR: + * Returns last async error. + * + * %DRM_AMDXDNA_BO_USAGE: + * Returns usage of heap/internal/external BOs. */ __u32 param; /** --=20 2.34.1