xref: /linux/drivers/gpu/drm/nouveau/nouveau_bo.h (revision 1f20a5769446a1acae67ac9e63d07a594829a789)
1 /* SPDX-License-Identifier: MIT */
2 #ifndef __NOUVEAU_BO_H__
3 #define __NOUVEAU_BO_H__
4 #include <drm/drm_gem.h>
5 #include <drm/ttm/ttm_bo.h>
6 #include <drm/ttm/ttm_placement.h>
7 
8 struct nouveau_channel;
9 struct nouveau_cli;
10 struct nouveau_drm;
11 struct nouveau_fence;
12 
13 struct nouveau_bo {
14 	struct ttm_buffer_object bo;
15 	struct ttm_placement placement;
16 	u32 valid_domains;
17 	struct ttm_place placements[3];
18 	bool force_coherent;
19 	struct ttm_bo_kmap_obj kmap;
20 	struct list_head head;
21 	struct list_head io_reserve_lru;
22 
23 	/* protected by ttm_bo_reserve() */
24 	struct drm_file *reserved_by;
25 	struct list_head entry;
26 	int pbbo_index;
27 	bool validate_mapped;
28 
29 	/* Root GEM object we derive the dma_resv of in case this BO is not
30 	 * shared between VMs.
31 	 */
32 	struct drm_gem_object *r_obj;
33 	bool no_share;
34 
35 	/* GPU address space is independent of CPU word size */
36 	uint64_t offset;
37 
38 	struct list_head vma_list;
39 
40 	unsigned contig:1;
41 	unsigned page:5;
42 	unsigned kind:8;
43 	unsigned comp:3;
44 	unsigned zeta:3;
45 	unsigned mode;
46 
47 	struct nouveau_drm_tile *tile;
48 };
49 
50 static inline struct nouveau_bo *
51 nouveau_bo(struct ttm_buffer_object *bo)
52 {
53 	return container_of(bo, struct nouveau_bo, bo);
54 }
55 
56 static inline int
57 nouveau_bo_ref(struct nouveau_bo *ref, struct nouveau_bo **pnvbo)
58 {
59 	struct nouveau_bo *prev;
60 
61 	if (!pnvbo)
62 		return -EINVAL;
63 	prev = *pnvbo;
64 
65 	if (ref) {
66 		ttm_bo_get(&ref->bo);
67 		*pnvbo = nouveau_bo(&ref->bo);
68 	} else {
69 		*pnvbo = NULL;
70 	}
71 	if (prev)
72 		ttm_bo_put(&prev->bo);
73 
74 	return 0;
75 }
76 
77 extern struct ttm_device_funcs nouveau_bo_driver;
78 
79 void nouveau_bo_move_init(struct nouveau_drm *);
80 struct nouveau_bo *nouveau_bo_alloc(struct nouveau_cli *, u64 *size, int *align,
81 				    u32 domain, u32 tile_mode, u32 tile_flags, bool internal);
82 int  nouveau_bo_init(struct nouveau_bo *, u64 size, int align, u32 domain,
83 		     struct sg_table *sg, struct dma_resv *robj);
84 int  nouveau_bo_new(struct nouveau_cli *, u64 size, int align, u32 domain,
85 		    u32 tile_mode, u32 tile_flags, struct sg_table *sg,
86 		    struct dma_resv *robj,
87 		    struct nouveau_bo **);
88 int  nouveau_bo_pin(struct nouveau_bo *, u32 flags, bool contig);
89 int  nouveau_bo_unpin(struct nouveau_bo *);
90 int  nouveau_bo_map(struct nouveau_bo *);
91 void nouveau_bo_unmap(struct nouveau_bo *);
92 void nouveau_bo_placement_set(struct nouveau_bo *, u32 type, u32 busy);
93 void nouveau_bo_wr16(struct nouveau_bo *, unsigned index, u16 val);
94 u32  nouveau_bo_rd32(struct nouveau_bo *, unsigned index);
95 void nouveau_bo_wr32(struct nouveau_bo *, unsigned index, u32 val);
96 vm_fault_t nouveau_ttm_fault_reserve_notify(struct ttm_buffer_object *bo);
97 void nouveau_bo_fence(struct nouveau_bo *, struct nouveau_fence *, bool exclusive);
98 int  nouveau_bo_validate(struct nouveau_bo *, bool interruptible,
99 			 bool no_wait_gpu);
100 void nouveau_bo_sync_for_device(struct nouveau_bo *nvbo);
101 void nouveau_bo_sync_for_cpu(struct nouveau_bo *nvbo);
102 void nouveau_bo_add_io_reserve_lru(struct ttm_buffer_object *bo);
103 void nouveau_bo_del_io_reserve_lru(struct ttm_buffer_object *bo);
104 
105 /* TODO: submit equivalent to TTM generic API upstream? */
106 static inline void __iomem *
107 nvbo_kmap_obj_iovirtual(struct nouveau_bo *nvbo)
108 {
109 	bool is_iomem;
110 	void __iomem *ioptr = (void __force __iomem *)ttm_kmap_obj_virtual(
111 						&nvbo->kmap, &is_iomem);
112 	WARN_ON_ONCE(ioptr && !is_iomem);
113 	return ioptr;
114 }
115 
116 static inline void
117 nouveau_bo_unmap_unpin_unref(struct nouveau_bo **pnvbo)
118 {
119 	if (*pnvbo) {
120 		nouveau_bo_unmap(*pnvbo);
121 		nouveau_bo_unpin(*pnvbo);
122 		nouveau_bo_ref(NULL, pnvbo);
123 	}
124 }
125 
126 static inline int
127 nouveau_bo_new_pin_map(struct nouveau_cli *cli, u64 size, int align, u32 domain,
128 		       struct nouveau_bo **pnvbo)
129 {
130 	int ret = nouveau_bo_new(cli, size, align, domain,
131 				 0, 0, NULL, NULL, pnvbo);
132 	if (ret == 0) {
133 		ret = nouveau_bo_pin(*pnvbo, domain, true);
134 		if (ret == 0) {
135 			ret = nouveau_bo_map(*pnvbo);
136 			if (ret == 0)
137 				return ret;
138 			nouveau_bo_unpin(*pnvbo);
139 		}
140 		nouveau_bo_ref(NULL, pnvbo);
141 	}
142 	return ret;
143 }
144 
145 int nv04_bo_move_init(struct nouveau_channel *, u32);
146 int nv04_bo_move_m2mf(struct nouveau_channel *, struct ttm_buffer_object *,
147 		      struct ttm_resource *, struct ttm_resource *);
148 
149 int nv50_bo_move_init(struct nouveau_channel *, u32);
150 int nv50_bo_move_m2mf(struct nouveau_channel *, struct ttm_buffer_object *,
151 		      struct ttm_resource *, struct ttm_resource *);
152 
153 int nv84_bo_move_exec(struct nouveau_channel *, struct ttm_buffer_object *,
154 		      struct ttm_resource *, struct ttm_resource *);
155 
156 int nva3_bo_move_copy(struct nouveau_channel *, struct ttm_buffer_object *,
157 		      struct ttm_resource *, struct ttm_resource *);
158 
159 int nvc0_bo_move_init(struct nouveau_channel *, u32);
160 int nvc0_bo_move_m2mf(struct nouveau_channel *, struct ttm_buffer_object *,
161 		      struct ttm_resource *, struct ttm_resource *);
162 
163 int nvc0_bo_move_copy(struct nouveau_channel *, struct ttm_buffer_object *,
164 		      struct ttm_resource *, struct ttm_resource *);
165 
166 int nve0_bo_move_init(struct nouveau_channel *, u32);
167 int nve0_bo_move_copy(struct nouveau_channel *, struct ttm_buffer_object *,
168 		      struct ttm_resource *, struct ttm_resource *);
169 
170 #define NVBO_WR32_(b,o,dr,f) nouveau_bo_wr32((b), (o)/4 + (dr), (f))
171 #define NVBO_RD32_(b,o,dr)   nouveau_bo_rd32((b), (o)/4 + (dr))
172 #define NVBO_RD32(A...) DRF_RD(NVBO_RD32_,                  ##A)
173 #define NVBO_RV32(A...) DRF_RV(NVBO_RD32_,                  ##A)
174 #define NVBO_TV32(A...) DRF_TV(NVBO_RD32_,                  ##A)
175 #define NVBO_TD32(A...) DRF_TD(NVBO_RD32_,                  ##A)
176 #define NVBO_WR32(A...) DRF_WR(            NVBO_WR32_,      ##A)
177 #define NVBO_WV32(A...) DRF_WV(            NVBO_WR32_,      ##A)
178 #define NVBO_WD32(A...) DRF_WD(            NVBO_WR32_,      ##A)
179 #define NVBO_MR32(A...) DRF_MR(NVBO_RD32_, NVBO_WR32_, u32, ##A)
180 #define NVBO_MV32(A...) DRF_MV(NVBO_RD32_, NVBO_WR32_, u32, ##A)
181 #define NVBO_MD32(A...) DRF_MD(NVBO_RD32_, NVBO_WR32_, u32, ##A)
182 #endif
183