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