1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * Copyright (C) 2024, Advanced Micro Devices, Inc. 4 */ 5 6 #ifndef _AMDXDNA_GEM_H_ 7 #define _AMDXDNA_GEM_H_ 8 9 #include <drm/drm_gem_shmem_helper.h> 10 #include <linux/hmm.h> 11 #include <linux/iova.h> 12 #include "amdxdna_pci_drv.h" 13 14 struct amdxdna_umap { 15 struct mmu_interval_notifier notifier; 16 struct hmm_range range; 17 struct work_struct hmm_unreg_work; 18 struct amdxdna_gem_obj *abo; 19 struct list_head node; 20 struct kref refcnt; 21 bool invalid; 22 bool unmapped; 23 }; 24 25 struct amdxdna_mem { 26 void *kva; 27 u64 dma_addr; 28 size_t size; 29 struct list_head umap_list; 30 bool map_invalid; 31 /* 32 * Cache the first mmap uva as PASID addr, which can be accessed by driver 33 * without taking notifier_lock. 34 */ 35 u64 uva; 36 }; 37 38 struct amdxdna_gem_obj { 39 struct drm_gem_shmem_object base; 40 struct amdxdna_client *client; 41 u8 type; 42 bool pinned; 43 struct mutex lock; /* Protects: pinned, mem.kva, open_ref */ 44 struct amdxdna_mem mem; 45 int open_ref; 46 47 /* Below members are initialized when needed */ 48 struct drm_mm_node mm_node; /* For AMDXDNA_BO_DEV */ 49 u32 heap_start_id; 50 u32 heap_end_id; 51 u64 dev_addr; /* For heap bo */ 52 u32 assigned_hwctx; 53 struct dma_buf *dma_buf; 54 struct dma_buf_attachment *attach; 55 56 /* True, if BO is managed by XRT, not application */ 57 bool internal; 58 /* True, if BO is not exportable */ 59 bool private_buffer; 60 }; 61 62 #define to_gobj(obj) (&(obj)->base.base) 63 #define is_import_bo(obj) ((obj)->attach) 64 65 static inline struct amdxdna_gem_obj *to_xdna_obj(struct drm_gem_object *gobj) 66 { 67 return container_of(gobj, struct amdxdna_gem_obj, base.base); 68 } 69 70 struct amdxdna_gem_obj *amdxdna_gem_get_obj(struct amdxdna_client *client, 71 u32 bo_hdl, u8 bo_type); 72 static inline void amdxdna_gem_put_obj(struct amdxdna_gem_obj *abo) 73 { 74 drm_gem_object_put(to_gobj(abo)); 75 } 76 77 /* 78 * Obtain the user virtual address for accessing the BO. 79 * It can be used for device to access the BO when PASID is enabled. 80 */ 81 static inline u64 amdxdna_gem_uva(struct amdxdna_gem_obj *abo) 82 { 83 return abo->mem.uva; 84 } 85 86 void *amdxdna_gem_vmap(struct amdxdna_gem_obj *abo); 87 u64 amdxdna_gem_dev_addr(struct amdxdna_gem_obj *abo); 88 89 static inline u64 amdxdna_dev_bo_offset(struct amdxdna_gem_obj *abo) 90 { 91 return amdxdna_gem_dev_addr(abo) - to_xdna_dev(to_gobj(abo)->dev)->dev_info->dev_mem_base; 92 } 93 94 static inline u64 amdxdna_obj_dma_addr(struct amdxdna_gem_obj *abo) 95 { 96 /* 97 * amdxdna_gem_obj_open() calls amdxdna_dma_map_bo() only when PASID is 98 * off, leaving mem.dma_addr at AMDXDNA_INVALID_ADDR when PASID is on. 99 * Avoid dereferencing abo->client, which is cleared to NULL by 100 * amdxdna_gem_obj_close() while internal kernel references remain. 101 */ 102 return (abo->mem.dma_addr != AMDXDNA_INVALID_ADDR) ? 103 abo->mem.dma_addr : amdxdna_gem_uva(abo); 104 } 105 106 void amdxdna_umap_put(struct amdxdna_umap *mapp); 107 108 struct drm_gem_object * 109 amdxdna_gem_create_shmem_object_cb(struct drm_device *dev, size_t size); 110 struct drm_gem_object * 111 amdxdna_gem_prime_import(struct drm_device *dev, struct dma_buf *dma_buf); 112 struct amdxdna_gem_obj * 113 amdxdna_drm_create_dev_bo(struct drm_device *dev, 114 struct amdxdna_drm_create_bo *args, struct drm_file *filp); 115 116 int amdxdna_gem_pin_nolock(struct amdxdna_gem_obj *abo); 117 int amdxdna_gem_pin(struct amdxdna_gem_obj *abo); 118 void amdxdna_gem_unpin(struct amdxdna_gem_obj *abo); 119 120 int amdxdna_drm_create_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *filp); 121 int amdxdna_drm_get_bo_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp); 122 int amdxdna_drm_sync_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *filp); 123 int amdxdna_drm_get_bo_usage(struct drm_device *dev, struct amdxdna_drm_get_array *args); 124 125 #endif /* _AMDXDNA_GEM_H_ */ 126