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