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