Home | History | Annotate | Line # | Download | only in amdgpu
      1  1.3  riastrad /*	$NetBSD: amdgpu_gmc.h,v 1.3 2021/12/19 12:21:29 riastradh Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*
      4  1.1  riastrad  * Copyright 2018 Advanced Micro Devices, Inc.
      5  1.1  riastrad  * All Rights Reserved.
      6  1.1  riastrad  *
      7  1.1  riastrad  * Permission is hereby granted, free of charge, to any person obtaining a
      8  1.1  riastrad  * copy of this software and associated documentation files (the
      9  1.1  riastrad  * "Software"), to deal in the Software without restriction, including
     10  1.1  riastrad  * without limitation the rights to use, copy, modify, merge, publish,
     11  1.1  riastrad  * distribute, sub license, and/or sell copies of the Software, and to
     12  1.1  riastrad  * permit persons to whom the Software is furnished to do so, subject to
     13  1.1  riastrad  * the following conditions:
     14  1.1  riastrad  *
     15  1.1  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     16  1.1  riastrad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     17  1.1  riastrad  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
     18  1.1  riastrad  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
     19  1.1  riastrad  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
     20  1.1  riastrad  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
     21  1.1  riastrad  * USE OR OTHER DEALINGS IN THE SOFTWARE.
     22  1.1  riastrad  *
     23  1.1  riastrad  * The above copyright notice and this permission notice (including the
     24  1.1  riastrad  * next paragraph) shall be included in all copies or substantial portions
     25  1.1  riastrad  * of the Software.
     26  1.1  riastrad  *
     27  1.1  riastrad  */
     28  1.1  riastrad #ifndef __AMDGPU_GMC_H__
     29  1.1  riastrad #define __AMDGPU_GMC_H__
     30  1.1  riastrad 
     31  1.1  riastrad #include <linux/types.h>
     32  1.1  riastrad 
     33  1.1  riastrad #include "amdgpu_irq.h"
     34  1.1  riastrad 
     35  1.1  riastrad /* VA hole for 48bit addresses on Vega10 */
     36  1.1  riastrad #define AMDGPU_GMC_HOLE_START	0x0000800000000000ULL
     37  1.1  riastrad #define AMDGPU_GMC_HOLE_END	0xffff800000000000ULL
     38  1.1  riastrad 
     39  1.1  riastrad /*
     40  1.1  riastrad  * Hardware is programmed as if the hole doesn't exists with start and end
     41  1.1  riastrad  * address values.
     42  1.1  riastrad  *
     43  1.1  riastrad  * This mask is used to remove the upper 16bits of the VA and so come up with
     44  1.1  riastrad  * the linear addr value.
     45  1.1  riastrad  */
     46  1.1  riastrad #define AMDGPU_GMC_HOLE_MASK	0x0000ffffffffffffULL
     47  1.1  riastrad 
     48  1.1  riastrad /*
     49  1.1  riastrad  * Ring size as power of two for the log of recent faults.
     50  1.1  riastrad  */
     51  1.1  riastrad #define AMDGPU_GMC_FAULT_RING_ORDER	8
     52  1.1  riastrad #define AMDGPU_GMC_FAULT_RING_SIZE	(1 << AMDGPU_GMC_FAULT_RING_ORDER)
     53  1.1  riastrad 
     54  1.1  riastrad /*
     55  1.1  riastrad  * Hash size as power of two for the log of recent faults
     56  1.1  riastrad  */
     57  1.1  riastrad #define AMDGPU_GMC_FAULT_HASH_ORDER	8
     58  1.1  riastrad #define AMDGPU_GMC_FAULT_HASH_SIZE	(1 << AMDGPU_GMC_FAULT_HASH_ORDER)
     59  1.1  riastrad 
     60  1.1  riastrad /*
     61  1.1  riastrad  * Number of IH timestamp ticks until a fault is considered handled
     62  1.1  riastrad  */
     63  1.1  riastrad #define AMDGPU_GMC_FAULT_TIMEOUT	5000ULL
     64  1.1  riastrad 
     65  1.1  riastrad struct firmware;
     66  1.1  riastrad 
     67  1.1  riastrad /*
     68  1.1  riastrad  * GMC page fault information
     69  1.1  riastrad  */
     70  1.1  riastrad struct amdgpu_gmc_fault {
     71  1.1  riastrad 	uint64_t	timestamp;
     72  1.1  riastrad 	uint64_t	next:AMDGPU_GMC_FAULT_RING_ORDER;
     73  1.1  riastrad 	uint64_t	key:52;
     74  1.1  riastrad };
     75  1.1  riastrad 
     76  1.1  riastrad /*
     77  1.1  riastrad  * VMHUB structures, functions & helpers
     78  1.1  riastrad  */
     79  1.1  riastrad struct amdgpu_vmhub {
     80  1.1  riastrad 	uint32_t	ctx0_ptb_addr_lo32;
     81  1.1  riastrad 	uint32_t	ctx0_ptb_addr_hi32;
     82  1.1  riastrad 	uint32_t	vm_inv_eng0_sem;
     83  1.1  riastrad 	uint32_t	vm_inv_eng0_req;
     84  1.1  riastrad 	uint32_t	vm_inv_eng0_ack;
     85  1.1  riastrad 	uint32_t	vm_context0_cntl;
     86  1.1  riastrad 	uint32_t	vm_l2_pro_fault_status;
     87  1.1  riastrad 	uint32_t	vm_l2_pro_fault_cntl;
     88  1.1  riastrad };
     89  1.1  riastrad 
     90  1.1  riastrad /*
     91  1.1  riastrad  * GPU MC structures, functions & helpers
     92  1.1  riastrad  */
     93  1.1  riastrad struct amdgpu_gmc_funcs {
     94  1.1  riastrad 	/* flush the vm tlb via mmio */
     95  1.1  riastrad 	void (*flush_gpu_tlb)(struct amdgpu_device *adev, uint32_t vmid,
     96  1.1  riastrad 				uint32_t vmhub, uint32_t flush_type);
     97  1.1  riastrad 	/* flush the vm tlb via pasid */
     98  1.1  riastrad 	int (*flush_gpu_tlb_pasid)(struct amdgpu_device *adev, uint16_t pasid,
     99  1.1  riastrad 					uint32_t flush_type, bool all_hub);
    100  1.1  riastrad 	/* flush the vm tlb via ring */
    101  1.1  riastrad 	uint64_t (*emit_flush_gpu_tlb)(struct amdgpu_ring *ring, unsigned vmid,
    102  1.1  riastrad 				       uint64_t pd_addr);
    103  1.1  riastrad 	/* Change the VMID -> PASID mapping */
    104  1.1  riastrad 	void (*emit_pasid_mapping)(struct amdgpu_ring *ring, unsigned vmid,
    105  1.1  riastrad 				   unsigned pasid);
    106  1.1  riastrad 	/* enable/disable PRT support */
    107  1.1  riastrad 	void (*set_prt)(struct amdgpu_device *adev, bool enable);
    108  1.1  riastrad 	/* map mtype to hardware flags */
    109  1.1  riastrad 	uint64_t (*map_mtype)(struct amdgpu_device *adev, uint32_t flags);
    110  1.1  riastrad 	/* get the pde for a given mc addr */
    111  1.1  riastrad 	void (*get_vm_pde)(struct amdgpu_device *adev, int level,
    112  1.1  riastrad 			   u64 *dst, u64 *flags);
    113  1.1  riastrad 	/* get the pte flags to use for a BO VA mapping */
    114  1.1  riastrad 	void (*get_vm_pte)(struct amdgpu_device *adev,
    115  1.1  riastrad 			   struct amdgpu_bo_va_mapping *mapping,
    116  1.1  riastrad 			   uint64_t *flags);
    117  1.1  riastrad };
    118  1.1  riastrad 
    119  1.1  riastrad struct amdgpu_xgmi {
    120  1.1  riastrad 	/* from psp */
    121  1.1  riastrad 	u64 node_id;
    122  1.1  riastrad 	u64 hive_id;
    123  1.1  riastrad 	/* fixed per family */
    124  1.1  riastrad 	u64 node_segment_size;
    125  1.1  riastrad 	/* physical node (0-3) */
    126  1.1  riastrad 	unsigned physical_node_id;
    127  1.1  riastrad 	/* number of nodes (0-4) */
    128  1.1  riastrad 	unsigned num_physical_nodes;
    129  1.1  riastrad 	/* gpu list in the same hive */
    130  1.1  riastrad 	struct list_head head;
    131  1.1  riastrad 	bool supported;
    132  1.1  riastrad 	struct ras_common_if *ras_if;
    133  1.1  riastrad };
    134  1.1  riastrad 
    135  1.1  riastrad struct amdgpu_gmc {
    136  1.1  riastrad 	/* FB's physical address in MMIO space (for CPU to
    137  1.1  riastrad 	 * map FB). This is different compared to the agp/
    138  1.1  riastrad 	 * gart/vram_start/end field as the later is from
    139  1.1  riastrad 	 * GPU's view and aper_base is from CPU's view.
    140  1.1  riastrad 	 */
    141  1.1  riastrad 	resource_size_t		aper_size;
    142  1.1  riastrad 	resource_size_t		aper_base;
    143  1.3  riastrad #ifdef __NetBSD__
    144  1.3  riastrad 	bus_space_tag_t		aper_tag;
    145  1.3  riastrad #endif
    146  1.1  riastrad 	/* for some chips with <= 32MB we need to lie
    147  1.1  riastrad 	 * about vram size near mc fb location */
    148  1.1  riastrad 	u64			mc_vram_size;
    149  1.1  riastrad 	u64			visible_vram_size;
    150  1.1  riastrad 	/* AGP aperture start and end in MC address space
    151  1.1  riastrad 	 * Driver find a hole in the MC address space
    152  1.1  riastrad 	 * to place AGP by setting MC_VM_AGP_BOT/TOP registers
    153  1.1  riastrad 	 * Under VMID0, logical address == MC address. AGP
    154  1.1  riastrad 	 * aperture maps to physical bus or IOVA addressed.
    155  1.1  riastrad 	 * AGP aperture is used to simulate FB in ZFB case.
    156  1.1  riastrad 	 * AGP aperture is also used for page table in system
    157  1.1  riastrad 	 * memory (mainly for APU).
    158  1.1  riastrad 	 *
    159  1.1  riastrad 	 */
    160  1.1  riastrad 	u64			agp_size;
    161  1.1  riastrad 	u64			agp_start;
    162  1.1  riastrad 	u64			agp_end;
    163  1.1  riastrad 	/* GART aperture start and end in MC address space
    164  1.1  riastrad 	 * Driver find a hole in the MC address space
    165  1.1  riastrad 	 * to place GART by setting VM_CONTEXT0_PAGE_TABLE_START/END_ADDR
    166  1.1  riastrad 	 * registers
    167  1.1  riastrad 	 * Under VMID0, logical address inside GART aperture will
    168  1.1  riastrad 	 * be translated through gpuvm gart page table to access
    169  1.1  riastrad 	 * paged system memory
    170  1.1  riastrad 	 */
    171  1.1  riastrad 	u64			gart_size;
    172  1.1  riastrad 	u64			gart_start;
    173  1.1  riastrad 	u64			gart_end;
    174  1.1  riastrad 	/* Frame buffer aperture of this GPU device. Different from
    175  1.1  riastrad 	 * fb_start (see below), this only covers the local GPU device.
    176  1.1  riastrad 	 * Driver get fb_start from MC_VM_FB_LOCATION_BASE (set by vbios)
    177  1.1  riastrad 	 * and calculate vram_start of this local device by adding an
    178  1.1  riastrad 	 * offset inside the XGMI hive.
    179  1.1  riastrad 	 * Under VMID0, logical address == MC address
    180  1.1  riastrad 	 */
    181  1.1  riastrad 	u64			vram_start;
    182  1.1  riastrad 	u64			vram_end;
    183  1.1  riastrad 	/* FB region , it's same as local vram region in single GPU, in XGMI
    184  1.1  riastrad 	 * configuration, this region covers all GPUs in the same hive ,
    185  1.1  riastrad 	 * each GPU in the hive has the same view of this FB region .
    186  1.1  riastrad 	 * GPU0's vram starts at offset (0 * segment size) ,
    187  1.1  riastrad 	 * GPU1 starts at offset (1 * segment size), etc.
    188  1.1  riastrad 	 */
    189  1.1  riastrad 	u64			fb_start;
    190  1.1  riastrad 	u64			fb_end;
    191  1.1  riastrad 	unsigned		vram_width;
    192  1.1  riastrad 	u64			real_vram_size;
    193  1.1  riastrad 	int			vram_mtrr;
    194  1.1  riastrad 	u64                     mc_mask;
    195  1.1  riastrad 	const struct firmware   *fw;	/* MC firmware */
    196  1.1  riastrad 	uint32_t                fw_version;
    197  1.1  riastrad 	struct amdgpu_irq_src	vm_fault;
    198  1.1  riastrad 	uint32_t		vram_type;
    199  1.1  riastrad 	uint8_t			vram_vendor;
    200  1.1  riastrad 	uint32_t                srbm_soft_reset;
    201  1.1  riastrad 	bool			prt_warning;
    202  1.1  riastrad 	uint64_t		stolen_size;
    203  1.1  riastrad 	/* apertures */
    204  1.1  riastrad 	u64			shared_aperture_start;
    205  1.1  riastrad 	u64			shared_aperture_end;
    206  1.1  riastrad 	u64			private_aperture_start;
    207  1.1  riastrad 	u64			private_aperture_end;
    208  1.1  riastrad 	/* protects concurrent invalidation */
    209  1.1  riastrad 	spinlock_t		invalidate_lock;
    210  1.1  riastrad 	bool			translate_further;
    211  1.1  riastrad 	struct kfd_vm_fault_info *vm_fault_info;
    212  1.1  riastrad 	atomic_t		vm_fault_info_updated;
    213  1.1  riastrad 
    214  1.1  riastrad 	struct amdgpu_gmc_fault	fault_ring[AMDGPU_GMC_FAULT_RING_SIZE];
    215  1.1  riastrad 	struct {
    216  1.1  riastrad 		uint64_t	idx:AMDGPU_GMC_FAULT_RING_ORDER;
    217  1.1  riastrad 	} fault_hash[AMDGPU_GMC_FAULT_HASH_SIZE];
    218  1.1  riastrad 	uint64_t		last_fault:AMDGPU_GMC_FAULT_RING_ORDER;
    219  1.1  riastrad 
    220  1.1  riastrad 	const struct amdgpu_gmc_funcs	*gmc_funcs;
    221  1.1  riastrad 
    222  1.1  riastrad 	struct amdgpu_xgmi xgmi;
    223  1.1  riastrad 	struct amdgpu_irq_src	ecc_irq;
    224  1.1  riastrad };
    225  1.1  riastrad 
    226  1.1  riastrad #define amdgpu_gmc_flush_gpu_tlb(adev, vmid, vmhub, type) ((adev)->gmc.gmc_funcs->flush_gpu_tlb((adev), (vmid), (vmhub), (type)))
    227  1.1  riastrad #define amdgpu_gmc_flush_gpu_tlb_pasid(adev, pasid, type, allhub) \
    228  1.1  riastrad 	((adev)->gmc.gmc_funcs->flush_gpu_tlb_pasid \
    229  1.1  riastrad 	((adev), (pasid), (type), (allhub)))
    230  1.1  riastrad #define amdgpu_gmc_emit_flush_gpu_tlb(r, vmid, addr) (r)->adev->gmc.gmc_funcs->emit_flush_gpu_tlb((r), (vmid), (addr))
    231  1.1  riastrad #define amdgpu_gmc_emit_pasid_mapping(r, vmid, pasid) (r)->adev->gmc.gmc_funcs->emit_pasid_mapping((r), (vmid), (pasid))
    232  1.1  riastrad #define amdgpu_gmc_map_mtype(adev, flags) (adev)->gmc.gmc_funcs->map_mtype((adev),(flags))
    233  1.1  riastrad #define amdgpu_gmc_get_vm_pde(adev, level, dst, flags) (adev)->gmc.gmc_funcs->get_vm_pde((adev), (level), (dst), (flags))
    234  1.1  riastrad #define amdgpu_gmc_get_vm_pte(adev, mapping, flags) (adev)->gmc.gmc_funcs->get_vm_pte((adev), (mapping), (flags))
    235  1.1  riastrad 
    236  1.1  riastrad /**
    237  1.1  riastrad  * amdgpu_gmc_vram_full_visible - Check if full VRAM is visible through the BAR
    238  1.1  riastrad  *
    239  1.1  riastrad  * @adev: amdgpu_device pointer
    240  1.1  riastrad  *
    241  1.1  riastrad  * Returns:
    242  1.1  riastrad  * True if full VRAM is visible through the BAR
    243  1.1  riastrad  */
    244  1.1  riastrad static inline bool amdgpu_gmc_vram_full_visible(struct amdgpu_gmc *gmc)
    245  1.1  riastrad {
    246  1.1  riastrad 	WARN_ON(gmc->real_vram_size < gmc->visible_vram_size);
    247  1.1  riastrad 
    248  1.1  riastrad 	return (gmc->real_vram_size == gmc->visible_vram_size);
    249  1.1  riastrad }
    250  1.1  riastrad 
    251  1.1  riastrad /**
    252  1.1  riastrad  * amdgpu_gmc_sign_extend - sign extend the given gmc address
    253  1.1  riastrad  *
    254  1.1  riastrad  * @addr: address to extend
    255  1.1  riastrad  */
    256  1.1  riastrad static inline uint64_t amdgpu_gmc_sign_extend(uint64_t addr)
    257  1.1  riastrad {
    258  1.1  riastrad 	if (addr >= AMDGPU_GMC_HOLE_START)
    259  1.1  riastrad 		addr |= AMDGPU_GMC_HOLE_END;
    260  1.1  riastrad 
    261  1.1  riastrad 	return addr;
    262  1.1  riastrad }
    263  1.1  riastrad 
    264  1.1  riastrad void amdgpu_gmc_get_pde_for_bo(struct amdgpu_bo *bo, int level,
    265  1.1  riastrad 			       uint64_t *addr, uint64_t *flags);
    266  1.1  riastrad int amdgpu_gmc_set_pte_pde(struct amdgpu_device *adev, void *cpu_pt_addr,
    267  1.1  riastrad 				uint32_t gpu_page_idx, uint64_t addr,
    268  1.1  riastrad 				uint64_t flags);
    269  1.1  riastrad uint64_t amdgpu_gmc_pd_addr(struct amdgpu_bo *bo);
    270  1.1  riastrad uint64_t amdgpu_gmc_agp_addr(struct ttm_buffer_object *bo);
    271  1.1  riastrad void amdgpu_gmc_vram_location(struct amdgpu_device *adev, struct amdgpu_gmc *mc,
    272  1.1  riastrad 			      u64 base);
    273  1.1  riastrad void amdgpu_gmc_gart_location(struct amdgpu_device *adev,
    274  1.1  riastrad 			      struct amdgpu_gmc *mc);
    275  1.1  riastrad void amdgpu_gmc_agp_location(struct amdgpu_device *adev,
    276  1.1  riastrad 			     struct amdgpu_gmc *mc);
    277  1.1  riastrad bool amdgpu_gmc_filter_faults(struct amdgpu_device *adev, uint64_t addr,
    278  1.1  riastrad 			      uint16_t pasid, uint64_t timestamp);
    279  1.1  riastrad int amdgpu_gmc_ras_late_init(struct amdgpu_device *adev);
    280  1.1  riastrad void amdgpu_gmc_ras_fini(struct amdgpu_device *adev);
    281  1.1  riastrad int amdgpu_gmc_allocate_vm_inv_eng(struct amdgpu_device *adev);
    282  1.1  riastrad 
    283  1.1  riastrad #endif
    284