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 <linux/hmm.h> 10 #include "amdxdna_pci_drv.h" 11 12 struct amdxdna_umap { 13 struct vm_area_struct *vma; 14 struct mmu_interval_notifier notifier; 15 struct hmm_range range; 16 struct work_struct hmm_unreg_work; 17 struct amdxdna_gem_obj *abo; 18 struct list_head node; 19 struct kref refcnt; 20 bool invalid; 21 bool unmapped; 22 }; 23 24 struct amdxdna_mem { 25 u64 userptr; 26 void *kva; 27 u64 dev_addr; 28 size_t size; 29 struct page **pages; 30 u32 nr_pages; 31 struct list_head umap_list; 32 bool map_invalid; 33 }; 34 35 struct amdxdna_gem_obj { 36 struct drm_gem_shmem_object base; 37 struct amdxdna_client *client; 38 u8 type; 39 bool pinned; 40 struct mutex lock; /* Protects: pinned */ 41 struct amdxdna_mem mem; 42 43 /* Below members is uninitialized when needed */ 44 struct drm_mm mm; /* For AMDXDNA_BO_DEV_HEAP */ 45 struct drm_mm_node mm_node; /* For AMDXDNA_BO_DEV */ 46 u32 assigned_hwctx; 47 struct dma_buf *dma_buf; 48 struct dma_buf_attachment *attach; 49 }; 50 51 #define to_gobj(obj) (&(obj)->base.base) 52 #define is_import_bo(obj) ((obj)->attach) 53 54 static inline struct amdxdna_gem_obj *to_xdna_obj(struct drm_gem_object *gobj) 55 { 56 return container_of(gobj, struct amdxdna_gem_obj, base.base); 57 } 58 59 struct amdxdna_gem_obj *amdxdna_gem_get_obj(struct amdxdna_client *client, 60 u32 bo_hdl, u8 bo_type); 61 static inline void amdxdna_gem_put_obj(struct amdxdna_gem_obj *abo) 62 { 63 drm_gem_object_put(to_gobj(abo)); 64 } 65 66 static inline u64 amdxdna_dev_bo_offset(struct amdxdna_gem_obj *abo) 67 { 68 return abo->mem.dev_addr - abo->client->dev_heap->mem.dev_addr; 69 } 70 71 void amdxdna_umap_put(struct amdxdna_umap *mapp); 72 73 struct drm_gem_object * 74 amdxdna_gem_create_object_cb(struct drm_device *dev, size_t size); 75 struct drm_gem_object * 76 amdxdna_gem_prime_import(struct drm_device *dev, struct dma_buf *dma_buf); 77 struct amdxdna_gem_obj * 78 amdxdna_drm_alloc_dev_bo(struct drm_device *dev, 79 struct amdxdna_drm_create_bo *args, 80 struct drm_file *filp); 81 82 int amdxdna_gem_pin_nolock(struct amdxdna_gem_obj *abo); 83 int amdxdna_gem_pin(struct amdxdna_gem_obj *abo); 84 void amdxdna_gem_unpin(struct amdxdna_gem_obj *abo); 85 86 int amdxdna_drm_create_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *filp); 87 int amdxdna_drm_get_bo_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp); 88 int amdxdna_drm_sync_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *filp); 89 90 #endif /* _AMDXDNA_GEM_H_ */ 91