xref: /linux/drivers/accel/amdxdna/amdxdna_gem.h (revision bed29492d413349e5b13f21936655064cdb63c91)
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_node		mm_node; /* For AMDXDNA_BO_DEV */
50 	u32				heap_start_id;
51 	u32				heap_end_id;
52 	u64				dev_addr; /* For heap bo */
53 	u32				assigned_hwctx;
54 	struct dma_buf			*dma_buf;
55 	struct dma_buf_attachment	*attach;
56 
57 	/* True, if BO is managed by XRT, not application */
58 	bool				internal;
59 	/* True, if BO is not exportable */
60 	bool				private_buffer;
61 };
62 
63 #define to_gobj(obj)    (&(obj)->base.base)
64 #define is_import_bo(obj) ((obj)->attach)
65 
66 static inline struct amdxdna_gem_obj *to_xdna_obj(struct drm_gem_object *gobj)
67 {
68 	return container_of(gobj, struct amdxdna_gem_obj, base.base);
69 }
70 
71 struct amdxdna_gem_obj *amdxdna_gem_get_obj(struct amdxdna_client *client,
72 					    u32 bo_hdl, u8 bo_type);
73 static inline void amdxdna_gem_put_obj(struct amdxdna_gem_obj *abo)
74 {
75 	drm_gem_object_put(to_gobj(abo));
76 }
77 
78 /*
79  * Obtain the user virtual address for accessing the BO.
80  * It can be used for device to access the BO when PASID is enabled.
81  */
82 static inline u64 amdxdna_gem_uva(struct amdxdna_gem_obj *abo)
83 {
84 	return abo->mem.uva;
85 }
86 
87 void *amdxdna_gem_vmap(struct amdxdna_gem_obj *abo);
88 u64 amdxdna_gem_dev_addr(struct amdxdna_gem_obj *abo);
89 
90 static inline u64 amdxdna_dev_bo_offset(struct amdxdna_gem_obj *abo)
91 {
92 	return amdxdna_gem_dev_addr(abo) - abo->client->xdna->dev_info->dev_mem_base;
93 }
94 
95 static inline u64 amdxdna_obj_dma_addr(struct amdxdna_gem_obj *abo)
96 {
97 	return amdxdna_pasid_on(abo->client) ? amdxdna_gem_uva(abo) : abo->mem.dma_addr;
98 }
99 
100 void amdxdna_umap_put(struct amdxdna_umap *mapp);
101 
102 struct drm_gem_object *
103 amdxdna_gem_create_shmem_object_cb(struct drm_device *dev, size_t size);
104 struct drm_gem_object *
105 amdxdna_gem_prime_import(struct drm_device *dev, struct dma_buf *dma_buf);
106 struct amdxdna_gem_obj *
107 amdxdna_drm_create_dev_bo(struct drm_device *dev,
108 			  struct amdxdna_drm_create_bo *args, struct drm_file *filp);
109 
110 int amdxdna_gem_pin_nolock(struct amdxdna_gem_obj *abo);
111 int amdxdna_gem_pin(struct amdxdna_gem_obj *abo);
112 void amdxdna_gem_unpin(struct amdxdna_gem_obj *abo);
113 
114 int amdxdna_drm_create_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *filp);
115 int amdxdna_drm_get_bo_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp);
116 int amdxdna_drm_sync_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *filp);
117 int amdxdna_drm_get_bo_usage(struct drm_device *dev, struct amdxdna_drm_get_array *args);
118 
119 #endif /* _AMDXDNA_GEM_H_ */
120