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