xref: /linux/drivers/accel/amdxdna/amdxdna_gem.h (revision 53597deca0e38c30e6cd4ba2114fa42d2bcd85bb)
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 vm_area_struct		*vma;
16 	struct mmu_interval_notifier	notifier;
17 	struct hmm_range		range;
18 	struct work_struct		hmm_unreg_work;
19 	struct amdxdna_gem_obj		*abo;
20 	struct list_head		node;
21 	struct kref			refcnt;
22 	bool				invalid;
23 	bool				unmapped;
24 };
25 
26 struct amdxdna_mem {
27 	void				*kva;
28 	u64				dma_addr;
29 	size_t				size;
30 	struct list_head		umap_list;
31 	bool				map_invalid;
32 	/*
33 	 * Cache the first mmap uva as PASID addr, which can be accessed by driver
34 	 * without taking notifier_lock.
35 	 */
36 	u64				uva;
37 };
38 
39 struct amdxdna_gem_obj {
40 	struct drm_gem_shmem_object	base;
41 	struct amdxdna_client		*client;
42 	u8				type;
43 	bool				pinned;
44 	struct mutex			lock; /* Protects: pinned, mem.kva, open_ref */
45 	struct amdxdna_mem		mem;
46 	int				open_ref;
47 
48 	/* Below members are initialized when needed */
49 	struct drm_mm			mm; /* For AMDXDNA_BO_DEV_HEAP */
50 	struct drm_mm_node		mm_node; /* For AMDXDNA_BO_DEV */
51 	u32				assigned_hwctx;
52 	struct dma_buf			*dma_buf;
53 	struct dma_buf_attachment	*attach;
54 
55 	/* True, if BO is managed by XRT, not application */
56 	bool				internal;
57 };
58 
59 #define to_gobj(obj)    (&(obj)->base.base)
60 #define is_import_bo(obj) ((obj)->attach)
61 
62 static inline struct amdxdna_gem_obj *to_xdna_obj(struct drm_gem_object *gobj)
63 {
64 	return container_of(gobj, struct amdxdna_gem_obj, base.base);
65 }
66 
67 struct amdxdna_gem_obj *amdxdna_gem_get_obj(struct amdxdna_client *client,
68 					    u32 bo_hdl, u8 bo_type);
69 static inline void amdxdna_gem_put_obj(struct amdxdna_gem_obj *abo)
70 {
71 	drm_gem_object_put(to_gobj(abo));
72 }
73 
74 void *amdxdna_gem_vmap(struct amdxdna_gem_obj *abo);
75 u64 amdxdna_gem_uva(struct amdxdna_gem_obj *abo);
76 u64 amdxdna_gem_dev_addr(struct amdxdna_gem_obj *abo);
77 
78 static inline u64 amdxdna_dev_bo_offset(struct amdxdna_gem_obj *abo)
79 {
80 	return amdxdna_gem_dev_addr(abo) - amdxdna_gem_dev_addr(abo->client->dev_heap);
81 }
82 
83 static inline u64 amdxdna_obj_dma_addr(struct amdxdna_gem_obj *abo)
84 {
85 	return amdxdna_pasid_on(abo->client) ? amdxdna_gem_uva(abo) : abo->mem.dma_addr;
86 }
87 
88 void amdxdna_umap_put(struct amdxdna_umap *mapp);
89 
90 struct drm_gem_object *
91 amdxdna_gem_create_shmem_object_cb(struct drm_device *dev, size_t size);
92 struct drm_gem_object *
93 amdxdna_gem_prime_import(struct drm_device *dev, struct dma_buf *dma_buf);
94 struct amdxdna_gem_obj *
95 amdxdna_drm_create_dev_bo(struct drm_device *dev,
96 			  struct amdxdna_drm_create_bo *args, struct drm_file *filp);
97 
98 int amdxdna_gem_pin_nolock(struct amdxdna_gem_obj *abo);
99 int amdxdna_gem_pin(struct amdxdna_gem_obj *abo);
100 void amdxdna_gem_unpin(struct amdxdna_gem_obj *abo);
101 
102 int amdxdna_drm_create_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *filp);
103 int amdxdna_drm_get_bo_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp);
104 int amdxdna_drm_sync_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *filp);
105 int amdxdna_drm_get_bo_usage(struct drm_device *dev, struct amdxdna_drm_get_array *args);
106 
107 #endif /* _AMDXDNA_GEM_H_ */
108