Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 4F7E0E77345 for ; Fri, 29 Sep 2023 22:22:08 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 1F3C410E165; Fri, 29 Sep 2023 22:22:08 +0000 (UTC) Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.136]) by gabe.freedesktop.org (Postfix) with ESMTPS id 6D50A10E165 for ; Fri, 29 Sep 2023 22:22:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1696026126; x=1727562126; h=date:from:to:cc:subject:message-id:references: content-transfer-encoding:in-reply-to:mime-version; bh=LgvLYiGqcYIgAYWH/cA/c/N2imUuzELGo6qRncWBEJk=; b=mOzlz/zPaG13M8iSCxSMTpA6SdpNpLdbhZN7y0JBm6UkVGqeNCZFr75e TcBcd2O3KUfWuC46laPE9L63YUvyJuMi+RvX5kj/VfteLIejCrGbMxhJZ 3rZXfBzQKmjAr0dese7pgFZ/W5HAWPb4alRHJwzxkd+F34lGVU8wQvYqV M/U9ZMKgcJ+0uEbkl6vHmZG3Xtt4pjoZydn+bCuQP4KLpglVk4jymDM/Q PVM0E5OHvzrDPZ6KJYRvt5BER4mB/KPiUbnuVR7GPymKgkDrYD2SvfkMV 7dkGayWylgX5H+mCsm9LqXOdl/vPe4st49HdahrvSBZj3Ws4P5AAzXztn A==; X-IronPort-AV: E=McAfee;i="6600,9927,10848"; a="361774425" X-IronPort-AV: E=Sophos;i="6.03,188,1694761200"; d="scan'208";a="361774425" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Sep 2023 15:22:05 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10848"; a="779402782" X-IronPort-AV: E=Sophos;i="6.03,188,1694761200"; d="scan'208";a="779402782" Received: from fmsmsx602.amr.corp.intel.com ([10.18.126.82]) by orsmga008.jf.intel.com with ESMTP/TLS/AES256-GCM-SHA384; 29 Sep 2023 15:22:04 -0700 Received: from fmsmsx610.amr.corp.intel.com (10.18.126.90) by fmsmsx602.amr.corp.intel.com (10.18.126.82) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.32; Fri, 29 Sep 2023 15:22:04 -0700 Received: from fmsmsx602.amr.corp.intel.com (10.18.126.82) by fmsmsx610.amr.corp.intel.com (10.18.126.90) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.32; Fri, 29 Sep 2023 15:22:04 -0700 Received: from FMSEDG603.ED.cps.intel.com (10.1.192.133) by fmsmsx602.amr.corp.intel.com (10.18.126.82) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.32 via Frontend Transport; Fri, 29 Sep 2023 15:22:04 -0700 Received: from NAM04-DM6-obe.outbound.protection.outlook.com (104.47.73.46) by edgegateway.intel.com (192.55.55.68) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.32; Fri, 29 Sep 2023 15:22:03 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=k/QC0CmjRESVwxtATwxkLhrQGaCHhACPohgZNB7ddT6VQSurHByf7Tf2z3iPq+aRJK0OcA47GQKFY08eb4NsN9G8fhCDadrU7UvOod6Rkkg4s004j+wZOMNre4EAkKrf62zayVEqN6frKBiebGGQFVcwZYokYnz7xHf4kzUYCf1BRuqpimCz/MWXyGazSYdTOP8MufBcTIqlON9Jc1C4TZmYQJgKcXY2Bo0msEmKtmF1/HmYR6CG4KOx5UkK8M9wh7CJAS84IWrcgYeJ1+fhDqOIsSg95xRoj2zog6PYD0wbxzff88RXSOtaKM853+2oEI/8+/KucoEniaTWy/nbnA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; 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=LaX9r/olQT1bdNM+p4tX2gi/Wja6HSpy1vZ7hZd2Zzc=; b=hnH0eGPFfzsjoj71nAKipInIhoaimVZZjtRA3Fl/mYwQ48wGWRY6bXYXUytWLlBD779wzrcX4VqEkqz4BRPtU546umh96xae02QLn7RfB/EZgXHBsBy1xt3rP1+IpRWQ2eP1w8o71CkbKXQPpDJTXwJYlO0jjoiHbpSV0xNuFR+OF3S0mIRZ4Ag8u4dQAPZLlb7lwPKMQ/Mbis16yNV8cHfhVAZ87mCT7Rulpi5nWGo8wSm4lJ01mff+VZbHNMCEpm2gA18lTeLLWou05Y+ChXhsBb0ZfoyEZAvdT6vZ3CanHXwfqhdPI1EdmqR/6I3a3dMYm35rISbUhQvDkgiVDA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=intel.com; dmarc=pass action=none header.from=intel.com; dkim=pass header.d=intel.com; arc=none Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=intel.com; Received: from PH8PR11MB6952.namprd11.prod.outlook.com (2603:10b6:510:224::13) by SA1PR11MB8593.namprd11.prod.outlook.com (2603:10b6:806:3ab::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6813.28; Fri, 29 Sep 2023 22:22:01 +0000 Received: from PH8PR11MB6952.namprd11.prod.outlook.com ([fe80::4182:281:404a:1042]) by PH8PR11MB6952.namprd11.prod.outlook.com ([fe80::4182:281:404a:1042%3]) with mapi id 15.20.6813.017; Fri, 29 Sep 2023 22:22:00 +0000 Date: Fri, 29 Sep 2023 15:21:57 -0700 From: Niranjana Vishwanathapura To: Rodrigo Vivi Message-ID: References: <20230929210130.1203203-1-rodrigo.vivi@intel.com> <20230929210130.1203203-3-rodrigo.vivi@intel.com> Content-Type: text/plain; charset="iso-8859-1"; format=flowed Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20230929210130.1203203-3-rodrigo.vivi@intel.com> X-ClientProxiedBy: BY3PR05CA0032.namprd05.prod.outlook.com (2603:10b6:a03:39b::7) To PH8PR11MB6952.namprd11.prod.outlook.com (2603:10b6:510:224::13) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: PH8PR11MB6952:EE_|SA1PR11MB8593:EE_ X-MS-Office365-Filtering-Correlation-Id: 93f44609-3b9e-4f20-f0ff-08dbc13a800f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: JSt1++A/AGiZvnB5AsNutOYL4UiRSlVMapvIlWW7Z14Zs7gPGM2D5epuszUwJIQJpNEVzD4joFHIgk7ENNTVRhtWMMb6ppxfM7he6xl2DQtJiFbsvGuL/chb3v9ZUn8Q/nW2XfzO7hFgdsx2i3oUPmROLSa5zz1naeBtuAm0gulJOXx5y71mrFSOUDGe2zTrwKLVJ07lghbZZrdJNCsSgUvzZ7jI5vmEGdMOjR5gn81FqBR2ex7Uampoub/0EGuogW9pbNwnM+Ows3DmwDNXdN0X/X+IBdOxOjlHrugUQ6tA6+eMTQWPzV5XZmXxHPHShon4L1Zil7LZ8tX2AiAr8AHeI3BUROh1r5OXE/MDr6RZ42uQB/PXGnY5q/4UL5H1i5K8UNLQJmq932WmT2ozu3IbQZ+4nosU7lYvSjpTFzYha9QprXiA6wMSqxMLRdgYKcJa405MJ9uHesEnqnpaZ8cx1rNJdwxeNoxSptjW40iX5mBfhSfm6cd/StPOVNKIbhFbHhwLJTKwZBYLkEVVdSq/D15RpOMiC6SUmWSh2vWv1EGJFWh9Kdk3N7AKFa1/ X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:PH8PR11MB6952.namprd11.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230031)(7916004)(136003)(366004)(396003)(346002)(376002)(39860400002)(230922051799003)(64100799003)(1800799009)(186009)(451199024)(26005)(33716001)(6506007)(38100700002)(83380400001)(6666004)(44832011)(82960400001)(66946007)(478600001)(6486002)(30864003)(5660300002)(6512007)(66476007)(316002)(6862004)(41300700001)(8676002)(2906002)(9686003)(8936002)(86362001)(6636002)(4326008)(66556008)(559001)(579004); DIR:OUT; SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?iso-8859-1?Q?kqaYDM/rFrP3nThq1aBRksWTl6olL53Q1cVuk2RS981PULPtrQNG17IIUX?= =?iso-8859-1?Q?cK/wWvX2G2e1/oMVQ1Pka6Hmzcep0mXnOVyIxkN1FAVWNekHw5StkezKXQ?= =?iso-8859-1?Q?ZAgkQg/isW79wXQRXSeLT269oK41sirMHWkKn8kCPFVPpibe1X0pcl949R?= =?iso-8859-1?Q?1k9NJK0ZndW3BY3+U+WzdIeLgS2YG3x8m95ocCXM1UYdovmU2pz57DQOnk?= =?iso-8859-1?Q?IopVR+aMBjhuoopE4tlQPDNUnNOYNTQqfg4m+siejtq7VMHAwLU4LNF+Em?= =?iso-8859-1?Q?boySKwXhm0BRAAZdW9pvGYO8UJ3Tbe0uH5kMmarcyfBMwhCNR8YJpYMxCa?= =?iso-8859-1?Q?k1nlRVeeWEPwhvjRWKqQsgVpeQc0KZPdIf/H9OvcnWz7WTgTs7Njfo3Lu4?= =?iso-8859-1?Q?7HObHQImoB3A2OzUtKby9cWhK0H96JASLfsJT4dHqc6Tqk2Is4Y/iniWaP?= =?iso-8859-1?Q?KF3m+SHDhhQ71ekiCsI9xr2HlWKjfpHZhsVXGGz2wcu3/7jtjOu9X7xVeD?= =?iso-8859-1?Q?A7+8KDShE3fj2Ym6kd50sgDKBhPqlzXq8+VXfkUbSf1YBFeDyIadSe7QUw?= =?iso-8859-1?Q?0wwpKYxqrkj8fvh0L+XUfuxGqxAIHLG2E/88uh9EYccJ5V/GoGY2heIU35?= =?iso-8859-1?Q?fG2B/AHGlhiZ60LKr+zDN3484vDocscJ6L3FtXZGN5f4oda4YzrvQ6n5Eg?= =?iso-8859-1?Q?VHAAhGavKgCoBtqM8WQXFYqD/xNCb9w3bjn1oVG+DXiI64EnaJA7582MKU?= =?iso-8859-1?Q?kbOe6erlhz3IVn4/+GH8VVaLdnSrR3iHTqPxbHSJFh6G+3yIzsxBi4Qqxq?= =?iso-8859-1?Q?AQzUxlCRVmNThbA2NaH7KSzaoGy/yQ16mGDgZ4A5A14QSnWpBhWKxQVTHb?= =?iso-8859-1?Q?3idCUW1rJvUV6dpa5J9SZLt2g6OCocMe2HTgdhdo8YG7PJVMtWuYSd3ZDs?= =?iso-8859-1?Q?u71F+1afI/1VTL/4MgoKn6QCJ/3SCS/SPd9pOBuoCUzkUy8IE9eb4ib3Ph?= =?iso-8859-1?Q?LnEowX1MKF1wB5JkByL8hLK7jH9GVpKF4rHJDR+h9nYdRcnkvb9m2INgAa?= =?iso-8859-1?Q?eowpayastT4GWjvJnN/v2ENFDaXAXu6dCOP4QR5a9UyUf5oqvz+gyOTMqp?= =?iso-8859-1?Q?5Jz75P/Vk1YN9IUWhLAwJcDpBVES2WFJmRyTtK+JE0LgsD8DHPI+t+VBpI?= =?iso-8859-1?Q?AVDuD64kmDOZzhB4eiJGQQFelp5QDXsZ7N00GfzJFOCMex9uFxo7thRdhH?= =?iso-8859-1?Q?z2eJRqKO5FluJuuO43Ta+YVO71v5a3RoWuaaBKoDuBcx0mamw2PoACzkv6?= =?iso-8859-1?Q?tBYCdga9+xJXOJZvvd9w41Ik0mvLD+h6yKcn7WTL5nUOsD1F7K/mg+1Yga?= =?iso-8859-1?Q?JcDS2nGtbVH7AUIurVQIght5zuUH68tNFHSrgQ5EDYoG5Y1Xo14+wLEkdj?= =?iso-8859-1?Q?hZpz++TfhAsitPE+sJ2U4HxlRU/k4hxlRqg7GnFoUZ2dPPJVqo+qrEtcPf?= =?iso-8859-1?Q?jeESp58gUpMTDyJPu0Zsy9UdKglhKKJVgc/8hY24vALfJlae6mdaxOtYhT?= =?iso-8859-1?Q?EBBzQ9o67AQECm6CQzCiUHqEf2NOYnLqUUNREqvUv1xc1FZSq/58Tpxq1b?= =?iso-8859-1?Q?xPqhjmK55Ha/p9uY7Cy+3WiAA3GKUBIC0q/kZPxD6Y9/pf2OAOYnlE672o?= =?iso-8859-1?Q?fIihejCTw3hgxFekmfo=3D?= X-MS-Exchange-CrossTenant-Network-Message-Id: 93f44609-3b9e-4f20-f0ff-08dbc13a800f X-MS-Exchange-CrossTenant-AuthSource: PH8PR11MB6952.namprd11.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 29 Sep 2023 22:22:00.6031 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 46c98d88-e344-4ed4-8496-4ed7712e255d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: h5sNLHigG04P4wGNUMNo2CUQrjp9z5CuipyubZyfuvrXPBHe5wvriLf48/rKsSM0JMlBR/pP/4vWqVgB8eXTKlFQWsYbCH05tbkR/BmTYm2zXddinhLt0Q/bxiLLlvd9 X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA1PR11MB8593 X-OriginatorOrg: intel.com Subject: Re: [Intel-xe] [PATCH 3/3] drm/xe: Kill xe_vm_doc.h X-BeenThere: intel-xe@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Intel Xe graphics driver List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: intel-xe@lists.freedesktop.org Errors-To: intel-xe-bounces@lists.freedesktop.org Sender: "Intel-xe" On Fri, Sep 29, 2023 at 05:01:28PM -0400, Rodrigo Vivi wrote: >Keep the documentation inside the code. >No update to the documentation itself at this time. >Let's first ensure that the entire documentation is closer >to the relevant code, otherwise it will get very easily outdated. > Currently it is not included in any rst file, hence it is not rendering. While we are at it, can you create a rst file/section for this? Niranjana >Signed-off-by: Rodrigo Vivi >--- > drivers/gpu/drm/xe/xe_vm.c | 546 ++++++++++++++++++++++++++++++++ > drivers/gpu/drm/xe/xe_vm_doc.h | 555 --------------------------------- > 2 files changed, 546 insertions(+), 555 deletions(-) > delete mode 100644 drivers/gpu/drm/xe/xe_vm_doc.h > >diff --git a/drivers/gpu/drm/xe/xe_vm.c b/drivers/gpu/drm/xe/xe_vm.c >index 57ffac324564..b00d1476e91c 100644 >--- a/drivers/gpu/drm/xe/xe_vm.c >+++ b/drivers/gpu/drm/xe/xe_vm.c >@@ -34,6 +34,552 @@ > #include "generated/xe_wa_oob.h" > #include "xe_wa.h" > >+/** >+ * DOC: XE VM (user address space) >+ * >+ * VM creation >+ * =========== >+ * >+ * Allocate a physical page for root of the page table structure, create default >+ * bind engine, and return a handle to the user. >+ * >+ * Scratch page >+ * ------------ >+ * >+ * If the VM is created with the flag, DRM_XE_VM_CREATE_SCRATCH_PAGE, set the >+ * entire page table structure defaults pointing to blank page allocated by the >+ * VM. Invalid memory access rather than fault just read / write to this page. >+ * >+ * VM bind (create GPU mapping for a BO or userptr) >+ * ================================================ >+ * >+ * Creates GPU mapings for a BO or userptr within a VM. VM binds uses the same >+ * in / out fence interface (struct drm_xe_sync) as execs which allows users to >+ * think of binds and execs as more or less the same operation. >+ * >+ * Operations >+ * ---------- >+ * >+ * XE_VM_BIND_OP_MAP - Create mapping for a BO >+ * XE_VM_BIND_OP_UNMAP - Destroy mapping for a BO / userptr >+ * XE_VM_BIND_OP_MAP_USERPTR - Create mapping for userptr >+ * >+ * Implementation details >+ * ~~~~~~~~~~~~~~~~~~~~~~ >+ * >+ * All bind operations are implemented via a hybrid approach of using the CPU >+ * and GPU to modify page tables. If a new physical page is allocated in the >+ * page table structure we populate that page via the CPU and insert that new >+ * page into the existing page table structure via a GPU job. Also any existing >+ * pages in the page table structure that need to be modified also are updated >+ * via the GPU job. As the root physical page is prealloced on VM creation our >+ * GPU job will always have at least 1 update. The in / out fences are passed to >+ * this job so again this is conceptually the same as an exec. >+ * >+ * Very simple example of few binds on an empty VM with 48 bits of address space >+ * and the resulting operations: >+ * >+ * .. code-block:: >+ * >+ * bind BO0 0x0-0x1000 >+ * alloc page level 3a, program PTE[0] to BO0 phys address (CPU) >+ * alloc page level 2, program PDE[0] page level 3a phys address (CPU) >+ * alloc page level 1, program PDE[0] page level 2 phys address (CPU) >+ * update root PDE[0] to page level 1 phys address (GPU) >+ * >+ * bind BO1 0x201000-0x202000 >+ * alloc page level 3b, program PTE[1] to BO1 phys address (CPU) >+ * update page level 2 PDE[1] to page level 3b phys address (GPU) >+ * >+ * bind BO2 0x1ff000-0x201000 >+ * update page level 3a PTE[511] to BO2 phys addres (GPU) >+ * update page level 3b PTE[0] to BO2 phys addres + 0x1000 (GPU) >+ * >+ * GPU bypass >+ * ~~~~~~~~~~ >+ * >+ * In the above example the steps using the GPU can be converted to CPU if the >+ * bind can be done immediately (all in-fences satisfied, VM dma-resv kernel >+ * slot is idle). >+ * >+ * Address space >+ * ------------- >+ * >+ * Depending on platform either 48 or 57 bits of address space is supported. >+ * >+ * Page sizes >+ * ---------- >+ * >+ * The minimum page size is either 4k or 64k depending on platform and memory >+ * placement (sysmem vs. VRAM). We enforce that binds must be aligned to the >+ * minimum page size. >+ * >+ * Larger pages (2M or 1GB) can be used for BOs in VRAM, the BO physical address >+ * is aligned to the larger pages size, and VA is aligned to the larger page >+ * size. Larger pages for userptrs / BOs in sysmem should be possible but is not >+ * yet implemented. >+ * >+ * Sync error handling mode >+ * ------------------------ >+ * >+ * In both modes during the bind IOCTL the user input is validated. In sync >+ * error handling mode the newly bound BO is validated (potentially moved back >+ * to a region of memory where is can be used), page tables are updated by the >+ * CPU and the job to do the GPU binds is created in the IOCTL itself. This step >+ * can fail due to memory pressure. The user can recover by freeing memory and >+ * trying this operation again. >+ * >+ * Async error handling mode >+ * ------------------------- >+ * >+ * In async error handling the step of validating the BO, updating page tables, >+ * and generating a job are deferred to an async worker. As this step can now >+ * fail after the IOCTL has reported success we need an error handling flow for >+ * which the user can recover from. >+ * >+ * The solution is for a user to register a user address with the VM which the >+ * VM uses to report errors to. The ufence wait interface can be used to wait on >+ * a VM going into an error state. Once an error is reported the VM's async >+ * worker is paused. While the VM's async worker is paused sync, >+ * XE_VM_BIND_OP_UNMAP operations are allowed (this can free memory). Once the >+ * uses believe the error state is fixed, the async worker can be resumed via >+ * XE_VM_BIND_OP_RESTART operation. When VM async bind work is restarted, the >+ * first operation processed is the operation that caused the original error. >+ * >+ * Bind queues / engines >+ * --------------------- >+ * >+ * Think of the case where we have two bind operations A + B and are submitted >+ * in that order. A has in fences while B has none. If using a single bind >+ * queue, B is now blocked on A's in fences even though it is ready to run. This >+ * example is a real use case for VK sparse binding. We work around this >+ * limitation by implementing bind engines. >+ * >+ * In the bind IOCTL the user can optionally pass in an engine ID which must map >+ * to an engine which is of the special class DRM_XE_ENGINE_CLASS_VM_BIND. >+ * Underneath this is a really virtual engine that can run on any of the copy >+ * hardware engines. The job(s) created each IOCTL are inserted into this >+ * engine's ring. In the example above if A and B have different bind engines B >+ * is free to pass A. If the engine ID field is omitted, the default bind queue >+ * for the VM is used. >+ * >+ * TODO: Explain race in issue 41 and how we solve it >+ * >+ * Array of bind operations >+ * ------------------------ >+ * >+ * The uAPI allows multiple binds operations to be passed in via a user array, >+ * of struct drm_xe_vm_bind_op, in a single VM bind IOCTL. This interface >+ * matches the VK sparse binding API. The implementation is rather simple, parse >+ * the array into a list of operations, pass the in fences to the first operation, >+ * and pass the out fences to the last operation. The ordered nature of a bind >+ * engine makes this possible. >+ * >+ * Munmap semantics for unbinds >+ * ---------------------------- >+ * >+ * Munmap allows things like: >+ * >+ * .. code-block:: >+ * >+ * 0x0000-0x2000 and 0x3000-0x5000 have mappings >+ * Munmap 0x1000-0x4000, results in mappings 0x0000-0x1000 and 0x4000-0x5000 >+ * >+ * To support this semantic in the above example we decompose the above example >+ * into 4 operations: >+ * >+ * .. code-block:: >+ * >+ * unbind 0x0000-0x2000 >+ * unbind 0x3000-0x5000 >+ * rebind 0x0000-0x1000 >+ * rebind 0x4000-0x5000 >+ * >+ * Why not just do a partial unbind of 0x1000-0x2000 and 0x3000-0x4000? This >+ * falls apart when using large pages at the edges and the unbind forces us to >+ * use a smaller page size. For simplity we always issue a set of unbinds >+ * unmapping anything in the range and at most 2 rebinds on the edges. >+ * >+ * Similar to an array of binds, in fences are passed to the first operation and >+ * out fences are signaled on the last operation. >+ * >+ * In this example there is a window of time where 0x0000-0x1000 and >+ * 0x4000-0x5000 are invalid but the user didn't ask for these addresses to be >+ * removed from the mapping. To work around this we treat any munmap style >+ * unbinds which require a rebind as a kernel operations (BO eviction or userptr >+ * invalidation). The first operation waits on the VM's >+ * DMA_RESV_USAGE_PREEMPT_FENCE slots (waits for all pending jobs on VM to >+ * complete / triggers preempt fences) and the last operation is installed in >+ * the VM's DMA_RESV_USAGE_KERNEL slot (blocks future jobs / resume compute mode >+ * VM). The caveat is all dma-resv slots must be updated atomically with respect >+ * to execs and compute mode rebind worker. To accomplish this, hold the >+ * vm->lock in write mode from the first operation until the last. >+ * >+ * Deferred binds in fault mode >+ * ---------------------------- >+ * >+ * In a VM is in fault mode (TODO: link to fault mode), new bind operations that >+ * create mappings are by default are deferred to the page fault handler (first >+ * use). This behavior can be overriden by setting the flag >+ * XE_VM_BIND_FLAG_IMMEDIATE which indicates to creating the mapping >+ * immediately. >+ * >+ * User pointer >+ * ============ >+ * >+ * User pointers are user allocated memory (malloc'd, mmap'd, etc..) for which the >+ * user wants to create a GPU mapping. Typically in other DRM drivers a dummy BO >+ * was created and then a binding was created. We bypass creating a dummy BO in >+ * XE and simply create a binding directly from the userptr. >+ * >+ * Invalidation >+ * ------------ >+ * >+ * Since this a core kernel managed memory the kernel can move this memory >+ * whenever it wants. We register an invalidation MMU notifier to alert XE when >+ * a user poiter is about to move. The invalidation notifier needs to block >+ * until all pending users (jobs or compute mode engines) of the userptr are >+ * idle to ensure no faults. This done by waiting on all of VM's dma-resv slots. >+ * >+ * Rebinds >+ * ------- >+ * >+ * Either the next exec (non-compute) or rebind worker (compute mode) will >+ * rebind the userptr. The invalidation MMU notifier kicks the rebind worker >+ * after the VM dma-resv wait if the VM is in compute mode. >+ * >+ * Compute mode >+ * ============ >+ * >+ * A VM in compute mode enables long running workloads and ultra low latency >+ * submission (ULLS). ULLS is implemented via a continuously running batch + >+ * semaphores. This enables to the user to insert jump to new batch commands >+ * into the continuously running batch. In both cases these batches exceed the >+ * time a dma fence is allowed to exist for before signaling, as such dma fences >+ * are not used when a VM is in compute mode. User fences (TODO: link user fence >+ * doc) are used instead to signal operation's completion. >+ * >+ * Preempt fences >+ * -------------- >+ * >+ * If the kernel decides to move memory around (either userptr invalidate, BO >+ * eviction, or mumap style unbind which results in a rebind) and a batch is >+ * running on an engine, that batch can fault or cause a memory corruption as >+ * page tables for the moved memory are no longer valid. To work around this we >+ * introduce the concept of preempt fences. When sw signaling is enabled on a >+ * preempt fence it tells the submission backend to kick that engine off the >+ * hardware and the preempt fence signals when the engine is off the hardware. >+ * Once all preempt fences are signaled for a VM the kernel can safely move the >+ * memory and kick the rebind worker which resumes all the engines execution. >+ * >+ * A preempt fence, for every engine using the VM, is installed the VM's >+ * dma-resv DMA_RESV_USAGE_PREEMPT_FENCE slot. The same preempt fence, for every >+ * engine using the VM, is also installed into the same dma-resv slot of every >+ * external BO mapped in the VM. >+ * >+ * Rebind worker >+ * ------------- >+ * >+ * The rebind worker is very similar to an exec. It is resposible for rebinding >+ * evicted BOs or userptrs, waiting on those operations, installing new preempt >+ * fences, and finally resuming executing of engines in the VM. >+ * >+ * Flow >+ * ~~~~ >+ * >+ * .. code-block:: >+ * >+ * <----------------------------------------------------------------------| >+ * Check if VM is closed, if so bail out | >+ * Lock VM global lock in read mode | >+ * Pin userptrs (also finds userptr invalidated since last rebind worker) | >+ * Lock VM dma-resv and external BOs dma-resv | >+ * Validate BOs that have been evicted | >+ * Wait on and allocate new preempt fences for every engine using the VM | >+ * Rebind invalidated userptrs + evicted BOs | >+ * Wait on last rebind fence | >+ * Wait VM's DMA_RESV_USAGE_KERNEL dma-resv slot | >+ * Install preeempt fences and issue resume for every engine using the VM | >+ * Check if any userptrs invalidated since pin | >+ * Squash resume for all engines | >+ * Unlock all | >+ * Wait all VM's dma-resv slots | >+ * Retry ---------------------------------------------------------- >+ * Release all engines waiting to resume >+ * Unlock all >+ * >+ * Timeslicing >+ * ----------- >+ * >+ * In order to prevent an engine from continuously being kicked off the hardware >+ * and making no forward progress an engine has a period of time it allowed to >+ * run after resume before it can be kicked off again. This effectively gives >+ * each engine a timeslice. >+ * >+ * Handling multiple GTs >+ * ===================== >+ * >+ * If a GT has slower access to some regions and the page table structure are in >+ * the slow region, the performance on that GT could adversely be affected. To >+ * work around this we allow a VM page tables to be shadowed in multiple GTs. >+ * When VM is created, a default bind engine and PT table structure are created >+ * on each GT. >+ * >+ * Binds can optionally pass in a mask of GTs where a mapping should be created, >+ * if this mask is zero then default to all the GTs where the VM has page >+ * tables. >+ * >+ * The implementation for this breaks down into a bunch for_each_gt loops in >+ * various places plus exporting a composite fence for multi-GT binds to the >+ * user. >+ * >+ * Fault mode (unified shared memory) >+ * ================================== >+ * >+ * A VM in fault mode can be enabled on devices that support page faults. If >+ * page faults are enabled, using dma fences can potentially induce a deadlock: >+ * A pending page fault can hold up the GPU work which holds up the dma fence >+ * signaling, and memory allocation is usually required to resolve a page >+ * fault, but memory allocation is not allowed to gate dma fence signaling. As >+ * such, dma fences are not allowed when VM is in fault mode. Because dma-fences >+ * are not allowed, long running workloads and ULLS are enabled on a faulting >+ * VM. >+ * >+ * Defered VM binds >+ * ---------------- >+ * >+ * By default, on a faulting VM binds just allocate the VMA and the actual >+ * updating of the page tables is defered to the page fault handler. This >+ * behavior can be overridden by setting the flag XE_VM_BIND_FLAG_IMMEDIATE in >+ * the VM bind which will then do the bind immediately. >+ * >+ * Page fault handler >+ * ------------------ >+ * >+ * Page faults are received in the G2H worker under the CT lock which is in the >+ * path of dma fences (no memory allocations are allowed, faults require memory >+ * allocations) thus we cannot process faults under the CT lock. Another issue >+ * is faults issue TLB invalidations which require G2H credits and we cannot >+ * allocate G2H credits in the G2H handlers without deadlocking. Lastly, we do >+ * not want the CT lock to be an outer lock of the VM global lock (VM global >+ * lock required to fault processing). >+ * >+ * To work around the above issue with processing faults in the G2H worker, we >+ * sink faults to a buffer which is large enough to sink all possible faults on >+ * the GT (1 per hardware engine) and kick a worker to process the faults. Since >+ * the page faults G2H are already received in a worker, kicking another worker >+ * adds more latency to a critical performance path. We add a fast path in the >+ * G2H irq handler which looks at first G2H and if it is a page fault we sink >+ * the fault to the buffer and kick the worker to process the fault. TLB >+ * invalidation responses are also in the critical path so these can also be >+ * processed in this fast path. >+ * >+ * Multiple buffers and workers are used and hashed over based on the ASID so >+ * faults from different VMs can be processed in parallel. >+ * >+ * The page fault handler itself is rather simple, flow is below. >+ * >+ * .. code-block:: >+ * >+ * Lookup VM from ASID in page fault G2H >+ * Lock VM global lock in read mode >+ * Lookup VMA from address in page fault G2H >+ * Check if VMA is valid, if not bail >+ * Check if VMA's BO has backing store, if not allocate >+ * <----------------------------------------------------------------------| >+ * If userptr, pin pages | >+ * Lock VM & BO dma-resv locks | >+ * If atomic fault, migrate to VRAM, else validate BO location | >+ * Issue rebind | >+ * Wait on rebind to complete | >+ * Check if userptr invalidated since pin | >+ * Drop VM & BO dma-resv locks | >+ * Retry ---------------------------------------------------------- >+ * Unlock all >+ * Issue blocking TLB invalidation | >+ * Send page fault response to GuC >+ * >+ * Access counters >+ * --------------- >+ * >+ * Access counters can be configured to trigger a G2H indicating the device is >+ * accessing VMAs in system memory frequently as hint to migrate those VMAs to >+ * VRAM. >+ * >+ * Same as the page fault handler, access counters G2H cannot be processed the >+ * G2H worker under the CT lock. Again we use a buffer to sink access counter >+ * G2H. Unlike page faults there is no upper bound so if the buffer is full we >+ * simply drop the G2H. Access counters are a best case optimization and it is >+ * safe to drop these unlike page faults. >+ * >+ * The access counter handler itself is rather simple flow is below. >+ * >+ * .. code-block:: >+ * >+ * Lookup VM from ASID in access counter G2H >+ * Lock VM global lock in read mode >+ * Lookup VMA from address in access counter G2H >+ * If userptr, bail nothing to do >+ * Lock VM & BO dma-resv locks >+ * Issue migration to VRAM >+ * Unlock all >+ * >+ * Notice no rebind is issued in the access counter handler as the rebind will >+ * be issued on next page fault. >+ * >+ * Cavets with eviction / user pointer invalidation >+ * ------------------------------------------------ >+ * >+ * In the case of eviction and user pointer invalidation on a faulting VM, there >+ * is no need to issue a rebind rather we just need to blow away the page tables >+ * for the VMAs and the page fault handler will rebind the VMAs when they fault. >+ * The cavet is to update / read the page table structure the VM global lock is >+ * neeeed. In both the case of eviction and user pointer invalidation locks are >+ * held which make acquiring the VM global lock impossible. To work around this >+ * every VMA maintains a list of leaf page table entries which should be written >+ * to zero to blow away the VMA's page tables. After writing zero to these >+ * entries a blocking TLB invalidate is issued. At this point it is safe for the >+ * kernel to move the VMA's memory around. This is a necessary lockless >+ * algorithm and is safe as leafs cannot be changed while either an eviction or >+ * userptr invalidation is occurring. >+ * >+ * Locking >+ * ======= >+ * >+ * VM locking protects all of the core data paths (bind operations, execs, >+ * evictions, and compute mode rebind worker) in XE. >+ * >+ * Locks >+ * ----- >+ * >+ * VM global lock (vm->lock) - rw semaphore lock. Outer most lock which protects >+ * the list of userptrs mapped in the VM, the list of engines using this VM, and >+ * the array of external BOs mapped in the VM. When adding or removing any of the >+ * aforemented state from the VM should acquire this lock in write mode. The VM >+ * bind path also acquires this lock in write while the exec / compute mode >+ * rebind worker acquire this lock in read mode. >+ * >+ * VM dma-resv lock (vm->ttm.base.resv->lock) - WW lock. Protects VM dma-resv >+ * slots which is shared with any private BO in the VM. Expected to be acquired >+ * during VM binds, execs, and compute mode rebind worker. This lock is also >+ * held when private BOs are being evicted. >+ * >+ * external BO dma-resv lock (bo->ttm.base.resv->lock) - WW lock. Protects >+ * external BO dma-resv slots. Expected to be acquired during VM binds (in >+ * addition to the VM dma-resv lock). All external BO dma-locks within a VM are >+ * expected to be acquired (in addition to the VM dma-resv lock) during execs >+ * and the compute mode rebind worker. This lock is also held when an external >+ * BO is being evicted. >+ * >+ * Putting it all together >+ * ----------------------- >+ * >+ * 1. An exec and bind operation with the same VM can't be executing at the same >+ * time (vm->lock). >+ * >+ * 2. A compute mode rebind worker and bind operation with the same VM can't be >+ * executing at the same time (vm->lock). >+ * >+ * 3. We can't add / remove userptrs or external BOs to a VM while an exec with >+ * the same VM is executing (vm->lock). >+ * >+ * 4. We can't add / remove userptrs, external BOs, or engines to a VM while a >+ * compute mode rebind worker with the same VM is executing (vm->lock). >+ * >+ * 5. Evictions within a VM can't be happen while an exec with the same VM is >+ * executing (dma-resv locks). >+ * >+ * 6. Evictions within a VM can't be happen while a compute mode rebind worker >+ * with the same VM is executing (dma-resv locks). >+ * >+ * dma-resv usage >+ * ============== >+ * >+ * As previously stated to enforce the ordering of kernel ops (eviction, userptr >+ * invalidation, munmap style unbinds which result in a rebind), rebinds during >+ * execs, execs, and resumes in the rebind worker we use both the VMs and >+ * external BOs dma-resv slots. Let try to make this as clear as possible. >+ * >+ * Slot installation >+ * ----------------- >+ * >+ * 1. Jobs from kernel ops install themselves into the DMA_RESV_USAGE_KERNEL >+ * slot of either an external BO or VM (depends on if kernel op is operating on >+ * an external or private BO) >+ * >+ * 2. In non-compute mode, jobs from execs install themselves into the >+ * DMA_RESV_USAGE_BOOKKEEP slot of the VM >+ * >+ * 3. In non-compute mode, jobs from execs install themselves into the >+ * DMA_RESV_USAGE_WRITE slot of all external BOs in the VM >+ * >+ * 4. Jobs from binds install themselves into the DMA_RESV_USAGE_BOOKKEEP slot >+ * of the VM >+ * >+ * 5. Jobs from binds install themselves into the DMA_RESV_USAGE_BOOKKEEP slot >+ * of the external BO (if the bind is to an external BO, this is addition to #4) >+ * >+ * 6. Every engine using a compute mode VM has a preempt fence in installed into >+ * the DMA_RESV_USAGE_PREEMPT_FENCE slot of the VM >+ * >+ * 7. Every engine using a compute mode VM has a preempt fence in installed into >+ * the DMA_RESV_USAGE_PREEMPT_FENCE slot of all the external BOs in the VM >+ * >+ * Slot waiting >+ * ------------ >+ * >+ * 1. The exection of all jobs from kernel ops shall wait on all slots >+ * (DMA_RESV_USAGE_PREEMPT_FENCE) of either an external BO or VM (depends on if >+ * kernel op is operating on external or private BO) >+ * >+ * 2. In non-compute mode, the exection of all jobs from rebinds in execs shall >+ * wait on the DMA_RESV_USAGE_KERNEL slot of either an external BO or VM >+ * (depends on if the rebind is operatiing on an external or private BO) >+ * >+ * 3. In non-compute mode, the exection of all jobs from execs shall wait on the >+ * last rebind job >+ * >+ * 4. In compute mode, the exection of all jobs from rebinds in the rebind >+ * worker shall wait on the DMA_RESV_USAGE_KERNEL slot of either an external BO >+ * or VM (depends on if rebind is operating on external or private BO) >+ * >+ * 5. In compute mode, resumes in rebind worker shall wait on last rebind fence >+ * >+ * 6. In compute mode, resumes in rebind worker shall wait on the >+ * DMA_RESV_USAGE_KERNEL slot of the VM >+ * >+ * Putting it all together >+ * ----------------------- >+ * >+ * 1. New jobs from kernel ops are blocked behind any existing jobs from >+ * non-compute mode execs >+ * >+ * 2. New jobs from non-compute mode execs are blocked behind any existing jobs >+ * from kernel ops and rebinds >+ * >+ * 3. New jobs from kernel ops are blocked behind all preempt fences signaling in >+ * compute mode >+ * >+ * 4. Compute mode engine resumes are blocked behind any existing jobs from >+ * kernel ops and rebinds >+ * >+ * Future work >+ * =========== >+ * >+ * Support large pages for sysmem and userptr. >+ * >+ * Update page faults to handle BOs are page level grainularity (e.g. part of BO >+ * could be in system memory while another part could be in VRAM). >+ * >+ * Page fault handler likely we be optimized a bit more (e.g. Rebinds always >+ * wait on the dma-resv kernel slots of VM or BO, technically we only have to >+ * wait the BO moving. If using a job to do the rebind, we could not block in >+ * the page fault handler rather attach a callback to fence of the rebind job to >+ * signal page fault complete. Our handling of short circuting for atomic faults >+ * for bound VMAs could be better. etc...). We can tune all of this once we have >+ * benchmarks / performance number from workloads up and running. >+ */ >+ > #define TEST_VM_ASYNC_OPS_ERROR > > /** >diff --git a/drivers/gpu/drm/xe/xe_vm_doc.h b/drivers/gpu/drm/xe/xe_vm_doc.h >deleted file mode 100644 >index b1b2dc4a6089..000000000000 >--- a/drivers/gpu/drm/xe/xe_vm_doc.h >+++ /dev/null >@@ -1,555 +0,0 @@ >-/* SPDX-License-Identifier: MIT */ >-/* >- * Copyright © 2022 Intel Corporation >- */ >- >-#ifndef _XE_VM_DOC_H_ >-#define _XE_VM_DOC_H_ >- >-/** >- * DOC: XE VM (user address space) >- * >- * VM creation >- * =========== >- * >- * Allocate a physical page for root of the page table structure, create default >- * bind engine, and return a handle to the user. >- * >- * Scratch page >- * ------------ >- * >- * If the VM is created with the flag, DRM_XE_VM_CREATE_SCRATCH_PAGE, set the >- * entire page table structure defaults pointing to blank page allocated by the >- * VM. Invalid memory access rather than fault just read / write to this page. >- * >- * VM bind (create GPU mapping for a BO or userptr) >- * ================================================ >- * >- * Creates GPU mapings for a BO or userptr within a VM. VM binds uses the same >- * in / out fence interface (struct drm_xe_sync) as execs which allows users to >- * think of binds and execs as more or less the same operation. >- * >- * Operations >- * ---------- >- * >- * XE_VM_BIND_OP_MAP - Create mapping for a BO >- * XE_VM_BIND_OP_UNMAP - Destroy mapping for a BO / userptr >- * XE_VM_BIND_OP_MAP_USERPTR - Create mapping for userptr >- * >- * Implementation details >- * ~~~~~~~~~~~~~~~~~~~~~~ >- * >- * All bind operations are implemented via a hybrid approach of using the CPU >- * and GPU to modify page tables. If a new physical page is allocated in the >- * page table structure we populate that page via the CPU and insert that new >- * page into the existing page table structure via a GPU job. Also any existing >- * pages in the page table structure that need to be modified also are updated >- * via the GPU job. As the root physical page is prealloced on VM creation our >- * GPU job will always have at least 1 update. The in / out fences are passed to >- * this job so again this is conceptually the same as an exec. >- * >- * Very simple example of few binds on an empty VM with 48 bits of address space >- * and the resulting operations: >- * >- * .. code-block:: >- * >- * bind BO0 0x0-0x1000 >- * alloc page level 3a, program PTE[0] to BO0 phys address (CPU) >- * alloc page level 2, program PDE[0] page level 3a phys address (CPU) >- * alloc page level 1, program PDE[0] page level 2 phys address (CPU) >- * update root PDE[0] to page level 1 phys address (GPU) >- * >- * bind BO1 0x201000-0x202000 >- * alloc page level 3b, program PTE[1] to BO1 phys address (CPU) >- * update page level 2 PDE[1] to page level 3b phys address (GPU) >- * >- * bind BO2 0x1ff000-0x201000 >- * update page level 3a PTE[511] to BO2 phys addres (GPU) >- * update page level 3b PTE[0] to BO2 phys addres + 0x1000 (GPU) >- * >- * GPU bypass >- * ~~~~~~~~~~ >- * >- * In the above example the steps using the GPU can be converted to CPU if the >- * bind can be done immediately (all in-fences satisfied, VM dma-resv kernel >- * slot is idle). >- * >- * Address space >- * ------------- >- * >- * Depending on platform either 48 or 57 bits of address space is supported. >- * >- * Page sizes >- * ---------- >- * >- * The minimum page size is either 4k or 64k depending on platform and memory >- * placement (sysmem vs. VRAM). We enforce that binds must be aligned to the >- * minimum page size. >- * >- * Larger pages (2M or 1GB) can be used for BOs in VRAM, the BO physical address >- * is aligned to the larger pages size, and VA is aligned to the larger page >- * size. Larger pages for userptrs / BOs in sysmem should be possible but is not >- * yet implemented. >- * >- * Sync error handling mode >- * ------------------------ >- * >- * In both modes during the bind IOCTL the user input is validated. In sync >- * error handling mode the newly bound BO is validated (potentially moved back >- * to a region of memory where is can be used), page tables are updated by the >- * CPU and the job to do the GPU binds is created in the IOCTL itself. This step >- * can fail due to memory pressure. The user can recover by freeing memory and >- * trying this operation again. >- * >- * Async error handling mode >- * ------------------------- >- * >- * In async error handling the step of validating the BO, updating page tables, >- * and generating a job are deferred to an async worker. As this step can now >- * fail after the IOCTL has reported success we need an error handling flow for >- * which the user can recover from. >- * >- * The solution is for a user to register a user address with the VM which the >- * VM uses to report errors to. The ufence wait interface can be used to wait on >- * a VM going into an error state. Once an error is reported the VM's async >- * worker is paused. While the VM's async worker is paused sync, >- * XE_VM_BIND_OP_UNMAP operations are allowed (this can free memory). Once the >- * uses believe the error state is fixed, the async worker can be resumed via >- * XE_VM_BIND_OP_RESTART operation. When VM async bind work is restarted, the >- * first operation processed is the operation that caused the original error. >- * >- * Bind queues / engines >- * --------------------- >- * >- * Think of the case where we have two bind operations A + B and are submitted >- * in that order. A has in fences while B has none. If using a single bind >- * queue, B is now blocked on A's in fences even though it is ready to run. This >- * example is a real use case for VK sparse binding. We work around this >- * limitation by implementing bind engines. >- * >- * In the bind IOCTL the user can optionally pass in an engine ID which must map >- * to an engine which is of the special class DRM_XE_ENGINE_CLASS_VM_BIND. >- * Underneath this is a really virtual engine that can run on any of the copy >- * hardware engines. The job(s) created each IOCTL are inserted into this >- * engine's ring. In the example above if A and B have different bind engines B >- * is free to pass A. If the engine ID field is omitted, the default bind queue >- * for the VM is used. >- * >- * TODO: Explain race in issue 41 and how we solve it >- * >- * Array of bind operations >- * ------------------------ >- * >- * The uAPI allows multiple binds operations to be passed in via a user array, >- * of struct drm_xe_vm_bind_op, in a single VM bind IOCTL. This interface >- * matches the VK sparse binding API. The implementation is rather simple, parse >- * the array into a list of operations, pass the in fences to the first operation, >- * and pass the out fences to the last operation. The ordered nature of a bind >- * engine makes this possible. >- * >- * Munmap semantics for unbinds >- * ---------------------------- >- * >- * Munmap allows things like: >- * >- * .. code-block:: >- * >- * 0x0000-0x2000 and 0x3000-0x5000 have mappings >- * Munmap 0x1000-0x4000, results in mappings 0x0000-0x1000 and 0x4000-0x5000 >- * >- * To support this semantic in the above example we decompose the above example >- * into 4 operations: >- * >- * .. code-block:: >- * >- * unbind 0x0000-0x2000 >- * unbind 0x3000-0x5000 >- * rebind 0x0000-0x1000 >- * rebind 0x4000-0x5000 >- * >- * Why not just do a partial unbind of 0x1000-0x2000 and 0x3000-0x4000? This >- * falls apart when using large pages at the edges and the unbind forces us to >- * use a smaller page size. For simplity we always issue a set of unbinds >- * unmapping anything in the range and at most 2 rebinds on the edges. >- * >- * Similar to an array of binds, in fences are passed to the first operation and >- * out fences are signaled on the last operation. >- * >- * In this example there is a window of time where 0x0000-0x1000 and >- * 0x4000-0x5000 are invalid but the user didn't ask for these addresses to be >- * removed from the mapping. To work around this we treat any munmap style >- * unbinds which require a rebind as a kernel operations (BO eviction or userptr >- * invalidation). The first operation waits on the VM's >- * DMA_RESV_USAGE_PREEMPT_FENCE slots (waits for all pending jobs on VM to >- * complete / triggers preempt fences) and the last operation is installed in >- * the VM's DMA_RESV_USAGE_KERNEL slot (blocks future jobs / resume compute mode >- * VM). The caveat is all dma-resv slots must be updated atomically with respect >- * to execs and compute mode rebind worker. To accomplish this, hold the >- * vm->lock in write mode from the first operation until the last. >- * >- * Deferred binds in fault mode >- * ---------------------------- >- * >- * In a VM is in fault mode (TODO: link to fault mode), new bind operations that >- * create mappings are by default are deferred to the page fault handler (first >- * use). This behavior can be overriden by setting the flag >- * XE_VM_BIND_FLAG_IMMEDIATE which indicates to creating the mapping >- * immediately. >- * >- * User pointer >- * ============ >- * >- * User pointers are user allocated memory (malloc'd, mmap'd, etc..) for which the >- * user wants to create a GPU mapping. Typically in other DRM drivers a dummy BO >- * was created and then a binding was created. We bypass creating a dummy BO in >- * XE and simply create a binding directly from the userptr. >- * >- * Invalidation >- * ------------ >- * >- * Since this a core kernel managed memory the kernel can move this memory >- * whenever it wants. We register an invalidation MMU notifier to alert XE when >- * a user poiter is about to move. The invalidation notifier needs to block >- * until all pending users (jobs or compute mode engines) of the userptr are >- * idle to ensure no faults. This done by waiting on all of VM's dma-resv slots. >- * >- * Rebinds >- * ------- >- * >- * Either the next exec (non-compute) or rebind worker (compute mode) will >- * rebind the userptr. The invalidation MMU notifier kicks the rebind worker >- * after the VM dma-resv wait if the VM is in compute mode. >- * >- * Compute mode >- * ============ >- * >- * A VM in compute mode enables long running workloads and ultra low latency >- * submission (ULLS). ULLS is implemented via a continuously running batch + >- * semaphores. This enables to the user to insert jump to new batch commands >- * into the continuously running batch. In both cases these batches exceed the >- * time a dma fence is allowed to exist for before signaling, as such dma fences >- * are not used when a VM is in compute mode. User fences (TODO: link user fence >- * doc) are used instead to signal operation's completion. >- * >- * Preempt fences >- * -------------- >- * >- * If the kernel decides to move memory around (either userptr invalidate, BO >- * eviction, or mumap style unbind which results in a rebind) and a batch is >- * running on an engine, that batch can fault or cause a memory corruption as >- * page tables for the moved memory are no longer valid. To work around this we >- * introduce the concept of preempt fences. When sw signaling is enabled on a >- * preempt fence it tells the submission backend to kick that engine off the >- * hardware and the preempt fence signals when the engine is off the hardware. >- * Once all preempt fences are signaled for a VM the kernel can safely move the >- * memory and kick the rebind worker which resumes all the engines execution. >- * >- * A preempt fence, for every engine using the VM, is installed the VM's >- * dma-resv DMA_RESV_USAGE_PREEMPT_FENCE slot. The same preempt fence, for every >- * engine using the VM, is also installed into the same dma-resv slot of every >- * external BO mapped in the VM. >- * >- * Rebind worker >- * ------------- >- * >- * The rebind worker is very similar to an exec. It is resposible for rebinding >- * evicted BOs or userptrs, waiting on those operations, installing new preempt >- * fences, and finally resuming executing of engines in the VM. >- * >- * Flow >- * ~~~~ >- * >- * .. code-block:: >- * >- * <----------------------------------------------------------------------| >- * Check if VM is closed, if so bail out | >- * Lock VM global lock in read mode | >- * Pin userptrs (also finds userptr invalidated since last rebind worker) | >- * Lock VM dma-resv and external BOs dma-resv | >- * Validate BOs that have been evicted | >- * Wait on and allocate new preempt fences for every engine using the VM | >- * Rebind invalidated userptrs + evicted BOs | >- * Wait on last rebind fence | >- * Wait VM's DMA_RESV_USAGE_KERNEL dma-resv slot | >- * Install preeempt fences and issue resume for every engine using the VM | >- * Check if any userptrs invalidated since pin | >- * Squash resume for all engines | >- * Unlock all | >- * Wait all VM's dma-resv slots | >- * Retry ---------------------------------------------------------- >- * Release all engines waiting to resume >- * Unlock all >- * >- * Timeslicing >- * ----------- >- * >- * In order to prevent an engine from continuously being kicked off the hardware >- * and making no forward progress an engine has a period of time it allowed to >- * run after resume before it can be kicked off again. This effectively gives >- * each engine a timeslice. >- * >- * Handling multiple GTs >- * ===================== >- * >- * If a GT has slower access to some regions and the page table structure are in >- * the slow region, the performance on that GT could adversely be affected. To >- * work around this we allow a VM page tables to be shadowed in multiple GTs. >- * When VM is created, a default bind engine and PT table structure are created >- * on each GT. >- * >- * Binds can optionally pass in a mask of GTs where a mapping should be created, >- * if this mask is zero then default to all the GTs where the VM has page >- * tables. >- * >- * The implementation for this breaks down into a bunch for_each_gt loops in >- * various places plus exporting a composite fence for multi-GT binds to the >- * user. >- * >- * Fault mode (unified shared memory) >- * ================================== >- * >- * A VM in fault mode can be enabled on devices that support page faults. If >- * page faults are enabled, using dma fences can potentially induce a deadlock: >- * A pending page fault can hold up the GPU work which holds up the dma fence >- * signaling, and memory allocation is usually required to resolve a page >- * fault, but memory allocation is not allowed to gate dma fence signaling. As >- * such, dma fences are not allowed when VM is in fault mode. Because dma-fences >- * are not allowed, long running workloads and ULLS are enabled on a faulting >- * VM. >- * >- * Defered VM binds >- * ---------------- >- * >- * By default, on a faulting VM binds just allocate the VMA and the actual >- * updating of the page tables is defered to the page fault handler. This >- * behavior can be overridden by setting the flag XE_VM_BIND_FLAG_IMMEDIATE in >- * the VM bind which will then do the bind immediately. >- * >- * Page fault handler >- * ------------------ >- * >- * Page faults are received in the G2H worker under the CT lock which is in the >- * path of dma fences (no memory allocations are allowed, faults require memory >- * allocations) thus we cannot process faults under the CT lock. Another issue >- * is faults issue TLB invalidations which require G2H credits and we cannot >- * allocate G2H credits in the G2H handlers without deadlocking. Lastly, we do >- * not want the CT lock to be an outer lock of the VM global lock (VM global >- * lock required to fault processing). >- * >- * To work around the above issue with processing faults in the G2H worker, we >- * sink faults to a buffer which is large enough to sink all possible faults on >- * the GT (1 per hardware engine) and kick a worker to process the faults. Since >- * the page faults G2H are already received in a worker, kicking another worker >- * adds more latency to a critical performance path. We add a fast path in the >- * G2H irq handler which looks at first G2H and if it is a page fault we sink >- * the fault to the buffer and kick the worker to process the fault. TLB >- * invalidation responses are also in the critical path so these can also be >- * processed in this fast path. >- * >- * Multiple buffers and workers are used and hashed over based on the ASID so >- * faults from different VMs can be processed in parallel. >- * >- * The page fault handler itself is rather simple, flow is below. >- * >- * .. code-block:: >- * >- * Lookup VM from ASID in page fault G2H >- * Lock VM global lock in read mode >- * Lookup VMA from address in page fault G2H >- * Check if VMA is valid, if not bail >- * Check if VMA's BO has backing store, if not allocate >- * <----------------------------------------------------------------------| >- * If userptr, pin pages | >- * Lock VM & BO dma-resv locks | >- * If atomic fault, migrate to VRAM, else validate BO location | >- * Issue rebind | >- * Wait on rebind to complete | >- * Check if userptr invalidated since pin | >- * Drop VM & BO dma-resv locks | >- * Retry ---------------------------------------------------------- >- * Unlock all >- * Issue blocking TLB invalidation | >- * Send page fault response to GuC >- * >- * Access counters >- * --------------- >- * >- * Access counters can be configured to trigger a G2H indicating the device is >- * accessing VMAs in system memory frequently as hint to migrate those VMAs to >- * VRAM. >- * >- * Same as the page fault handler, access counters G2H cannot be processed the >- * G2H worker under the CT lock. Again we use a buffer to sink access counter >- * G2H. Unlike page faults there is no upper bound so if the buffer is full we >- * simply drop the G2H. Access counters are a best case optimization and it is >- * safe to drop these unlike page faults. >- * >- * The access counter handler itself is rather simple flow is below. >- * >- * .. code-block:: >- * >- * Lookup VM from ASID in access counter G2H >- * Lock VM global lock in read mode >- * Lookup VMA from address in access counter G2H >- * If userptr, bail nothing to do >- * Lock VM & BO dma-resv locks >- * Issue migration to VRAM >- * Unlock all >- * >- * Notice no rebind is issued in the access counter handler as the rebind will >- * be issued on next page fault. >- * >- * Cavets with eviction / user pointer invalidation >- * ------------------------------------------------ >- * >- * In the case of eviction and user pointer invalidation on a faulting VM, there >- * is no need to issue a rebind rather we just need to blow away the page tables >- * for the VMAs and the page fault handler will rebind the VMAs when they fault. >- * The cavet is to update / read the page table structure the VM global lock is >- * neeeed. In both the case of eviction and user pointer invalidation locks are >- * held which make acquiring the VM global lock impossible. To work around this >- * every VMA maintains a list of leaf page table entries which should be written >- * to zero to blow away the VMA's page tables. After writing zero to these >- * entries a blocking TLB invalidate is issued. At this point it is safe for the >- * kernel to move the VMA's memory around. This is a necessary lockless >- * algorithm and is safe as leafs cannot be changed while either an eviction or >- * userptr invalidation is occurring. >- * >- * Locking >- * ======= >- * >- * VM locking protects all of the core data paths (bind operations, execs, >- * evictions, and compute mode rebind worker) in XE. >- * >- * Locks >- * ----- >- * >- * VM global lock (vm->lock) - rw semaphore lock. Outer most lock which protects >- * the list of userptrs mapped in the VM, the list of engines using this VM, and >- * the array of external BOs mapped in the VM. When adding or removing any of the >- * aforemented state from the VM should acquire this lock in write mode. The VM >- * bind path also acquires this lock in write while the exec / compute mode >- * rebind worker acquire this lock in read mode. >- * >- * VM dma-resv lock (vm->ttm.base.resv->lock) - WW lock. Protects VM dma-resv >- * slots which is shared with any private BO in the VM. Expected to be acquired >- * during VM binds, execs, and compute mode rebind worker. This lock is also >- * held when private BOs are being evicted. >- * >- * external BO dma-resv lock (bo->ttm.base.resv->lock) - WW lock. Protects >- * external BO dma-resv slots. Expected to be acquired during VM binds (in >- * addition to the VM dma-resv lock). All external BO dma-locks within a VM are >- * expected to be acquired (in addition to the VM dma-resv lock) during execs >- * and the compute mode rebind worker. This lock is also held when an external >- * BO is being evicted. >- * >- * Putting it all together >- * ----------------------- >- * >- * 1. An exec and bind operation with the same VM can't be executing at the same >- * time (vm->lock). >- * >- * 2. A compute mode rebind worker and bind operation with the same VM can't be >- * executing at the same time (vm->lock). >- * >- * 3. We can't add / remove userptrs or external BOs to a VM while an exec with >- * the same VM is executing (vm->lock). >- * >- * 4. We can't add / remove userptrs, external BOs, or engines to a VM while a >- * compute mode rebind worker with the same VM is executing (vm->lock). >- * >- * 5. Evictions within a VM can't be happen while an exec with the same VM is >- * executing (dma-resv locks). >- * >- * 6. Evictions within a VM can't be happen while a compute mode rebind worker >- * with the same VM is executing (dma-resv locks). >- * >- * dma-resv usage >- * ============== >- * >- * As previously stated to enforce the ordering of kernel ops (eviction, userptr >- * invalidation, munmap style unbinds which result in a rebind), rebinds during >- * execs, execs, and resumes in the rebind worker we use both the VMs and >- * external BOs dma-resv slots. Let try to make this as clear as possible. >- * >- * Slot installation >- * ----------------- >- * >- * 1. Jobs from kernel ops install themselves into the DMA_RESV_USAGE_KERNEL >- * slot of either an external BO or VM (depends on if kernel op is operating on >- * an external or private BO) >- * >- * 2. In non-compute mode, jobs from execs install themselves into the >- * DMA_RESV_USAGE_BOOKKEEP slot of the VM >- * >- * 3. In non-compute mode, jobs from execs install themselves into the >- * DMA_RESV_USAGE_WRITE slot of all external BOs in the VM >- * >- * 4. Jobs from binds install themselves into the DMA_RESV_USAGE_BOOKKEEP slot >- * of the VM >- * >- * 5. Jobs from binds install themselves into the DMA_RESV_USAGE_BOOKKEEP slot >- * of the external BO (if the bind is to an external BO, this is addition to #4) >- * >- * 6. Every engine using a compute mode VM has a preempt fence in installed into >- * the DMA_RESV_USAGE_PREEMPT_FENCE slot of the VM >- * >- * 7. Every engine using a compute mode VM has a preempt fence in installed into >- * the DMA_RESV_USAGE_PREEMPT_FENCE slot of all the external BOs in the VM >- * >- * Slot waiting >- * ------------ >- * >- * 1. The exection of all jobs from kernel ops shall wait on all slots >- * (DMA_RESV_USAGE_PREEMPT_FENCE) of either an external BO or VM (depends on if >- * kernel op is operating on external or private BO) >- * >- * 2. In non-compute mode, the exection of all jobs from rebinds in execs shall >- * wait on the DMA_RESV_USAGE_KERNEL slot of either an external BO or VM >- * (depends on if the rebind is operatiing on an external or private BO) >- * >- * 3. In non-compute mode, the exection of all jobs from execs shall wait on the >- * last rebind job >- * >- * 4. In compute mode, the exection of all jobs from rebinds in the rebind >- * worker shall wait on the DMA_RESV_USAGE_KERNEL slot of either an external BO >- * or VM (depends on if rebind is operating on external or private BO) >- * >- * 5. In compute mode, resumes in rebind worker shall wait on last rebind fence >- * >- * 6. In compute mode, resumes in rebind worker shall wait on the >- * DMA_RESV_USAGE_KERNEL slot of the VM >- * >- * Putting it all together >- * ----------------------- >- * >- * 1. New jobs from kernel ops are blocked behind any existing jobs from >- * non-compute mode execs >- * >- * 2. New jobs from non-compute mode execs are blocked behind any existing jobs >- * from kernel ops and rebinds >- * >- * 3. New jobs from kernel ops are blocked behind all preempt fences signaling in >- * compute mode >- * >- * 4. Compute mode engine resumes are blocked behind any existing jobs from >- * kernel ops and rebinds >- * >- * Future work >- * =========== >- * >- * Support large pages for sysmem and userptr. >- * >- * Update page faults to handle BOs are page level grainularity (e.g. part of BO >- * could be in system memory while another part could be in VRAM). >- * >- * Page fault handler likely we be optimized a bit more (e.g. Rebinds always >- * wait on the dma-resv kernel slots of VM or BO, technically we only have to >- * wait the BO moving. If using a job to do the rebind, we could not block in >- * the page fault handler rather attach a callback to fence of the rebind job to >- * signal page fault complete. Our handling of short circuting for atomic faults >- * for bound VMAs could be better. etc...). We can tune all of this once we have >- * benchmarks / performance number from workloads up and running. >- */ >- >-#endif >-- >2.41.0 >