1 /* SPDX-License-Identifier: MIT */
2 #ifndef __NOUVEAU_BO_H__
3 #define __NOUVEAU_BO_H__
4 
5 #include <drm/drm_gem.h>
6 
7 struct nouveau_channel;
8 struct nouveau_fence;
9 struct nvkm_vma;
10 
11 struct nouveau_bo {
12 	struct ttm_buffer_object bo;
13 	struct ttm_placement placement;
14 	u32 valid_domains;
15 	struct ttm_place placements[3];
16 	struct ttm_place busy_placements[3];
17 	bool force_coherent;
18 	struct ttm_bo_kmap_obj kmap;
19 	struct list_head head;
20 
21 	/* protected by ttm_bo_reserve() */
22 	struct drm_file *reserved_by;
23 	struct list_head entry;
24 	int pbbo_index;
25 	bool validate_mapped;
26 
27 	struct list_head vma_list;
28 
29 	unsigned contig:1;
30 	unsigned page:5;
31 	unsigned kind:8;
32 	unsigned comp:3;
33 	unsigned zeta:3;
34 	unsigned mode;
35 
36 	struct nouveau_drm_tile *tile;
37 
38 	/* protect by the ttm reservation lock */
39 	int pin_refcnt;
40 
41 	struct ttm_bo_kmap_obj dma_buf_vmap;
42 };
43 
44 static inline struct nouveau_bo *
45 nouveau_bo(struct ttm_buffer_object *bo)
46 {
47 	return container_of(bo, struct nouveau_bo, bo);
48 }
49 
50 static inline int
51 nouveau_bo_ref(struct nouveau_bo *ref, struct nouveau_bo **pnvbo)
52 {
53 	struct nouveau_bo *prev;
54 
55 	if (!pnvbo)
56 		return -EINVAL;
57 	prev = *pnvbo;
58 
59 	if (ref) {
60 		ttm_bo_get(&ref->bo);
61 		*pnvbo = nouveau_bo(&ref->bo);
62 	} else {
63 		*pnvbo = NULL;
64 	}
65 	if (prev)
66 		ttm_bo_put(&prev->bo);
67 
68 	return 0;
69 }
70 
71 extern struct ttm_bo_driver nouveau_bo_driver;
72 
73 void nouveau_bo_move_init(struct nouveau_drm *);
74 struct nouveau_bo *nouveau_bo_alloc(struct nouveau_cli *, u64 *size, int *align,
75 				    u32 flags, u32 tile_mode, u32 tile_flags);
76 int  nouveau_bo_init(struct nouveau_bo *, u64 size, int align, u32 flags,
77 		     struct sg_table *sg, struct dma_resv *robj);
78 int  nouveau_bo_new(struct nouveau_cli *, u64 size, int align, u32 flags,
79 		    u32 tile_mode, u32 tile_flags, struct sg_table *sg,
80 		    struct dma_resv *robj,
81 		    struct nouveau_bo **);
82 int  nouveau_bo_pin(struct nouveau_bo *, u32 flags, bool contig);
83 int  nouveau_bo_unpin(struct nouveau_bo *);
84 int  nouveau_bo_map(struct nouveau_bo *);
85 void nouveau_bo_unmap(struct nouveau_bo *);
86 void nouveau_bo_placement_set(struct nouveau_bo *, u32 type, u32 busy);
87 void nouveau_bo_wr16(struct nouveau_bo *, unsigned index, u16 val);
88 u32  nouveau_bo_rd32(struct nouveau_bo *, unsigned index);
89 void nouveau_bo_wr32(struct nouveau_bo *, unsigned index, u32 val);
90 void nouveau_bo_fence(struct nouveau_bo *, struct nouveau_fence *, bool exclusive);
91 int  nouveau_bo_validate(struct nouveau_bo *, bool interruptible,
92 			 bool no_wait_gpu);
93 void nouveau_bo_sync_for_device(struct nouveau_bo *nvbo);
94 void nouveau_bo_sync_for_cpu(struct nouveau_bo *nvbo);
95 
96 /* TODO: submit equivalent to TTM generic API upstream? */
97 static inline void __iomem *
98 nvbo_kmap_obj_iovirtual(struct nouveau_bo *nvbo)
99 {
100 	bool is_iomem;
101 	void __iomem *ioptr = (void __force __iomem *)ttm_kmap_obj_virtual(
102 						&nvbo->kmap, &is_iomem);
103 	WARN_ON_ONCE(ioptr && !is_iomem);
104 	return ioptr;
105 }
106 
107 static inline void
108 nouveau_bo_unmap_unpin_unref(struct nouveau_bo **pnvbo)
109 {
110 	if (*pnvbo) {
111 		nouveau_bo_unmap(*pnvbo);
112 		nouveau_bo_unpin(*pnvbo);
113 		nouveau_bo_ref(NULL, pnvbo);
114 	}
115 }
116 
117 static inline int
118 nouveau_bo_new_pin_map(struct nouveau_cli *cli, u64 size, int align, u32 flags,
119 		       struct nouveau_bo **pnvbo)
120 {
121 	int ret = nouveau_bo_new(cli, size, align, flags,
122 				 0, 0, NULL, NULL, pnvbo);
123 	if (ret == 0) {
124 		ret = nouveau_bo_pin(*pnvbo, flags, true);
125 		if (ret == 0) {
126 			ret = nouveau_bo_map(*pnvbo);
127 			if (ret == 0)
128 				return ret;
129 			nouveau_bo_unpin(*pnvbo);
130 		}
131 		nouveau_bo_ref(NULL, pnvbo);
132 	}
133 	return ret;
134 }
135 #endif
136