Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0 OR MIT
0002 /*
0003  * Copyright 2014-2022 Advanced Micro Devices, Inc.
0004  *
0005  * Permission is hereby granted, free of charge, to any person obtaining a
0006  * copy of this software and associated documentation files (the "Software"),
0007  * to deal in the Software without restriction, including without limitation
0008  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
0009  * and/or sell copies of the Software, and to permit persons to whom the
0010  * Software is furnished to do so, subject to the following conditions:
0011  *
0012  * The above copyright notice and this permission notice shall be included in
0013  * all copies or substantial portions of the Software.
0014  *
0015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0016  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0017  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
0018  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
0019  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
0020  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
0021  * OTHER DEALINGS IN THE SOFTWARE.
0022  *
0023  */
0024 
0025 #include <linux/device.h>
0026 #include <linux/export.h>
0027 #include <linux/err.h>
0028 #include <linux/fs.h>
0029 #include <linux/sched.h>
0030 #include <linux/slab.h>
0031 #include <linux/uaccess.h>
0032 #include <linux/compat.h>
0033 #include <uapi/linux/kfd_ioctl.h>
0034 #include <linux/time.h>
0035 #include "kfd_priv.h"
0036 #include <linux/mm.h>
0037 #include <linux/mman.h>
0038 #include <linux/processor.h>
0039 
0040 /*
0041  * The primary memory I/O features being added for revisions of gfxip
0042  * beyond 7.0 (Kaveri) are:
0043  *
0044  * Access to ATC/IOMMU mapped memory w/ associated extension of VA to 48b
0045  *
0046  * “Flat” shader memory access – These are new shader vector memory
0047  * operations that do not reference a T#/V# so a “pointer” is what is
0048  * sourced from the vector gprs for direct access to memory.
0049  * This pointer space has the Shared(LDS) and Private(Scratch) memory
0050  * mapped into this pointer space as apertures.
0051  * The hardware then determines how to direct the memory request
0052  * based on what apertures the request falls in.
0053  *
0054  * Unaligned support and alignment check
0055  *
0056  *
0057  * System Unified Address - SUA
0058  *
0059  * The standard usage for GPU virtual addresses are that they are mapped by
0060  * a set of page tables we call GPUVM and these page tables are managed by
0061  * a combination of vidMM/driver software components.  The current virtual
0062  * address (VA) range for GPUVM is 40b.
0063  *
0064  * As of gfxip7.1 and beyond we’re adding the ability for compute memory
0065  * clients (CP/RLC, DMA, SHADER(ifetch, scalar, and vector ops)) to access
0066  * the same page tables used by host x86 processors and that are managed by
0067  * the operating system. This is via a technique and hardware called ATC/IOMMU.
0068  * The GPU has the capability of accessing both the GPUVM and ATC address
0069  * spaces for a given VMID (process) simultaneously and we call this feature
0070  * system unified address (SUA).
0071  *
0072  * There are three fundamental address modes of operation for a given VMID
0073  * (process) on the GPU:
0074  *
0075  *  HSA64 – 64b pointers and the default address space is ATC
0076  *  HSA32 – 32b pointers and the default address space is ATC
0077  *  GPUVM – 64b pointers and the default address space is GPUVM (driver
0078  *      model mode)
0079  *
0080  *
0081  * HSA64 - ATC/IOMMU 64b
0082  *
0083  * A 64b pointer in the AMD64/IA64 CPU architecture is not fully utilized
0084  * by the CPU so an AMD CPU can only access the high area
0085  * (VA[63:47] == 0x1FFFF) and low area (VA[63:47 == 0) of the address space
0086  * so the actual VA carried to translation is 48b.  There is a “hole” in
0087  * the middle of the 64b VA space.
0088  *
0089  * The GPU not only has access to all of the CPU accessible address space via
0090  * ATC/IOMMU, but it also has access to the GPUVM address space.  The “system
0091  * unified address” feature (SUA) is the mapping of GPUVM and ATC address
0092  * spaces into a unified pointer space.  The method we take for 64b mode is
0093  * to map the full 40b GPUVM address space into the hole of the 64b address
0094  * space.
0095 
0096  * The GPUVM_Base/GPUVM_Limit defines the aperture in the 64b space where we
0097  * direct requests to be translated via GPUVM page tables instead of the
0098  * IOMMU path.
0099  *
0100  *
0101  * 64b to 49b Address conversion
0102  *
0103  * Note that there are still significant portions of unused regions (holes)
0104  * in the 64b address space even for the GPU.  There are several places in
0105  * the pipeline (sw and hw), we wish to compress the 64b virtual address
0106  * to a 49b address.  This 49b address is constituted of an “ATC” bit
0107  * plus a 48b virtual address.  This 49b address is what is passed to the
0108  * translation hardware.  ATC==0 means the 48b address is a GPUVM address
0109  * (max of 2^40 – 1) intended to be translated via GPUVM page tables.
0110  * ATC==1 means the 48b address is intended to be translated via IOMMU
0111  * page tables.
0112  *
0113  * A 64b pointer is compared to the apertures that are defined (Base/Limit), in
0114  * this case the GPUVM aperture (red) is defined and if a pointer falls in this
0115  * aperture, we subtract the GPUVM_Base address and set the ATC bit to zero
0116  * as part of the 64b to 49b conversion.
0117  *
0118  * Where this 64b to 49b conversion is done is a function of the usage.
0119  * Most GPU memory access is via memory objects where the driver builds
0120  * a descriptor which consists of a base address and a memory access by
0121  * the GPU usually consists of some kind of an offset or Cartesian coordinate
0122  * that references this memory descriptor.  This is the case for shader
0123  * instructions that reference the T# or V# constants, or for specified
0124  * locations of assets (ex. the shader program location).  In these cases
0125  * the driver is what handles the 64b to 49b conversion and the base
0126  * address in the descriptor (ex. V# or T# or shader program location)
0127  * is defined as a 48b address w/ an ATC bit.  For this usage a given
0128  * memory object cannot straddle multiple apertures in the 64b address
0129  * space. For example a shader program cannot jump in/out between ATC
0130  * and GPUVM space.
0131  *
0132  * In some cases we wish to pass a 64b pointer to the GPU hardware and
0133  * the GPU hw does the 64b to 49b conversion before passing memory
0134  * requests to the cache/memory system.  This is the case for the
0135  * S_LOAD and FLAT_* shader memory instructions where we have 64b pointers
0136  * in scalar and vector GPRs respectively.
0137  *
0138  * In all cases (no matter where the 64b -> 49b conversion is done), the gfxip
0139  * hardware sends a 48b address along w/ an ATC bit, to the memory controller
0140  * on the memory request interfaces.
0141  *
0142  *  <client>_MC_rdreq_atc   // read request ATC bit
0143  *
0144  *      0 : <client>_MC_rdreq_addr is a GPUVM VA
0145  *
0146  *      1 : <client>_MC_rdreq_addr is a ATC VA
0147  *
0148  *
0149  * “Spare” aperture (APE1)
0150  *
0151  * We use the GPUVM aperture to differentiate ATC vs. GPUVM, but we also use
0152  * apertures to set the Mtype field for S_LOAD/FLAT_* ops which is input to the
0153  * config tables for setting cache policies. The “spare” (APE1) aperture is
0154  * motivated by getting a different Mtype from the default.
0155  * The default aperture isn’t an actual base/limit aperture; it is just the
0156  * address space that doesn’t hit any defined base/limit apertures.
0157  * The following diagram is a complete picture of the gfxip7.x SUA apertures.
0158  * The APE1 can be placed either below or above
0159  * the hole (cannot be in the hole).
0160  *
0161  *
0162  * General Aperture definitions and rules
0163  *
0164  * An aperture register definition consists of a Base, Limit, Mtype, and
0165  * usually an ATC bit indicating which translation tables that aperture uses.
0166  * In all cases (for SUA and DUA apertures discussed later), aperture base
0167  * and limit definitions are 64KB aligned.
0168  *
0169  *  <ape>_Base[63:0] = { <ape>_Base_register[63:16], 0x0000 }
0170  *
0171  *  <ape>_Limit[63:0] = { <ape>_Limit_register[63:16], 0xFFFF }
0172  *
0173  * The base and limit are considered inclusive to an aperture so being
0174  * inside an aperture means (address >= Base) AND (address <= Limit).
0175  *
0176  * In no case is a payload that straddles multiple apertures expected to work.
0177  * For example a load_dword_x4 that starts in one aperture and ends in another,
0178  * does not work.  For the vector FLAT_* ops we have detection capability in
0179  * the shader for reporting a “memory violation” back to the
0180  * SQ block for use in traps.
0181  * A memory violation results when an op falls into the hole,
0182  * or a payload straddles multiple apertures.  The S_LOAD instruction
0183  * does not have this detection.
0184  *
0185  * Apertures cannot overlap.
0186  *
0187  *
0188  *
0189  * HSA32 - ATC/IOMMU 32b
0190  *
0191  * For HSA32 mode, the pointers are interpreted as 32 bits and use a single GPR
0192  * instead of two for the S_LOAD and FLAT_* ops. The entire GPUVM space of 40b
0193  * will not fit so there is only partial visibility to the GPUVM
0194  * space (defined by the aperture) for S_LOAD and FLAT_* ops.
0195  * There is no spare (APE1) aperture for HSA32 mode.
0196  *
0197  *
0198  * GPUVM 64b mode (driver model)
0199  *
0200  * This mode is related to HSA64 in that the difference really is that
0201  * the default aperture is GPUVM (ATC==0) and not ATC space.
0202  * We have gfxip7.x hardware that has FLAT_* and S_LOAD support for
0203  * SUA GPUVM mode, but does not support HSA32/HSA64.
0204  *
0205  *
0206  * Device Unified Address - DUA
0207  *
0208  * Device unified address (DUA) is the name of the feature that maps the
0209  * Shared(LDS) memory and Private(Scratch) memory into the overall address
0210  * space for use by the new FLAT_* vector memory ops.  The Shared and
0211  * Private memories are mapped as apertures into the address space,
0212  * and the hardware detects when a FLAT_* memory request is to be redirected
0213  * to the LDS or Scratch memory when it falls into one of these apertures.
0214  * Like the SUA apertures, the Shared/Private apertures are 64KB aligned and
0215  * the base/limit is “in” the aperture. For both HSA64 and GPUVM SUA modes,
0216  * the Shared/Private apertures are always placed in a limited selection of
0217  * options in the hole of the 64b address space. For HSA32 mode, the
0218  * Shared/Private apertures can be placed anywhere in the 32b space
0219  * except at 0.
0220  *
0221  *
0222  * HSA64 Apertures for FLAT_* vector ops
0223  *
0224  * For HSA64 SUA mode, the Shared and Private apertures are always placed
0225  * in the hole w/ a limited selection of possible locations. The requests
0226  * that fall in the private aperture are expanded as a function of the
0227  * work-item id (tid) and redirected to the location of the
0228  * “hidden private memory”. The hidden private can be placed in either GPUVM
0229  * or ATC space. The addresses that fall in the shared aperture are
0230  * re-directed to the on-chip LDS memory hardware.
0231  *
0232  *
0233  * HSA32 Apertures for FLAT_* vector ops
0234  *
0235  * In HSA32 mode, the Private and Shared apertures can be placed anywhere
0236  * in the 32b space except at 0 (Private or Shared Base at zero disables
0237  * the apertures). If the base address of the apertures are non-zero
0238  * (ie apertures exists), the size is always 64KB.
0239  *
0240  *
0241  * GPUVM Apertures for FLAT_* vector ops
0242  *
0243  * In GPUVM mode, the Shared/Private apertures are specified identically
0244  * to HSA64 mode where they are always in the hole at a limited selection
0245  * of locations.
0246  *
0247  *
0248  * Aperture Definitions for SUA and DUA
0249  *
0250  * The interpretation of the aperture register definitions for a given
0251  * VMID is a function of the “SUA Mode” which is one of HSA64, HSA32, or
0252  * GPUVM64 discussed in previous sections. The mode is first decoded, and
0253  * then the remaining register decode is a function of the mode.
0254  *
0255  *
0256  * SUA Mode Decode
0257  *
0258  * For the S_LOAD and FLAT_* shader operations, the SUA mode is decoded from
0259  * the COMPUTE_DISPATCH_INITIATOR:DATA_ATC bit and
0260  * the SH_MEM_CONFIG:PTR32 bits.
0261  *
0262  * COMPUTE_DISPATCH_INITIATOR:DATA_ATC    SH_MEM_CONFIG:PTR32        Mode
0263  *
0264  * 1                                              0                  HSA64
0265  *
0266  * 1                                              1                  HSA32
0267  *
0268  * 0                                              X                 GPUVM64
0269  *
0270  * In general the hardware will ignore the PTR32 bit and treat
0271  * as “0” whenever DATA_ATC = “0”, but sw should set PTR32=0
0272  * when DATA_ATC=0.
0273  *
0274  * The DATA_ATC bit is only set for compute dispatches.
0275  * All “Draw” dispatches are hardcoded to GPUVM64 mode
0276  * for FLAT_* / S_LOAD operations.
0277  */
0278 
0279 #define MAKE_GPUVM_APP_BASE_VI(gpu_num) \
0280     (((uint64_t)(gpu_num) << 61) + 0x1000000000000L)
0281 
0282 #define MAKE_GPUVM_APP_LIMIT(base, size) \
0283     (((uint64_t)(base) & 0xFFFFFF0000000000UL) + (size) - 1)
0284 
0285 #define MAKE_SCRATCH_APP_BASE_VI() \
0286     (((uint64_t)(0x1UL) << 61) + 0x100000000L)
0287 
0288 #define MAKE_SCRATCH_APP_LIMIT(base) \
0289     (((uint64_t)base & 0xFFFFFFFF00000000UL) | 0xFFFFFFFF)
0290 
0291 #define MAKE_LDS_APP_BASE_VI() \
0292     (((uint64_t)(0x1UL) << 61) + 0x0)
0293 #define MAKE_LDS_APP_LIMIT(base) \
0294     (((uint64_t)(base) & 0xFFFFFFFF00000000UL) | 0xFFFFFFFF)
0295 
0296 /* On GFXv9 the LDS and scratch apertures are programmed independently
0297  * using the high 16 bits of the 64-bit virtual address. They must be
0298  * in the hole, which will be the case as long as the high 16 bits are
0299  * not 0.
0300  *
0301  * The aperture sizes are still 4GB implicitly.
0302  *
0303  * A GPUVM aperture is not applicable on GFXv9.
0304  */
0305 #define MAKE_LDS_APP_BASE_V9() ((uint64_t)(0x1UL) << 48)
0306 #define MAKE_SCRATCH_APP_BASE_V9() ((uint64_t)(0x2UL) << 48)
0307 
0308 /* User mode manages most of the SVM aperture address space. The low
0309  * 16MB are reserved for kernel use (CWSR trap handler and kernel IB
0310  * for now).
0311  */
0312 #define SVM_USER_BASE (u64)(KFD_CWSR_TBA_TMA_SIZE + 2*PAGE_SIZE)
0313 #define SVM_CWSR_BASE (SVM_USER_BASE - KFD_CWSR_TBA_TMA_SIZE)
0314 #define SVM_IB_BASE   (SVM_CWSR_BASE - PAGE_SIZE)
0315 
0316 static void kfd_init_apertures_vi(struct kfd_process_device *pdd, uint8_t id)
0317 {
0318     /*
0319      * node id couldn't be 0 - the three MSB bits of
0320      * aperture shouldn't be 0
0321      */
0322     pdd->lds_base = MAKE_LDS_APP_BASE_VI();
0323     pdd->lds_limit = MAKE_LDS_APP_LIMIT(pdd->lds_base);
0324 
0325     if (!pdd->dev->use_iommu_v2) {
0326         /* dGPUs: SVM aperture starting at 0
0327          * with small reserved space for kernel.
0328          * Set them to CANONICAL addresses.
0329          */
0330         pdd->gpuvm_base = SVM_USER_BASE;
0331         pdd->gpuvm_limit =
0332             pdd->dev->shared_resources.gpuvm_size - 1;
0333     } else {
0334         /* set them to non CANONICAL addresses, and no SVM is
0335          * allocated.
0336          */
0337         pdd->gpuvm_base = MAKE_GPUVM_APP_BASE_VI(id + 1);
0338         pdd->gpuvm_limit = MAKE_GPUVM_APP_LIMIT(pdd->gpuvm_base,
0339                 pdd->dev->shared_resources.gpuvm_size);
0340     }
0341 
0342     pdd->scratch_base = MAKE_SCRATCH_APP_BASE_VI();
0343     pdd->scratch_limit = MAKE_SCRATCH_APP_LIMIT(pdd->scratch_base);
0344 }
0345 
0346 static void kfd_init_apertures_v9(struct kfd_process_device *pdd, uint8_t id)
0347 {
0348     pdd->lds_base = MAKE_LDS_APP_BASE_V9();
0349     pdd->lds_limit = MAKE_LDS_APP_LIMIT(pdd->lds_base);
0350 
0351     /* Raven needs SVM to support graphic handle, etc. Leave the small
0352      * reserved space before SVM on Raven as well, even though we don't
0353      * have to.
0354      * Set gpuvm_base and gpuvm_limit to CANONICAL addresses so that they
0355      * are used in Thunk to reserve SVM.
0356      */
0357     pdd->gpuvm_base = SVM_USER_BASE;
0358     pdd->gpuvm_limit =
0359         pdd->dev->shared_resources.gpuvm_size - 1;
0360 
0361     pdd->scratch_base = MAKE_SCRATCH_APP_BASE_V9();
0362     pdd->scratch_limit = MAKE_SCRATCH_APP_LIMIT(pdd->scratch_base);
0363 }
0364 
0365 int kfd_init_apertures(struct kfd_process *process)
0366 {
0367     uint8_t id  = 0;
0368     struct kfd_dev *dev;
0369     struct kfd_process_device *pdd;
0370 
0371     /*Iterating over all devices*/
0372     while (kfd_topology_enum_kfd_devices(id, &dev) == 0) {
0373         if (!dev || kfd_devcgroup_check_permission(dev)) {
0374             /* Skip non GPU devices and devices to which the
0375              * current process have no access to. Access can be
0376              * limited by placing the process in a specific
0377              * cgroup hierarchy
0378              */
0379             id++;
0380             continue;
0381         }
0382 
0383         pdd = kfd_create_process_device_data(dev, process);
0384         if (!pdd) {
0385             pr_err("Failed to create process device data\n");
0386             return -ENOMEM;
0387         }
0388         /*
0389          * For 64 bit process apertures will be statically reserved in
0390          * the x86_64 non canonical process address space
0391          * amdkfd doesn't currently support apertures for 32 bit process
0392          */
0393         if (process->is_32bit_user_mode) {
0394             pdd->lds_base = pdd->lds_limit = 0;
0395             pdd->gpuvm_base = pdd->gpuvm_limit = 0;
0396             pdd->scratch_base = pdd->scratch_limit = 0;
0397         } else {
0398             switch (dev->adev->asic_type) {
0399             case CHIP_KAVERI:
0400             case CHIP_HAWAII:
0401             case CHIP_CARRIZO:
0402             case CHIP_TONGA:
0403             case CHIP_FIJI:
0404             case CHIP_POLARIS10:
0405             case CHIP_POLARIS11:
0406             case CHIP_POLARIS12:
0407             case CHIP_VEGAM:
0408                 kfd_init_apertures_vi(pdd, id);
0409                 break;
0410             default:
0411                 if (KFD_GC_VERSION(dev) >= IP_VERSION(9, 0, 1))
0412                     kfd_init_apertures_v9(pdd, id);
0413                 else {
0414                     WARN(1, "Unexpected ASIC family %u",
0415                          dev->adev->asic_type);
0416                     return -EINVAL;
0417                 }
0418             }
0419 
0420             if (!dev->use_iommu_v2) {
0421                 /* dGPUs: the reserved space for kernel
0422                  * before SVM
0423                  */
0424                 pdd->qpd.cwsr_base = SVM_CWSR_BASE;
0425                 pdd->qpd.ib_base = SVM_IB_BASE;
0426             }
0427         }
0428 
0429         dev_dbg(kfd_device, "node id %u\n", id);
0430         dev_dbg(kfd_device, "gpu id %u\n", pdd->dev->id);
0431         dev_dbg(kfd_device, "lds_base %llX\n", pdd->lds_base);
0432         dev_dbg(kfd_device, "lds_limit %llX\n", pdd->lds_limit);
0433         dev_dbg(kfd_device, "gpuvm_base %llX\n", pdd->gpuvm_base);
0434         dev_dbg(kfd_device, "gpuvm_limit %llX\n", pdd->gpuvm_limit);
0435         dev_dbg(kfd_device, "scratch_base %llX\n", pdd->scratch_base);
0436         dev_dbg(kfd_device, "scratch_limit %llX\n", pdd->scratch_limit);
0437 
0438         id++;
0439     }
0440 
0441     return 0;
0442 }