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 /* True, if BO is not exportable */
58 bool private_buffer;
59 };
60
61 #define to_gobj(obj) (&(obj)->base.base)
62 #define is_import_bo(obj) ((obj)->attach)
63
to_xdna_obj(struct drm_gem_object * gobj)64 static inline struct amdxdna_gem_obj *to_xdna_obj(struct drm_gem_object *gobj)
65 {
66 return container_of(gobj, struct amdxdna_gem_obj, base.base);
67 }
68
69 struct amdxdna_gem_obj *amdxdna_gem_get_obj(struct amdxdna_client *client,
70 u32 bo_hdl, u8 bo_type);
amdxdna_gem_put_obj(struct amdxdna_gem_obj * abo)71 static inline void amdxdna_gem_put_obj(struct amdxdna_gem_obj *abo)
72 {
73 drm_gem_object_put(to_gobj(abo));
74 }
75
76 void *amdxdna_gem_vmap(struct amdxdna_gem_obj *abo);
77 u64 amdxdna_gem_uva(struct amdxdna_gem_obj *abo);
78 u64 amdxdna_gem_dev_addr(struct amdxdna_gem_obj *abo);
79
amdxdna_dev_bo_offset(struct amdxdna_gem_obj * abo)80 static inline u64 amdxdna_dev_bo_offset(struct amdxdna_gem_obj *abo)
81 {
82 return amdxdna_gem_dev_addr(abo) - amdxdna_gem_dev_addr(abo->client->dev_heap);
83 }
84
amdxdna_obj_dma_addr(struct amdxdna_gem_obj * abo)85 static inline u64 amdxdna_obj_dma_addr(struct amdxdna_gem_obj *abo)
86 {
87 return amdxdna_pasid_on(abo->client) ? amdxdna_gem_uva(abo) : abo->mem.dma_addr;
88 }
89
90 void amdxdna_umap_put(struct amdxdna_umap *mapp);
91
92 struct drm_gem_object *
93 amdxdna_gem_create_shmem_object_cb(struct drm_device *dev, size_t size);
94 struct drm_gem_object *
95 amdxdna_gem_prime_import(struct drm_device *dev, struct dma_buf *dma_buf);
96 struct amdxdna_gem_obj *
97 amdxdna_drm_create_dev_bo(struct drm_device *dev,
98 struct amdxdna_drm_create_bo *args, struct drm_file *filp);
99
100 int amdxdna_gem_pin_nolock(struct amdxdna_gem_obj *abo);
101 int amdxdna_gem_pin(struct amdxdna_gem_obj *abo);
102 void amdxdna_gem_unpin(struct amdxdna_gem_obj *abo);
103
104 int amdxdna_drm_create_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *filp);
105 int amdxdna_drm_get_bo_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp);
106 int amdxdna_drm_sync_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *filp);
107 int amdxdna_drm_get_bo_usage(struct drm_device *dev, struct amdxdna_drm_get_array *args);
108
109 #endif /* _AMDXDNA_GEM_H_ */
110