Merge branch 'mesa_7_7_branch'
[mesa.git] / src / gallium / drivers / nv50 / nv50_transfer.c
1
2 #include "pipe/p_context.h"
3 #include "pipe/p_inlines.h"
4 #include "util/u_format.h"
5 #include "util/u_math.h"
6
7 #include "nv50_context.h"
8
9 struct nv50_transfer {
10 struct pipe_transfer base;
11 struct nouveau_bo *bo;
12 unsigned level_offset;
13 unsigned level_tiling;
14 int level_pitch;
15 int level_width;
16 int level_height;
17 int level_depth;
18 int level_x;
19 int level_y;
20 int level_z;
21 unsigned nblocksx;
22 unsigned nblocksy;
23 };
24
25 static void
26 nv50_transfer_rect_m2mf(struct pipe_screen *pscreen,
27 struct nouveau_bo *src_bo, unsigned src_offset,
28 int src_pitch, unsigned src_tile_mode,
29 int sx, int sy, int sz, int sw, int sh, int sd,
30 struct nouveau_bo *dst_bo, unsigned dst_offset,
31 int dst_pitch, unsigned dst_tile_mode,
32 int dx, int dy, int dz, int dw, int dh, int dd,
33 int cpp, int width, int height,
34 unsigned src_reloc, unsigned dst_reloc)
35 {
36 struct nv50_screen *screen = nv50_screen(pscreen);
37 struct nouveau_channel *chan = screen->m2mf->channel;
38 struct nouveau_grobj *m2mf = screen->m2mf;
39
40 src_reloc |= NOUVEAU_BO_RD;
41 dst_reloc |= NOUVEAU_BO_WR;
42
43 WAIT_RING (chan, 14);
44
45 if (!src_bo->tile_flags) {
46 BEGIN_RING(chan, m2mf,
47 NV50_MEMORY_TO_MEMORY_FORMAT_LINEAR_IN, 1);
48 OUT_RING (chan, 1);
49 BEGIN_RING(chan, m2mf,
50 NV04_MEMORY_TO_MEMORY_FORMAT_PITCH_IN, 1);
51 OUT_RING (chan, src_pitch);
52 src_offset += (sy * src_pitch) + (sx * cpp);
53 } else {
54 BEGIN_RING(chan, m2mf,
55 NV50_MEMORY_TO_MEMORY_FORMAT_LINEAR_IN, 6);
56 OUT_RING (chan, 0);
57 OUT_RING (chan, src_tile_mode << 4);
58 OUT_RING (chan, sw * cpp);
59 OUT_RING (chan, sh);
60 OUT_RING (chan, sd);
61 OUT_RING (chan, sz); /* copying only 1 zslice per call */
62 }
63
64 if (!dst_bo->tile_flags) {
65 BEGIN_RING(chan, m2mf,
66 NV50_MEMORY_TO_MEMORY_FORMAT_LINEAR_OUT, 1);
67 OUT_RING (chan, 1);
68 BEGIN_RING(chan, m2mf,
69 NV04_MEMORY_TO_MEMORY_FORMAT_PITCH_OUT, 1);
70 OUT_RING (chan, dst_pitch);
71 dst_offset += (dy * dst_pitch) + (dx * cpp);
72 } else {
73 BEGIN_RING(chan, m2mf,
74 NV50_MEMORY_TO_MEMORY_FORMAT_LINEAR_OUT, 6);
75 OUT_RING (chan, 0);
76 OUT_RING (chan, dst_tile_mode << 4);
77 OUT_RING (chan, dw * cpp);
78 OUT_RING (chan, dh);
79 OUT_RING (chan, dd);
80 OUT_RING (chan, dz); /* copying only 1 zslice per call */
81 }
82
83 while (height) {
84 int line_count = height > 2047 ? 2047 : height;
85
86 MARK_RING (chan, 15, 4); /* flush on lack of space or relocs */
87 BEGIN_RING(chan, m2mf,
88 NV50_MEMORY_TO_MEMORY_FORMAT_OFFSET_IN_HIGH, 2);
89 OUT_RELOCh(chan, src_bo, src_offset, src_reloc);
90 OUT_RELOCh(chan, dst_bo, dst_offset, dst_reloc);
91 BEGIN_RING(chan, m2mf,
92 NV04_MEMORY_TO_MEMORY_FORMAT_OFFSET_IN, 2);
93 OUT_RELOCl(chan, src_bo, src_offset, src_reloc);
94 OUT_RELOCl(chan, dst_bo, dst_offset, dst_reloc);
95 if (src_bo->tile_flags) {
96 BEGIN_RING(chan, m2mf,
97 NV50_MEMORY_TO_MEMORY_FORMAT_TILING_POSITION_IN, 1);
98 OUT_RING (chan, (sy << 16) | (sx * cpp));
99 } else {
100 src_offset += (line_count * src_pitch);
101 }
102 if (dst_bo->tile_flags) {
103 BEGIN_RING(chan, m2mf,
104 NV50_MEMORY_TO_MEMORY_FORMAT_TILING_POSITION_OUT, 1);
105 OUT_RING (chan, (dy << 16) | (dx * cpp));
106 } else {
107 dst_offset += (line_count * dst_pitch);
108 }
109 BEGIN_RING(chan, m2mf,
110 NV04_MEMORY_TO_MEMORY_FORMAT_LINE_LENGTH_IN, 4);
111 OUT_RING (chan, width * cpp);
112 OUT_RING (chan, line_count);
113 OUT_RING (chan, 0x00000101);
114 OUT_RING (chan, 0);
115 FIRE_RING (chan);
116
117 height -= line_count;
118 sy += line_count;
119 dy += line_count;
120 }
121 }
122
123 static struct pipe_transfer *
124 nv50_transfer_new(struct pipe_screen *pscreen, struct pipe_texture *pt,
125 unsigned face, unsigned level, unsigned zslice,
126 enum pipe_transfer_usage usage,
127 unsigned x, unsigned y, unsigned w, unsigned h)
128 {
129 struct nouveau_device *dev = nouveau_screen(pscreen)->device;
130 struct nv50_miptree *mt = nv50_miptree(pt);
131 struct nv50_miptree_level *lvl = &mt->level[level];
132 struct nv50_transfer *tx;
133 unsigned nx, ny, image = 0;
134 int ret;
135
136 if (pt->target == PIPE_TEXTURE_CUBE)
137 image = face;
138
139 tx = CALLOC_STRUCT(nv50_transfer);
140 if (!tx)
141 return NULL;
142
143 pipe_texture_reference(&tx->base.texture, pt);
144 tx->nblocksx = util_format_get_nblocksx(pt->format, u_minify(pt->width0, level));
145 tx->nblocksy = util_format_get_nblocksy(pt->format, u_minify(pt->height0, level));
146 tx->base.width = w;
147 tx->base.height = h;
148 tx->base.stride = tx->nblocksx * util_format_get_blocksize(pt->format);
149 tx->base.usage = usage;
150
151 tx->level_pitch = lvl->pitch;
152 tx->level_width = u_minify(mt->base.base.width0, level);
153 tx->level_height = u_minify(mt->base.base.height0, level);
154 tx->level_depth = u_minify(mt->base.base.depth0, level);
155 tx->level_offset = lvl->image_offset[image];
156 tx->level_tiling = lvl->tile_mode;
157 tx->level_z = zslice;
158 tx->level_x = util_format_get_nblocksx(pt->format, x);
159 tx->level_y = util_format_get_nblocksy(pt->format, y);
160 ret = nouveau_bo_new(dev, NOUVEAU_BO_GART | NOUVEAU_BO_MAP, 0,
161 tx->nblocksy * tx->base.stride, &tx->bo);
162 if (ret) {
163 FREE(tx);
164 return NULL;
165 }
166
167 if (usage & PIPE_TRANSFER_READ) {
168 nx = util_format_get_nblocksx(pt->format, tx->base.width);
169 ny = util_format_get_nblocksy(pt->format, tx->base.height);
170
171 nv50_transfer_rect_m2mf(pscreen, mt->base.bo, tx->level_offset,
172 tx->level_pitch, tx->level_tiling,
173 x, y, zslice,
174 tx->nblocksx, tx->nblocksy,
175 tx->level_depth,
176 tx->bo, 0,
177 tx->base.stride, tx->bo->tile_mode,
178 0, 0, 0,
179 tx->nblocksx, tx->nblocksy, 1,
180 util_format_get_blocksize(pt->format), nx, ny,
181 NOUVEAU_BO_VRAM | NOUVEAU_BO_GART,
182 NOUVEAU_BO_GART);
183 }
184
185 return &tx->base;
186 }
187
188 static void
189 nv50_transfer_del(struct pipe_transfer *ptx)
190 {
191 struct nv50_transfer *tx = (struct nv50_transfer *)ptx;
192 struct nv50_miptree *mt = nv50_miptree(ptx->texture);
193 struct pipe_texture *pt = ptx->texture;
194
195 unsigned nx = util_format_get_nblocksx(pt->format, tx->base.width);
196 unsigned ny = util_format_get_nblocksy(pt->format, tx->base.height);
197
198 if (ptx->usage & PIPE_TRANSFER_WRITE) {
199 struct pipe_screen *pscreen = pt->screen;
200
201 nv50_transfer_rect_m2mf(pscreen, tx->bo, 0,
202 tx->base.stride, tx->bo->tile_mode,
203 0, 0, 0,
204 tx->nblocksx, tx->nblocksy, 1,
205 mt->base.bo, tx->level_offset,
206 tx->level_pitch, tx->level_tiling,
207 tx->level_x, tx->level_y, tx->level_z,
208 tx->nblocksx, tx->nblocksy,
209 tx->level_depth,
210 util_format_get_blocksize(pt->format), nx, ny,
211 NOUVEAU_BO_GART, NOUVEAU_BO_VRAM |
212 NOUVEAU_BO_GART);
213 }
214
215 nouveau_bo_ref(NULL, &tx->bo);
216 pipe_texture_reference(&ptx->texture, NULL);
217 FREE(ptx);
218 }
219
220 static void *
221 nv50_transfer_map(struct pipe_screen *pscreen, struct pipe_transfer *ptx)
222 {
223 struct nv50_transfer *tx = (struct nv50_transfer *)ptx;
224 unsigned flags = 0;
225 int ret;
226
227 if (ptx->usage & PIPE_TRANSFER_WRITE)
228 flags |= NOUVEAU_BO_WR;
229 if (ptx->usage & PIPE_TRANSFER_READ)
230 flags |= NOUVEAU_BO_RD;
231
232 ret = nouveau_bo_map(tx->bo, flags);
233 if (ret)
234 return NULL;
235 return tx->bo->map;
236 }
237
238 static void
239 nv50_transfer_unmap(struct pipe_screen *pscreen, struct pipe_transfer *ptx)
240 {
241 struct nv50_transfer *tx = (struct nv50_transfer *)ptx;
242
243 nouveau_bo_unmap(tx->bo);
244 }
245
246 void
247 nv50_transfer_init_screen_functions(struct pipe_screen *pscreen)
248 {
249 pscreen->get_tex_transfer = nv50_transfer_new;
250 pscreen->tex_transfer_destroy = nv50_transfer_del;
251 pscreen->transfer_map = nv50_transfer_map;
252 pscreen->transfer_unmap = nv50_transfer_unmap;
253 }
254
255 void
256 nv50_upload_sifc(struct nv50_context *nv50,
257 struct nouveau_bo *bo, unsigned dst_offset, unsigned reloc,
258 unsigned dst_format, int dst_w, int dst_h, int dst_pitch,
259 void *src, unsigned src_format, int src_pitch,
260 int x, int y, int w, int h, int cpp)
261 {
262 struct nouveau_channel *chan = nv50->screen->base.channel;
263 struct nouveau_grobj *eng2d = nv50->screen->eng2d;
264 struct nouveau_grobj *tesla = nv50->screen->tesla;
265 unsigned line_dwords = (w * cpp + 3) / 4;
266
267 reloc |= NOUVEAU_BO_WR;
268
269 MARK_RING (chan, 32, 2); /* flush on lack of space or relocs */
270
271 if (bo->tile_flags) {
272 BEGIN_RING(chan, eng2d, NV50_2D_DST_FORMAT, 5);
273 OUT_RING (chan, dst_format);
274 OUT_RING (chan, 0);
275 OUT_RING (chan, bo->tile_mode << 4);
276 OUT_RING (chan, 1);
277 OUT_RING (chan, 0);
278 } else {
279 BEGIN_RING(chan, eng2d, NV50_2D_DST_FORMAT, 2);
280 OUT_RING (chan, dst_format);
281 OUT_RING (chan, 1);
282 BEGIN_RING(chan, eng2d, NV50_2D_DST_PITCH, 1);
283 OUT_RING (chan, dst_pitch);
284 }
285
286 BEGIN_RING(chan, eng2d, NV50_2D_DST_WIDTH, 4);
287 OUT_RING (chan, dst_w);
288 OUT_RING (chan, dst_h);
289 OUT_RELOCh(chan, bo, dst_offset, reloc);
290 OUT_RELOCl(chan, bo, dst_offset, reloc);
291
292 /* NV50_2D_OPERATION_SRCCOPY assumed already set */
293
294 BEGIN_RING(chan, eng2d, NV50_2D_SIFC_BITMAP_ENABLE, 2);
295 OUT_RING (chan, 0);
296 OUT_RING (chan, src_format);
297 BEGIN_RING(chan, eng2d, NV50_2D_SIFC_WIDTH, 10);
298 OUT_RING (chan, w);
299 OUT_RING (chan, h);
300 OUT_RING (chan, 0);
301 OUT_RING (chan, 1);
302 OUT_RING (chan, 0);
303 OUT_RING (chan, 1);
304 OUT_RING (chan, 0);
305 OUT_RING (chan, x);
306 OUT_RING (chan, 0);
307 OUT_RING (chan, y);
308
309 while (h--) {
310 const uint32_t *p = src;
311 unsigned count = line_dwords;
312
313 while (count) {
314 unsigned nr = MIN2(count, 1792);
315
316 if (chan->pushbuf->remaining <= nr) {
317 FIRE_RING (chan);
318
319 BEGIN_RING(chan, eng2d,
320 NV50_2D_DST_ADDRESS_HIGH, 2);
321 OUT_RELOCh(chan, bo, dst_offset, reloc);
322 OUT_RELOCl(chan, bo, dst_offset, reloc);
323 }
324 assert(chan->pushbuf->remaining > nr);
325
326 BEGIN_RING(chan, eng2d,
327 NV50_2D_SIFC_DATA | (2 << 29), nr);
328 OUT_RINGp (chan, p, nr);
329
330 p += nr;
331 count -= nr;
332 }
333
334 src += src_pitch;
335 }
336
337 BEGIN_RING(chan, tesla, NV50TCL_CODE_CB_FLUSH, 1);
338 OUT_RING (chan, 0);
339 }