blob: 661fec2a2cc144bf16df85171e7aa498352e84b7 [file] [log] [blame]
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001/*
2 * Copyright 2009 Advanced Micro Devices, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Alex Deucher <alexander.deucher@amd.com>
25 */
26#include "drmP.h"
27#include "drm.h"
28#include "radeon_drm.h"
29#include "radeon_drv.h"
30
31#include "r600_blit_shaders.h"
32
33#define DI_PT_RECTLIST 0x11
34#define DI_INDEX_SIZE_16_BIT 0x0
35#define DI_SRC_SEL_AUTO_INDEX 0x2
36
37#define FMT_8 0x1
38#define FMT_5_6_5 0x8
39#define FMT_8_8_8_8 0x1a
40#define COLOR_8 0x1
41#define COLOR_5_6_5 0x8
42#define COLOR_8_8_8_8 0x1a
43
Andi Kleen74740c82011-10-13 16:08:43 -070044static void
Jerome Glisse3ce0a232009-09-08 10:10:24 +100045set_render_target(drm_radeon_private_t *dev_priv, int format, int w, int h, u64 gpu_addr)
46{
47 u32 cb_color_info;
48 int pitch, slice;
49 RING_LOCALS;
50 DRM_DEBUG("\n");
51
Matt Turnerd964fc52010-02-25 04:23:31 +000052 h = ALIGN(h, 8);
Jerome Glisse3ce0a232009-09-08 10:10:24 +100053 if (h < 8)
54 h = 8;
55
56 cb_color_info = ((format << 2) | (1 << 27));
57 pitch = (w / 8) - 1;
58 slice = ((w * h) / 64) - 1;
59
60 if (((dev_priv->flags & RADEON_FAMILY_MASK) > CHIP_R600) &&
61 ((dev_priv->flags & RADEON_FAMILY_MASK) < CHIP_RV770)) {
62 BEGIN_RING(21 + 2);
63 OUT_RING(CP_PACKET3(R600_IT_SET_CONTEXT_REG, 1));
64 OUT_RING((R600_CB_COLOR0_BASE - R600_SET_CONTEXT_REG_OFFSET) >> 2);
65 OUT_RING(gpu_addr >> 8);
66 OUT_RING(CP_PACKET3(R600_IT_SURFACE_BASE_UPDATE, 0));
67 OUT_RING(2 << 0);
68 } else {
69 BEGIN_RING(21);
70 OUT_RING(CP_PACKET3(R600_IT_SET_CONTEXT_REG, 1));
71 OUT_RING((R600_CB_COLOR0_BASE - R600_SET_CONTEXT_REG_OFFSET) >> 2);
72 OUT_RING(gpu_addr >> 8);
73 }
74
75 OUT_RING(CP_PACKET3(R600_IT_SET_CONTEXT_REG, 1));
76 OUT_RING((R600_CB_COLOR0_SIZE - R600_SET_CONTEXT_REG_OFFSET) >> 2);
77 OUT_RING((pitch << 0) | (slice << 10));
78
79 OUT_RING(CP_PACKET3(R600_IT_SET_CONTEXT_REG, 1));
80 OUT_RING((R600_CB_COLOR0_VIEW - R600_SET_CONTEXT_REG_OFFSET) >> 2);
81 OUT_RING(0);
82
83 OUT_RING(CP_PACKET3(R600_IT_SET_CONTEXT_REG, 1));
84 OUT_RING((R600_CB_COLOR0_INFO - R600_SET_CONTEXT_REG_OFFSET) >> 2);
85 OUT_RING(cb_color_info);
86
87 OUT_RING(CP_PACKET3(R600_IT_SET_CONTEXT_REG, 1));
88 OUT_RING((R600_CB_COLOR0_TILE - R600_SET_CONTEXT_REG_OFFSET) >> 2);
89 OUT_RING(0);
90
91 OUT_RING(CP_PACKET3(R600_IT_SET_CONTEXT_REG, 1));
92 OUT_RING((R600_CB_COLOR0_FRAG - R600_SET_CONTEXT_REG_OFFSET) >> 2);
93 OUT_RING(0);
94
95 OUT_RING(CP_PACKET3(R600_IT_SET_CONTEXT_REG, 1));
96 OUT_RING((R600_CB_COLOR0_MASK - R600_SET_CONTEXT_REG_OFFSET) >> 2);
97 OUT_RING(0);
98
99 ADVANCE_RING();
100}
101
Andi Kleen74740c82011-10-13 16:08:43 -0700102static void
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000103cp_set_surface_sync(drm_radeon_private_t *dev_priv,
104 u32 sync_type, u32 size, u64 mc_addr)
105{
106 u32 cp_coher_size;
107 RING_LOCALS;
108 DRM_DEBUG("\n");
109
110 if (size == 0xffffffff)
111 cp_coher_size = 0xffffffff;
112 else
113 cp_coher_size = ((size + 255) >> 8);
114
115 BEGIN_RING(5);
116 OUT_RING(CP_PACKET3(R600_IT_SURFACE_SYNC, 3));
117 OUT_RING(sync_type);
118 OUT_RING(cp_coher_size);
119 OUT_RING((mc_addr >> 8));
120 OUT_RING(10); /* poll interval */
121 ADVANCE_RING();
122}
123
Andi Kleen74740c82011-10-13 16:08:43 -0700124static void
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000125set_shaders(struct drm_device *dev)
126{
127 drm_radeon_private_t *dev_priv = dev->dev_private;
128 u64 gpu_addr;
Alex Deucher5d93b1352009-09-09 16:09:36 -0400129 int i;
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000130 u32 *vs, *ps;
131 uint32_t sq_pgm_resources;
132 RING_LOCALS;
133 DRM_DEBUG("\n");
134
135 /* load shaders */
136 vs = (u32 *) ((char *)dev->agp_buffer_map->handle + dev_priv->blit_vb->offset);
137 ps = (u32 *) ((char *)dev->agp_buffer_map->handle + dev_priv->blit_vb->offset + 256);
138
Alex Deucher5d93b1352009-09-09 16:09:36 -0400139 for (i = 0; i < r6xx_vs_size; i++)
Cédric Canodee54c42011-02-11 19:45:36 -0500140 vs[i] = cpu_to_le32(r6xx_vs[i]);
Alex Deucher5d93b1352009-09-09 16:09:36 -0400141 for (i = 0; i < r6xx_ps_size; i++)
Cédric Canodee54c42011-02-11 19:45:36 -0500142 ps[i] = cpu_to_le32(r6xx_ps[i]);
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000143
144 dev_priv->blit_vb->used = 512;
145
146 gpu_addr = dev_priv->gart_buffers_offset + dev_priv->blit_vb->offset;
147
148 /* setup shader regs */
149 sq_pgm_resources = (1 << 0);
150
151 BEGIN_RING(9 + 12);
152 /* VS */
153 OUT_RING(CP_PACKET3(R600_IT_SET_CONTEXT_REG, 1));
154 OUT_RING((R600_SQ_PGM_START_VS - R600_SET_CONTEXT_REG_OFFSET) >> 2);
155 OUT_RING(gpu_addr >> 8);
156
157 OUT_RING(CP_PACKET3(R600_IT_SET_CONTEXT_REG, 1));
158 OUT_RING((R600_SQ_PGM_RESOURCES_VS - R600_SET_CONTEXT_REG_OFFSET) >> 2);
159 OUT_RING(sq_pgm_resources);
160
161 OUT_RING(CP_PACKET3(R600_IT_SET_CONTEXT_REG, 1));
162 OUT_RING((R600_SQ_PGM_CF_OFFSET_VS - R600_SET_CONTEXT_REG_OFFSET) >> 2);
163 OUT_RING(0);
164
165 /* PS */
166 OUT_RING(CP_PACKET3(R600_IT_SET_CONTEXT_REG, 1));
167 OUT_RING((R600_SQ_PGM_START_PS - R600_SET_CONTEXT_REG_OFFSET) >> 2);
168 OUT_RING((gpu_addr + 256) >> 8);
169
170 OUT_RING(CP_PACKET3(R600_IT_SET_CONTEXT_REG, 1));
171 OUT_RING((R600_SQ_PGM_RESOURCES_PS - R600_SET_CONTEXT_REG_OFFSET) >> 2);
172 OUT_RING(sq_pgm_resources | (1 << 28));
173
174 OUT_RING(CP_PACKET3(R600_IT_SET_CONTEXT_REG, 1));
175 OUT_RING((R600_SQ_PGM_EXPORTS_PS - R600_SET_CONTEXT_REG_OFFSET) >> 2);
176 OUT_RING(2);
177
178 OUT_RING(CP_PACKET3(R600_IT_SET_CONTEXT_REG, 1));
179 OUT_RING((R600_SQ_PGM_CF_OFFSET_PS - R600_SET_CONTEXT_REG_OFFSET) >> 2);
180 OUT_RING(0);
181 ADVANCE_RING();
182
183 cp_set_surface_sync(dev_priv,
184 R600_SH_ACTION_ENA, 512, gpu_addr);
185}
186
Andi Kleen74740c82011-10-13 16:08:43 -0700187static void
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000188set_vtx_resource(drm_radeon_private_t *dev_priv, u64 gpu_addr)
189{
190 uint32_t sq_vtx_constant_word2;
191 RING_LOCALS;
192 DRM_DEBUG("\n");
193
194 sq_vtx_constant_word2 = (((gpu_addr >> 32) & 0xff) | (16 << 8));
Cédric Canodee54c42011-02-11 19:45:36 -0500195#ifdef __BIG_ENDIAN
196 sq_vtx_constant_word2 |= (2 << 30);
197#endif
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000198
199 BEGIN_RING(9);
200 OUT_RING(CP_PACKET3(R600_IT_SET_RESOURCE, 7));
201 OUT_RING(0x460);
202 OUT_RING(gpu_addr & 0xffffffff);
203 OUT_RING(48 - 1);
204 OUT_RING(sq_vtx_constant_word2);
205 OUT_RING(1 << 0);
206 OUT_RING(0);
207 OUT_RING(0);
208 OUT_RING(R600_SQ_TEX_VTX_VALID_BUFFER << 30);
209 ADVANCE_RING();
210
211 if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV610) ||
212 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV620) ||
213 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS780) ||
214 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS880) ||
215 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV710))
216 cp_set_surface_sync(dev_priv,
217 R600_TC_ACTION_ENA, 48, gpu_addr);
218 else
219 cp_set_surface_sync(dev_priv,
220 R600_VC_ACTION_ENA, 48, gpu_addr);
221}
222
Andi Kleen74740c82011-10-13 16:08:43 -0700223static void
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000224set_tex_resource(drm_radeon_private_t *dev_priv,
225 int format, int w, int h, int pitch, u64 gpu_addr)
226{
227 uint32_t sq_tex_resource_word0, sq_tex_resource_word1, sq_tex_resource_word4;
228 RING_LOCALS;
229 DRM_DEBUG("\n");
230
231 if (h < 1)
232 h = 1;
233
234 sq_tex_resource_word0 = (1 << 0);
235 sq_tex_resource_word0 |= ((((pitch >> 3) - 1) << 8) |
236 ((w - 1) << 19));
237
238 sq_tex_resource_word1 = (format << 26);
239 sq_tex_resource_word1 |= ((h - 1) << 0);
240
241 sq_tex_resource_word4 = ((1 << 14) |
242 (0 << 16) |
243 (1 << 19) |
244 (2 << 22) |
245 (3 << 25));
246
247 BEGIN_RING(9);
248 OUT_RING(CP_PACKET3(R600_IT_SET_RESOURCE, 7));
249 OUT_RING(0);
250 OUT_RING(sq_tex_resource_word0);
251 OUT_RING(sq_tex_resource_word1);
252 OUT_RING(gpu_addr >> 8);
253 OUT_RING(gpu_addr >> 8);
254 OUT_RING(sq_tex_resource_word4);
255 OUT_RING(0);
256 OUT_RING(R600_SQ_TEX_VTX_VALID_TEXTURE << 30);
257 ADVANCE_RING();
258
259}
260
Andi Kleen74740c82011-10-13 16:08:43 -0700261static void
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000262set_scissors(drm_radeon_private_t *dev_priv, int x1, int y1, int x2, int y2)
263{
264 RING_LOCALS;
265 DRM_DEBUG("\n");
266
267 BEGIN_RING(12);
268 OUT_RING(CP_PACKET3(R600_IT_SET_CONTEXT_REG, 2));
269 OUT_RING((R600_PA_SC_SCREEN_SCISSOR_TL - R600_SET_CONTEXT_REG_OFFSET) >> 2);
270 OUT_RING((x1 << 0) | (y1 << 16));
271 OUT_RING((x2 << 0) | (y2 << 16));
272
273 OUT_RING(CP_PACKET3(R600_IT_SET_CONTEXT_REG, 2));
274 OUT_RING((R600_PA_SC_GENERIC_SCISSOR_TL - R600_SET_CONTEXT_REG_OFFSET) >> 2);
275 OUT_RING((x1 << 0) | (y1 << 16) | (1 << 31));
276 OUT_RING((x2 << 0) | (y2 << 16));
277
278 OUT_RING(CP_PACKET3(R600_IT_SET_CONTEXT_REG, 2));
279 OUT_RING((R600_PA_SC_WINDOW_SCISSOR_TL - R600_SET_CONTEXT_REG_OFFSET) >> 2);
280 OUT_RING((x1 << 0) | (y1 << 16) | (1 << 31));
281 OUT_RING((x2 << 0) | (y2 << 16));
282 ADVANCE_RING();
283}
284
Andi Kleen74740c82011-10-13 16:08:43 -0700285static void
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000286draw_auto(drm_radeon_private_t *dev_priv)
287{
288 RING_LOCALS;
289 DRM_DEBUG("\n");
290
291 BEGIN_RING(10);
292 OUT_RING(CP_PACKET3(R600_IT_SET_CONFIG_REG, 1));
293 OUT_RING((R600_VGT_PRIMITIVE_TYPE - R600_SET_CONFIG_REG_OFFSET) >> 2);
294 OUT_RING(DI_PT_RECTLIST);
295
296 OUT_RING(CP_PACKET3(R600_IT_INDEX_TYPE, 0));
Cédric Canodee54c42011-02-11 19:45:36 -0500297#ifdef __BIG_ENDIAN
298 OUT_RING((2 << 2) | DI_INDEX_SIZE_16_BIT);
299#else
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000300 OUT_RING(DI_INDEX_SIZE_16_BIT);
Cédric Canodee54c42011-02-11 19:45:36 -0500301#endif
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000302
303 OUT_RING(CP_PACKET3(R600_IT_NUM_INSTANCES, 0));
304 OUT_RING(1);
305
306 OUT_RING(CP_PACKET3(R600_IT_DRAW_INDEX_AUTO, 1));
307 OUT_RING(3);
308 OUT_RING(DI_SRC_SEL_AUTO_INDEX);
309
310 ADVANCE_RING();
311 COMMIT_RING();
312}
313
Andi Kleen74740c82011-10-13 16:08:43 -0700314static void
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000315set_default_state(drm_radeon_private_t *dev_priv)
316{
Alex Deucher5d93b1352009-09-09 16:09:36 -0400317 int i;
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000318 u32 sq_config, sq_gpr_resource_mgmt_1, sq_gpr_resource_mgmt_2;
319 u32 sq_thread_resource_mgmt, sq_stack_resource_mgmt_1, sq_stack_resource_mgmt_2;
320 int num_ps_gprs, num_vs_gprs, num_temp_gprs, num_gs_gprs, num_es_gprs;
321 int num_ps_threads, num_vs_threads, num_gs_threads, num_es_threads;
322 int num_ps_stack_entries, num_vs_stack_entries, num_gs_stack_entries, num_es_stack_entries;
323 RING_LOCALS;
324
325 switch ((dev_priv->flags & RADEON_FAMILY_MASK)) {
326 case CHIP_R600:
327 num_ps_gprs = 192;
328 num_vs_gprs = 56;
329 num_temp_gprs = 4;
330 num_gs_gprs = 0;
331 num_es_gprs = 0;
332 num_ps_threads = 136;
333 num_vs_threads = 48;
334 num_gs_threads = 4;
335 num_es_threads = 4;
336 num_ps_stack_entries = 128;
337 num_vs_stack_entries = 128;
338 num_gs_stack_entries = 0;
339 num_es_stack_entries = 0;
340 break;
341 case CHIP_RV630:
342 case CHIP_RV635:
343 num_ps_gprs = 84;
344 num_vs_gprs = 36;
345 num_temp_gprs = 4;
346 num_gs_gprs = 0;
347 num_es_gprs = 0;
348 num_ps_threads = 144;
349 num_vs_threads = 40;
350 num_gs_threads = 4;
351 num_es_threads = 4;
352 num_ps_stack_entries = 40;
353 num_vs_stack_entries = 40;
354 num_gs_stack_entries = 32;
355 num_es_stack_entries = 16;
356 break;
357 case CHIP_RV610:
358 case CHIP_RV620:
359 case CHIP_RS780:
360 case CHIP_RS880:
361 default:
362 num_ps_gprs = 84;
363 num_vs_gprs = 36;
364 num_temp_gprs = 4;
365 num_gs_gprs = 0;
366 num_es_gprs = 0;
367 num_ps_threads = 136;
368 num_vs_threads = 48;
369 num_gs_threads = 4;
370 num_es_threads = 4;
371 num_ps_stack_entries = 40;
372 num_vs_stack_entries = 40;
373 num_gs_stack_entries = 32;
374 num_es_stack_entries = 16;
375 break;
376 case CHIP_RV670:
377 num_ps_gprs = 144;
378 num_vs_gprs = 40;
379 num_temp_gprs = 4;
380 num_gs_gprs = 0;
381 num_es_gprs = 0;
382 num_ps_threads = 136;
383 num_vs_threads = 48;
384 num_gs_threads = 4;
385 num_es_threads = 4;
386 num_ps_stack_entries = 40;
387 num_vs_stack_entries = 40;
388 num_gs_stack_entries = 32;
389 num_es_stack_entries = 16;
390 break;
391 case CHIP_RV770:
392 num_ps_gprs = 192;
393 num_vs_gprs = 56;
394 num_temp_gprs = 4;
395 num_gs_gprs = 0;
396 num_es_gprs = 0;
397 num_ps_threads = 188;
398 num_vs_threads = 60;
399 num_gs_threads = 0;
400 num_es_threads = 0;
401 num_ps_stack_entries = 256;
402 num_vs_stack_entries = 256;
403 num_gs_stack_entries = 0;
404 num_es_stack_entries = 0;
405 break;
406 case CHIP_RV730:
407 case CHIP_RV740:
408 num_ps_gprs = 84;
409 num_vs_gprs = 36;
410 num_temp_gprs = 4;
411 num_gs_gprs = 0;
412 num_es_gprs = 0;
413 num_ps_threads = 188;
414 num_vs_threads = 60;
415 num_gs_threads = 0;
416 num_es_threads = 0;
417 num_ps_stack_entries = 128;
418 num_vs_stack_entries = 128;
419 num_gs_stack_entries = 0;
420 num_es_stack_entries = 0;
421 break;
422 case CHIP_RV710:
423 num_ps_gprs = 192;
424 num_vs_gprs = 56;
425 num_temp_gprs = 4;
426 num_gs_gprs = 0;
427 num_es_gprs = 0;
428 num_ps_threads = 144;
429 num_vs_threads = 48;
430 num_gs_threads = 0;
431 num_es_threads = 0;
432 num_ps_stack_entries = 128;
433 num_vs_stack_entries = 128;
434 num_gs_stack_entries = 0;
435 num_es_stack_entries = 0;
436 break;
437 }
438
439 if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV610) ||
440 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV620) ||
441 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS780) ||
442 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS880) ||
443 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV710))
444 sq_config = 0;
445 else
446 sq_config = R600_VC_ENABLE;
447
448 sq_config |= (R600_DX9_CONSTS |
449 R600_ALU_INST_PREFER_VECTOR |
450 R600_PS_PRIO(0) |
451 R600_VS_PRIO(1) |
452 R600_GS_PRIO(2) |
453 R600_ES_PRIO(3));
454
455 sq_gpr_resource_mgmt_1 = (R600_NUM_PS_GPRS(num_ps_gprs) |
456 R600_NUM_VS_GPRS(num_vs_gprs) |
457 R600_NUM_CLAUSE_TEMP_GPRS(num_temp_gprs));
458 sq_gpr_resource_mgmt_2 = (R600_NUM_GS_GPRS(num_gs_gprs) |
459 R600_NUM_ES_GPRS(num_es_gprs));
460 sq_thread_resource_mgmt = (R600_NUM_PS_THREADS(num_ps_threads) |
461 R600_NUM_VS_THREADS(num_vs_threads) |
462 R600_NUM_GS_THREADS(num_gs_threads) |
463 R600_NUM_ES_THREADS(num_es_threads));
464 sq_stack_resource_mgmt_1 = (R600_NUM_PS_STACK_ENTRIES(num_ps_stack_entries) |
465 R600_NUM_VS_STACK_ENTRIES(num_vs_stack_entries));
466 sq_stack_resource_mgmt_2 = (R600_NUM_GS_STACK_ENTRIES(num_gs_stack_entries) |
467 R600_NUM_ES_STACK_ENTRIES(num_es_stack_entries));
468
469 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_RV770) {
Alex Deucher5d93b1352009-09-09 16:09:36 -0400470 BEGIN_RING(r7xx_default_size + 10);
471 for (i = 0; i < r7xx_default_size; i++)
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000472 OUT_RING(r7xx_default_state[i]);
473 } else {
Alex Deucher5d93b1352009-09-09 16:09:36 -0400474 BEGIN_RING(r6xx_default_size + 10);
475 for (i = 0; i < r6xx_default_size; i++)
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000476 OUT_RING(r6xx_default_state[i]);
477 }
478 OUT_RING(CP_PACKET3(R600_IT_EVENT_WRITE, 0));
479 OUT_RING(R600_CACHE_FLUSH_AND_INV_EVENT);
480 /* SQ config */
481 OUT_RING(CP_PACKET3(R600_IT_SET_CONFIG_REG, 6));
482 OUT_RING((R600_SQ_CONFIG - R600_SET_CONFIG_REG_OFFSET) >> 2);
483 OUT_RING(sq_config);
484 OUT_RING(sq_gpr_resource_mgmt_1);
485 OUT_RING(sq_gpr_resource_mgmt_2);
486 OUT_RING(sq_thread_resource_mgmt);
487 OUT_RING(sq_stack_resource_mgmt_1);
488 OUT_RING(sq_stack_resource_mgmt_2);
489 ADVANCE_RING();
490}
491
Steven Fuerst747f49b2012-08-15 15:07:15 -0700492/* 23 bits of float fractional data */
493#define I2F_FRAC_BITS 23
494#define I2F_MASK ((1 << I2F_FRAC_BITS) - 1)
495
496/*
497 * Converts unsigned integer into 32-bit IEEE floating point representation.
498 * Will be exact from 0 to 2^24. Above that, we round towards zero
499 * as the fractional bits will not fit in a float. (It would be better to
500 * round towards even as the fpu does, but that is slower.)
501 */
Steven Fuerst9e9eb7c2012-08-15 15:07:16 -0700502__pure uint32_t int2float(uint32_t x)
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000503{
Steven Fuerst747f49b2012-08-15 15:07:15 -0700504 uint32_t msb, exponent, fraction;
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000505
Steven Fuerst747f49b2012-08-15 15:07:15 -0700506 /* Zero is special */
507 if (!x) return 0;
508
509 /* Get location of the most significant bit */
510 msb = __fls(x);
511
512 /*
513 * Use a rotate instead of a shift because that works both leftwards
514 * and rightwards due to the mod(32) behaviour. This means we don't
515 * need to check to see if we are above 2^24 or not.
516 */
517 fraction = ror32(x, (msb - I2F_FRAC_BITS) & 0x1f) & I2F_MASK;
518 exponent = (127 + msb) << I2F_FRAC_BITS;
519
520 return fraction + exponent;
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000521}
522
Andi Kleen74740c82011-10-13 16:08:43 -0700523static int r600_nomm_get_vb(struct drm_device *dev)
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000524{
525 drm_radeon_private_t *dev_priv = dev->dev_private;
526 dev_priv->blit_vb = radeon_freelist_get(dev);
527 if (!dev_priv->blit_vb) {
528 DRM_ERROR("Unable to allocate vertex buffer for blit\n");
529 return -EAGAIN;
530 }
531 return 0;
532}
533
Andi Kleen74740c82011-10-13 16:08:43 -0700534static void r600_nomm_put_vb(struct drm_device *dev)
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000535{
536 drm_radeon_private_t *dev_priv = dev->dev_private;
537
538 dev_priv->blit_vb->used = 0;
539 radeon_cp_discard_buffer(dev, dev_priv->blit_vb->file_priv->master, dev_priv->blit_vb);
540}
541
Andi Kleen74740c82011-10-13 16:08:43 -0700542static void *r600_nomm_get_vb_ptr(struct drm_device *dev)
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000543{
544 drm_radeon_private_t *dev_priv = dev->dev_private;
545 return (((char *)dev->agp_buffer_map->handle +
546 dev_priv->blit_vb->offset + dev_priv->blit_vb->used));
547}
548
549int
550r600_prepare_blit_copy(struct drm_device *dev, struct drm_file *file_priv)
551{
552 drm_radeon_private_t *dev_priv = dev->dev_private;
Alex Deucherc42750b2010-07-21 10:29:32 +1000553 int ret;
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000554 DRM_DEBUG("\n");
555
Alex Deucherc42750b2010-07-21 10:29:32 +1000556 ret = r600_nomm_get_vb(dev);
557 if (ret)
558 return ret;
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000559
560 dev_priv->blit_vb->file_priv = file_priv;
561
562 set_default_state(dev_priv);
563 set_shaders(dev);
564
565 return 0;
566}
567
568
569void
570r600_done_blit_copy(struct drm_device *dev)
571{
572 drm_radeon_private_t *dev_priv = dev->dev_private;
573 RING_LOCALS;
574 DRM_DEBUG("\n");
575
576 BEGIN_RING(5);
577 OUT_RING(CP_PACKET3(R600_IT_EVENT_WRITE, 0));
578 OUT_RING(R600_CACHE_FLUSH_AND_INV_EVENT);
579 /* wait for 3D idle clean */
580 OUT_RING(CP_PACKET3(R600_IT_SET_CONFIG_REG, 1));
581 OUT_RING((R600_WAIT_UNTIL - R600_SET_CONFIG_REG_OFFSET) >> 2);
582 OUT_RING(RADEON_WAIT_3D_IDLE | RADEON_WAIT_3D_IDLECLEAN);
583
584 ADVANCE_RING();
585 COMMIT_RING();
586
587 r600_nomm_put_vb(dev);
588}
589
590void
591r600_blit_copy(struct drm_device *dev,
592 uint64_t src_gpu_addr, uint64_t dst_gpu_addr,
593 int size_bytes)
594{
595 drm_radeon_private_t *dev_priv = dev->dev_private;
596 int max_bytes;
597 u64 vb_addr;
598 u32 *vb;
599
Dave Airlieceeb5022009-10-12 13:54:10 +1000600 vb = r600_nomm_get_vb_ptr(dev);
601
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000602 if ((size_bytes & 3) || (src_gpu_addr & 3) || (dst_gpu_addr & 3)) {
603 max_bytes = 8192;
604
605 while (size_bytes) {
606 int cur_size = size_bytes;
607 int src_x = src_gpu_addr & 255;
608 int dst_x = dst_gpu_addr & 255;
609 int h = 1;
610 src_gpu_addr = src_gpu_addr & ~255;
611 dst_gpu_addr = dst_gpu_addr & ~255;
612
613 if (!src_x && !dst_x) {
614 h = (cur_size / max_bytes);
615 if (h > 8192)
616 h = 8192;
617 if (h == 0)
618 h = 1;
619 else
620 cur_size = max_bytes;
621 } else {
622 if (cur_size > max_bytes)
623 cur_size = max_bytes;
624 if (cur_size > (max_bytes - dst_x))
625 cur_size = (max_bytes - dst_x);
626 if (cur_size > (max_bytes - src_x))
627 cur_size = (max_bytes - src_x);
628 }
629
630 if ((dev_priv->blit_vb->used + 48) > dev_priv->blit_vb->total) {
631
632 r600_nomm_put_vb(dev);
633 r600_nomm_get_vb(dev);
634 if (!dev_priv->blit_vb)
635 return;
636 set_shaders(dev);
Dave Airlieceeb5022009-10-12 13:54:10 +1000637 vb = r600_nomm_get_vb_ptr(dev);
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000638 }
639
Steven Fuerst7ff64fc2012-08-15 15:07:14 -0700640 vb[0] = int2float(dst_x);
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000641 vb[1] = 0;
Steven Fuerst7ff64fc2012-08-15 15:07:14 -0700642 vb[2] = int2float(src_x);
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000643 vb[3] = 0;
644
Steven Fuerst7ff64fc2012-08-15 15:07:14 -0700645 vb[4] = int2float(dst_x);
646 vb[5] = int2float(h);
647 vb[6] = int2float(src_x);
648 vb[7] = int2float(h);
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000649
Steven Fuerst7ff64fc2012-08-15 15:07:14 -0700650 vb[8] = int2float(dst_x + cur_size);
651 vb[9] = int2float(h);
652 vb[10] = int2float(src_x + cur_size);
653 vb[11] = int2float(h);
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000654
655 /* src */
656 set_tex_resource(dev_priv, FMT_8,
657 src_x + cur_size, h, src_x + cur_size,
658 src_gpu_addr);
659
660 cp_set_surface_sync(dev_priv,
661 R600_TC_ACTION_ENA, (src_x + cur_size * h), src_gpu_addr);
662
663 /* dst */
664 set_render_target(dev_priv, COLOR_8,
665 dst_x + cur_size, h,
666 dst_gpu_addr);
667
668 /* scissors */
669 set_scissors(dev_priv, dst_x, 0, dst_x + cur_size, h);
670
671 /* Vertex buffer setup */
672 vb_addr = dev_priv->gart_buffers_offset +
673 dev_priv->blit_vb->offset +
674 dev_priv->blit_vb->used;
675 set_vtx_resource(dev_priv, vb_addr);
676
677 /* draw */
678 draw_auto(dev_priv);
679
680 cp_set_surface_sync(dev_priv,
681 R600_CB_ACTION_ENA | R600_CB0_DEST_BASE_ENA,
682 cur_size * h, dst_gpu_addr);
683
684 vb += 12;
685 dev_priv->blit_vb->used += 12 * 4;
686
687 src_gpu_addr += cur_size * h;
688 dst_gpu_addr += cur_size * h;
689 size_bytes -= cur_size * h;
690 }
691 } else {
692 max_bytes = 8192 * 4;
693
694 while (size_bytes) {
695 int cur_size = size_bytes;
696 int src_x = (src_gpu_addr & 255);
697 int dst_x = (dst_gpu_addr & 255);
698 int h = 1;
699 src_gpu_addr = src_gpu_addr & ~255;
700 dst_gpu_addr = dst_gpu_addr & ~255;
701
702 if (!src_x && !dst_x) {
703 h = (cur_size / max_bytes);
704 if (h > 8192)
705 h = 8192;
706 if (h == 0)
707 h = 1;
708 else
709 cur_size = max_bytes;
710 } else {
711 if (cur_size > max_bytes)
712 cur_size = max_bytes;
713 if (cur_size > (max_bytes - dst_x))
714 cur_size = (max_bytes - dst_x);
715 if (cur_size > (max_bytes - src_x))
716 cur_size = (max_bytes - src_x);
717 }
718
719 if ((dev_priv->blit_vb->used + 48) > dev_priv->blit_vb->total) {
720 r600_nomm_put_vb(dev);
721 r600_nomm_get_vb(dev);
722 if (!dev_priv->blit_vb)
723 return;
724
725 set_shaders(dev);
Dave Airlieceeb5022009-10-12 13:54:10 +1000726 vb = r600_nomm_get_vb_ptr(dev);
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000727 }
728
Steven Fuerst7ff64fc2012-08-15 15:07:14 -0700729 vb[0] = int2float(dst_x / 4);
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000730 vb[1] = 0;
Steven Fuerst7ff64fc2012-08-15 15:07:14 -0700731 vb[2] = int2float(src_x / 4);
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000732 vb[3] = 0;
733
Steven Fuerst7ff64fc2012-08-15 15:07:14 -0700734 vb[4] = int2float(dst_x / 4);
735 vb[5] = int2float(h);
736 vb[6] = int2float(src_x / 4);
737 vb[7] = int2float(h);
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000738
Steven Fuerst7ff64fc2012-08-15 15:07:14 -0700739 vb[8] = int2float((dst_x + cur_size) / 4);
740 vb[9] = int2float(h);
741 vb[10] = int2float((src_x + cur_size) / 4);
742 vb[11] = int2float(h);
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000743
744 /* src */
745 set_tex_resource(dev_priv, FMT_8_8_8_8,
746 (src_x + cur_size) / 4,
747 h, (src_x + cur_size) / 4,
748 src_gpu_addr);
749
750 cp_set_surface_sync(dev_priv,
751 R600_TC_ACTION_ENA, (src_x + cur_size * h), src_gpu_addr);
752
753 /* dst */
754 set_render_target(dev_priv, COLOR_8_8_8_8,
Andre Maasikas5b31aee2009-09-21 08:59:41 -0400755 (dst_x + cur_size) / 4, h,
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000756 dst_gpu_addr);
757
758 /* scissors */
759 set_scissors(dev_priv, (dst_x / 4), 0, (dst_x + cur_size / 4), h);
760
761 /* Vertex buffer setup */
762 vb_addr = dev_priv->gart_buffers_offset +
763 dev_priv->blit_vb->offset +
764 dev_priv->blit_vb->used;
765 set_vtx_resource(dev_priv, vb_addr);
766
767 /* draw */
768 draw_auto(dev_priv);
769
770 cp_set_surface_sync(dev_priv,
771 R600_CB_ACTION_ENA | R600_CB0_DEST_BASE_ENA,
772 cur_size * h, dst_gpu_addr);
773
774 vb += 12;
775 dev_priv->blit_vb->used += 12 * 4;
776
777 src_gpu_addr += cur_size * h;
778 dst_gpu_addr += cur_size * h;
779 size_bytes -= cur_size * h;
780 }
781 }
782}
783
784void
785r600_blit_swap(struct drm_device *dev,
786 uint64_t src_gpu_addr, uint64_t dst_gpu_addr,
787 int sx, int sy, int dx, int dy,
788 int w, int h, int src_pitch, int dst_pitch, int cpp)
789{
790 drm_radeon_private_t *dev_priv = dev->dev_private;
791 int cb_format, tex_format;
Robert Nolandc54b18202009-10-20 08:11:36 -0500792 int sx2, sy2, dx2, dy2;
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000793 u64 vb_addr;
794 u32 *vb;
795
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000796 if ((dev_priv->blit_vb->used + 48) > dev_priv->blit_vb->total) {
797
798 r600_nomm_put_vb(dev);
799 r600_nomm_get_vb(dev);
800 if (!dev_priv->blit_vb)
801 return;
802
803 set_shaders(dev);
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000804 }
Robert Noland33fdb152009-10-20 13:07:38 -0500805 vb = r600_nomm_get_vb_ptr(dev);
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000806
Robert Nolandc54b18202009-10-20 08:11:36 -0500807 sx2 = sx + w;
808 sy2 = sy + h;
809 dx2 = dx + w;
810 dy2 = dy + h;
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000811
Steven Fuerst7ff64fc2012-08-15 15:07:14 -0700812 vb[0] = int2float(dx);
813 vb[1] = int2float(dy);
814 vb[2] = int2float(sx);
815 vb[3] = int2float(sy);
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000816
Steven Fuerst7ff64fc2012-08-15 15:07:14 -0700817 vb[4] = int2float(dx);
818 vb[5] = int2float(dy2);
819 vb[6] = int2float(sx);
820 vb[7] = int2float(sy2);
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000821
Steven Fuerst7ff64fc2012-08-15 15:07:14 -0700822 vb[8] = int2float(dx2);
823 vb[9] = int2float(dy2);
824 vb[10] = int2float(sx2);
825 vb[11] = int2float(sy2);
Robert Nolandc54b18202009-10-20 08:11:36 -0500826
827 switch(cpp) {
828 case 4:
829 cb_format = COLOR_8_8_8_8;
830 tex_format = FMT_8_8_8_8;
831 break;
832 case 2:
833 cb_format = COLOR_5_6_5;
834 tex_format = FMT_5_6_5;
835 break;
836 default:
837 cb_format = COLOR_8;
838 tex_format = FMT_8;
839 break;
840 }
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000841
842 /* src */
843 set_tex_resource(dev_priv, tex_format,
844 src_pitch / cpp,
Robert Nolandc54b18202009-10-20 08:11:36 -0500845 sy2, src_pitch / cpp,
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000846 src_gpu_addr);
847
848 cp_set_surface_sync(dev_priv,
Robert Nolandc54b18202009-10-20 08:11:36 -0500849 R600_TC_ACTION_ENA, src_pitch * sy2, src_gpu_addr);
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000850
851 /* dst */
852 set_render_target(dev_priv, cb_format,
Robert Nolandc54b18202009-10-20 08:11:36 -0500853 dst_pitch / cpp, dy2,
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000854 dst_gpu_addr);
855
856 /* scissors */
Robert Nolandc54b18202009-10-20 08:11:36 -0500857 set_scissors(dev_priv, dx, dy, dx2, dy2);
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000858
859 /* Vertex buffer setup */
860 vb_addr = dev_priv->gart_buffers_offset +
861 dev_priv->blit_vb->offset +
862 dev_priv->blit_vb->used;
863 set_vtx_resource(dev_priv, vb_addr);
864
865 /* draw */
866 draw_auto(dev_priv);
867
868 cp_set_surface_sync(dev_priv,
869 R600_CB_ACTION_ENA | R600_CB0_DEST_BASE_ENA,
Robert Nolandc54b18202009-10-20 08:11:36 -0500870 dst_pitch * dy2, dst_gpu_addr);
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000871
872 dev_priv->blit_vb->used += 12 * 4;
873}