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