blob: 40867290863c238032f3865629c9bca816276597 [file] [log] [blame]
Alex Deucherd7ccd8f2010-09-09 11:33:36 -04001/*
2 * Copyright 2010 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
27#include "drmP.h"
28#include "drm.h"
29#include "radeon_drm.h"
30#include "radeon.h"
31
32#include "evergreend.h"
33#include "evergreen_blit_shaders.h"
34
35#define DI_PT_RECTLIST 0x11
36#define DI_INDEX_SIZE_16_BIT 0x0
37#define DI_SRC_SEL_AUTO_INDEX 0x2
38
39#define FMT_8 0x1
40#define FMT_5_6_5 0x8
41#define FMT_8_8_8_8 0x1a
42#define COLOR_8 0x1
43#define COLOR_5_6_5 0x8
44#define COLOR_8_8_8_8 0x1a
45
46/* emits 17 */
47static void
48set_render_target(struct radeon_device *rdev, int format,
49 int w, int h, u64 gpu_addr)
50{
51 u32 cb_color_info;
52 int pitch, slice;
53
54 h = ALIGN(h, 8);
55 if (h < 8)
56 h = 8;
57
Alex Deucher27dcfc12011-02-10 14:51:34 -050058 cb_color_info = ((format << 2) | (1 << 24) | (1 << 8));
Alex Deucherd7ccd8f2010-09-09 11:33:36 -040059 pitch = (w / 8) - 1;
60 slice = ((w * h) / 64) - 1;
61
62 radeon_ring_write(rdev, PACKET3(PACKET3_SET_CONTEXT_REG, 15));
63 radeon_ring_write(rdev, (CB_COLOR0_BASE - PACKET3_SET_CONTEXT_REG_START) >> 2);
64 radeon_ring_write(rdev, gpu_addr >> 8);
65 radeon_ring_write(rdev, pitch);
66 radeon_ring_write(rdev, slice);
67 radeon_ring_write(rdev, 0);
68 radeon_ring_write(rdev, cb_color_info);
69 radeon_ring_write(rdev, (1 << 4));
70 radeon_ring_write(rdev, (w - 1) | ((h - 1) << 16));
71 radeon_ring_write(rdev, 0);
72 radeon_ring_write(rdev, 0);
73 radeon_ring_write(rdev, 0);
74 radeon_ring_write(rdev, 0);
75 radeon_ring_write(rdev, 0);
76 radeon_ring_write(rdev, 0);
77 radeon_ring_write(rdev, 0);
78 radeon_ring_write(rdev, 0);
79}
80
81/* emits 5dw */
82static void
83cp_set_surface_sync(struct radeon_device *rdev,
84 u32 sync_type, u32 size,
85 u64 mc_addr)
86{
87 u32 cp_coher_size;
88
89 if (size == 0xffffffff)
90 cp_coher_size = 0xffffffff;
91 else
92 cp_coher_size = ((size + 255) >> 8);
93
94 radeon_ring_write(rdev, PACKET3(PACKET3_SURFACE_SYNC, 3));
95 radeon_ring_write(rdev, sync_type);
96 radeon_ring_write(rdev, cp_coher_size);
97 radeon_ring_write(rdev, mc_addr >> 8);
98 radeon_ring_write(rdev, 10); /* poll interval */
99}
100
101/* emits 11dw + 1 surface sync = 16dw */
102static void
103set_shaders(struct radeon_device *rdev)
104{
105 u64 gpu_addr;
106
107 /* VS */
108 gpu_addr = rdev->r600_blit.shader_gpu_addr + rdev->r600_blit.vs_offset;
109 radeon_ring_write(rdev, PACKET3(PACKET3_SET_CONTEXT_REG, 3));
110 radeon_ring_write(rdev, (SQ_PGM_START_VS - PACKET3_SET_CONTEXT_REG_START) >> 2);
111 radeon_ring_write(rdev, gpu_addr >> 8);
112 radeon_ring_write(rdev, 2);
113 radeon_ring_write(rdev, 0);
114
115 /* PS */
116 gpu_addr = rdev->r600_blit.shader_gpu_addr + rdev->r600_blit.ps_offset;
117 radeon_ring_write(rdev, PACKET3(PACKET3_SET_CONTEXT_REG, 4));
118 radeon_ring_write(rdev, (SQ_PGM_START_PS - PACKET3_SET_CONTEXT_REG_START) >> 2);
119 radeon_ring_write(rdev, gpu_addr >> 8);
120 radeon_ring_write(rdev, 1);
121 radeon_ring_write(rdev, 0);
122 radeon_ring_write(rdev, 2);
123
124 gpu_addr = rdev->r600_blit.shader_gpu_addr + rdev->r600_blit.vs_offset;
125 cp_set_surface_sync(rdev, PACKET3_SH_ACTION_ENA, 512, gpu_addr);
126}
127
128/* emits 10 + 1 sync (5) = 15 */
129static void
130set_vtx_resource(struct radeon_device *rdev, u64 gpu_addr)
131{
132 u32 sq_vtx_constant_word2, sq_vtx_constant_word3;
133
134 /* high addr, stride */
135 sq_vtx_constant_word2 = ((upper_32_bits(gpu_addr) & 0xff) | (16 << 8));
Alex Deucher0f234f5f2011-02-13 19:06:33 -0500136#ifdef __BIG_ENDIAN
137 sq_vtx_constant_word2 |= (2 << 30);
138#endif
Alex Deucherd7ccd8f2010-09-09 11:33:36 -0400139 /* xyzw swizzles */
140 sq_vtx_constant_word3 = (0 << 3) | (1 << 6) | (2 << 9) | (3 << 12);
141
142 radeon_ring_write(rdev, PACKET3(PACKET3_SET_RESOURCE, 8));
143 radeon_ring_write(rdev, 0x580);
144 radeon_ring_write(rdev, gpu_addr & 0xffffffff);
145 radeon_ring_write(rdev, 48 - 1); /* size */
146 radeon_ring_write(rdev, sq_vtx_constant_word2);
147 radeon_ring_write(rdev, sq_vtx_constant_word3);
148 radeon_ring_write(rdev, 0);
149 radeon_ring_write(rdev, 0);
150 radeon_ring_write(rdev, 0);
151 radeon_ring_write(rdev, SQ_TEX_VTX_VALID_BUFFER << 30);
152
Alex Deuchere719ebd2010-11-22 17:56:33 -0500153 if ((rdev->family == CHIP_CEDAR) ||
Alex Deucherff5b8562011-01-06 21:19:28 -0500154 (rdev->family == CHIP_PALM) ||
155 (rdev->family == CHIP_CAICOS))
Alex Deucherd7ccd8f2010-09-09 11:33:36 -0400156 cp_set_surface_sync(rdev,
157 PACKET3_TC_ACTION_ENA, 48, gpu_addr);
158 else
159 cp_set_surface_sync(rdev,
160 PACKET3_VC_ACTION_ENA, 48, gpu_addr);
161
162}
163
164/* emits 10 */
165static void
166set_tex_resource(struct radeon_device *rdev,
167 int format, int w, int h, int pitch,
168 u64 gpu_addr)
169{
170 u32 sq_tex_resource_word0, sq_tex_resource_word1;
171 u32 sq_tex_resource_word4, sq_tex_resource_word7;
172
173 if (h < 1)
174 h = 1;
175
176 sq_tex_resource_word0 = (1 << 0); /* 2D */
177 sq_tex_resource_word0 |= ((((pitch >> 3) - 1) << 6) |
178 ((w - 1) << 18));
Alex Deucher27dcfc12011-02-10 14:51:34 -0500179 sq_tex_resource_word1 = ((h - 1) << 0) | (1 << 28);
Alex Deucherd7ccd8f2010-09-09 11:33:36 -0400180 /* xyzw swizzles */
181 sq_tex_resource_word4 = (0 << 16) | (1 << 19) | (2 << 22) | (3 << 25);
182
183 sq_tex_resource_word7 = format | (SQ_TEX_VTX_VALID_TEXTURE << 30);
184
185 radeon_ring_write(rdev, PACKET3(PACKET3_SET_RESOURCE, 8));
186 radeon_ring_write(rdev, 0);
187 radeon_ring_write(rdev, sq_tex_resource_word0);
188 radeon_ring_write(rdev, sq_tex_resource_word1);
189 radeon_ring_write(rdev, gpu_addr >> 8);
190 radeon_ring_write(rdev, gpu_addr >> 8);
191 radeon_ring_write(rdev, sq_tex_resource_word4);
192 radeon_ring_write(rdev, 0);
193 radeon_ring_write(rdev, 0);
194 radeon_ring_write(rdev, sq_tex_resource_word7);
195}
196
197/* emits 12 */
198static void
199set_scissors(struct radeon_device *rdev, int x1, int y1,
200 int x2, int y2)
201{
Alex Deucherac10f812011-05-25 01:00:45 -0400202 /* workaround some hw bugs */
203 if (x2 == 0)
204 x1 = 1;
205 if (y2 == 0)
206 y1 = 1;
207 if (rdev->family == CHIP_CAYMAN) {
208 if ((x2 == 1) && (y2 == 1))
209 x2 = 2;
210 }
211
Alex Deucherd7ccd8f2010-09-09 11:33:36 -0400212 radeon_ring_write(rdev, PACKET3(PACKET3_SET_CONTEXT_REG, 2));
213 radeon_ring_write(rdev, (PA_SC_SCREEN_SCISSOR_TL - PACKET3_SET_CONTEXT_REG_START) >> 2);
214 radeon_ring_write(rdev, (x1 << 0) | (y1 << 16));
215 radeon_ring_write(rdev, (x2 << 0) | (y2 << 16));
216
217 radeon_ring_write(rdev, PACKET3(PACKET3_SET_CONTEXT_REG, 2));
218 radeon_ring_write(rdev, (PA_SC_GENERIC_SCISSOR_TL - PACKET3_SET_CONTEXT_REG_START) >> 2);
219 radeon_ring_write(rdev, (x1 << 0) | (y1 << 16) | (1 << 31));
220 radeon_ring_write(rdev, (x2 << 0) | (y2 << 16));
221
222 radeon_ring_write(rdev, PACKET3(PACKET3_SET_CONTEXT_REG, 2));
223 radeon_ring_write(rdev, (PA_SC_WINDOW_SCISSOR_TL - PACKET3_SET_CONTEXT_REG_START) >> 2);
224 radeon_ring_write(rdev, (x1 << 0) | (y1 << 16) | (1 << 31));
225 radeon_ring_write(rdev, (x2 << 0) | (y2 << 16));
226}
227
228/* emits 10 */
229static void
230draw_auto(struct radeon_device *rdev)
231{
232 radeon_ring_write(rdev, PACKET3(PACKET3_SET_CONFIG_REG, 1));
233 radeon_ring_write(rdev, (VGT_PRIMITIVE_TYPE - PACKET3_SET_CONFIG_REG_START) >> 2);
234 radeon_ring_write(rdev, DI_PT_RECTLIST);
235
236 radeon_ring_write(rdev, PACKET3(PACKET3_INDEX_TYPE, 0));
Alex Deucher0f234f5f2011-02-13 19:06:33 -0500237 radeon_ring_write(rdev,
238#ifdef __BIG_ENDIAN
239 (2 << 2) |
240#endif
241 DI_INDEX_SIZE_16_BIT);
Alex Deucherd7ccd8f2010-09-09 11:33:36 -0400242
243 radeon_ring_write(rdev, PACKET3(PACKET3_NUM_INSTANCES, 0));
244 radeon_ring_write(rdev, 1);
245
246 radeon_ring_write(rdev, PACKET3(PACKET3_DRAW_INDEX_AUTO, 1));
247 radeon_ring_write(rdev, 3);
248 radeon_ring_write(rdev, DI_SRC_SEL_AUTO_INDEX);
249
250}
251
Alex Deucher12920592011-02-02 12:37:40 -0500252/* emits 36 */
Alex Deucherd7ccd8f2010-09-09 11:33:36 -0400253static void
254set_default_state(struct radeon_device *rdev)
255{
256 u32 sq_config, sq_gpr_resource_mgmt_1, sq_gpr_resource_mgmt_2, sq_gpr_resource_mgmt_3;
257 u32 sq_thread_resource_mgmt, sq_thread_resource_mgmt_2;
258 u32 sq_stack_resource_mgmt_1, sq_stack_resource_mgmt_2, sq_stack_resource_mgmt_3;
259 int num_ps_gprs, num_vs_gprs, num_temp_gprs;
260 int num_gs_gprs, num_es_gprs, num_hs_gprs, num_ls_gprs;
261 int num_ps_threads, num_vs_threads, num_gs_threads, num_es_threads;
262 int num_hs_threads, num_ls_threads;
263 int num_ps_stack_entries, num_vs_stack_entries, num_gs_stack_entries, num_es_stack_entries;
264 int num_hs_stack_entries, num_ls_stack_entries;
Alex Deucher1e644d62011-01-27 17:01:52 -0500265 u64 gpu_addr;
266 int dwords;
Alex Deucherd7ccd8f2010-09-09 11:33:36 -0400267
268 switch (rdev->family) {
269 case CHIP_CEDAR:
270 default:
271 num_ps_gprs = 93;
272 num_vs_gprs = 46;
273 num_temp_gprs = 4;
274 num_gs_gprs = 31;
275 num_es_gprs = 31;
276 num_hs_gprs = 23;
277 num_ls_gprs = 23;
278 num_ps_threads = 96;
279 num_vs_threads = 16;
280 num_gs_threads = 16;
281 num_es_threads = 16;
282 num_hs_threads = 16;
283 num_ls_threads = 16;
284 num_ps_stack_entries = 42;
285 num_vs_stack_entries = 42;
286 num_gs_stack_entries = 42;
287 num_es_stack_entries = 42;
288 num_hs_stack_entries = 42;
289 num_ls_stack_entries = 42;
290 break;
291 case CHIP_REDWOOD:
292 num_ps_gprs = 93;
293 num_vs_gprs = 46;
294 num_temp_gprs = 4;
295 num_gs_gprs = 31;
296 num_es_gprs = 31;
297 num_hs_gprs = 23;
298 num_ls_gprs = 23;
299 num_ps_threads = 128;
300 num_vs_threads = 20;
301 num_gs_threads = 20;
302 num_es_threads = 20;
303 num_hs_threads = 20;
304 num_ls_threads = 20;
305 num_ps_stack_entries = 42;
306 num_vs_stack_entries = 42;
307 num_gs_stack_entries = 42;
308 num_es_stack_entries = 42;
309 num_hs_stack_entries = 42;
310 num_ls_stack_entries = 42;
311 break;
312 case CHIP_JUNIPER:
313 num_ps_gprs = 93;
314 num_vs_gprs = 46;
315 num_temp_gprs = 4;
316 num_gs_gprs = 31;
317 num_es_gprs = 31;
318 num_hs_gprs = 23;
319 num_ls_gprs = 23;
320 num_ps_threads = 128;
321 num_vs_threads = 20;
322 num_gs_threads = 20;
323 num_es_threads = 20;
324 num_hs_threads = 20;
325 num_ls_threads = 20;
326 num_ps_stack_entries = 85;
327 num_vs_stack_entries = 85;
328 num_gs_stack_entries = 85;
329 num_es_stack_entries = 85;
330 num_hs_stack_entries = 85;
331 num_ls_stack_entries = 85;
332 break;
333 case CHIP_CYPRESS:
334 case CHIP_HEMLOCK:
335 num_ps_gprs = 93;
336 num_vs_gprs = 46;
337 num_temp_gprs = 4;
338 num_gs_gprs = 31;
339 num_es_gprs = 31;
340 num_hs_gprs = 23;
341 num_ls_gprs = 23;
342 num_ps_threads = 128;
343 num_vs_threads = 20;
344 num_gs_threads = 20;
345 num_es_threads = 20;
346 num_hs_threads = 20;
347 num_ls_threads = 20;
348 num_ps_stack_entries = 85;
349 num_vs_stack_entries = 85;
350 num_gs_stack_entries = 85;
351 num_es_stack_entries = 85;
352 num_hs_stack_entries = 85;
353 num_ls_stack_entries = 85;
354 break;
Alex Deuchere719ebd2010-11-22 17:56:33 -0500355 case CHIP_PALM:
356 num_ps_gprs = 93;
357 num_vs_gprs = 46;
358 num_temp_gprs = 4;
359 num_gs_gprs = 31;
360 num_es_gprs = 31;
361 num_hs_gprs = 23;
362 num_ls_gprs = 23;
363 num_ps_threads = 96;
364 num_vs_threads = 16;
365 num_gs_threads = 16;
366 num_es_threads = 16;
367 num_hs_threads = 16;
368 num_ls_threads = 16;
369 num_ps_stack_entries = 42;
370 num_vs_stack_entries = 42;
371 num_gs_stack_entries = 42;
372 num_es_stack_entries = 42;
373 num_hs_stack_entries = 42;
374 num_ls_stack_entries = 42;
375 break;
Alex Deucherff5b8562011-01-06 21:19:28 -0500376 case CHIP_BARTS:
377 num_ps_gprs = 93;
378 num_vs_gprs = 46;
379 num_temp_gprs = 4;
380 num_gs_gprs = 31;
381 num_es_gprs = 31;
382 num_hs_gprs = 23;
383 num_ls_gprs = 23;
384 num_ps_threads = 128;
385 num_vs_threads = 20;
386 num_gs_threads = 20;
387 num_es_threads = 20;
388 num_hs_threads = 20;
389 num_ls_threads = 20;
390 num_ps_stack_entries = 85;
391 num_vs_stack_entries = 85;
392 num_gs_stack_entries = 85;
393 num_es_stack_entries = 85;
394 num_hs_stack_entries = 85;
395 num_ls_stack_entries = 85;
396 break;
397 case CHIP_TURKS:
398 num_ps_gprs = 93;
399 num_vs_gprs = 46;
400 num_temp_gprs = 4;
401 num_gs_gprs = 31;
402 num_es_gprs = 31;
403 num_hs_gprs = 23;
404 num_ls_gprs = 23;
405 num_ps_threads = 128;
406 num_vs_threads = 20;
407 num_gs_threads = 20;
408 num_es_threads = 20;
409 num_hs_threads = 20;
410 num_ls_threads = 20;
411 num_ps_stack_entries = 42;
412 num_vs_stack_entries = 42;
413 num_gs_stack_entries = 42;
414 num_es_stack_entries = 42;
415 num_hs_stack_entries = 42;
416 num_ls_stack_entries = 42;
417 break;
418 case CHIP_CAICOS:
419 num_ps_gprs = 93;
420 num_vs_gprs = 46;
421 num_temp_gprs = 4;
422 num_gs_gprs = 31;
423 num_es_gprs = 31;
424 num_hs_gprs = 23;
425 num_ls_gprs = 23;
426 num_ps_threads = 128;
427 num_vs_threads = 10;
428 num_gs_threads = 10;
429 num_es_threads = 10;
430 num_hs_threads = 10;
431 num_ls_threads = 10;
432 num_ps_stack_entries = 42;
433 num_vs_stack_entries = 42;
434 num_gs_stack_entries = 42;
435 num_es_stack_entries = 42;
436 num_hs_stack_entries = 42;
437 num_ls_stack_entries = 42;
438 break;
Alex Deucherd7ccd8f2010-09-09 11:33:36 -0400439 }
440
Alex Deuchere719ebd2010-11-22 17:56:33 -0500441 if ((rdev->family == CHIP_CEDAR) ||
Alex Deucherff5b8562011-01-06 21:19:28 -0500442 (rdev->family == CHIP_PALM) ||
443 (rdev->family == CHIP_CAICOS))
Alex Deucherd7ccd8f2010-09-09 11:33:36 -0400444 sq_config = 0;
445 else
446 sq_config = VC_ENABLE;
447
448 sq_config |= (EXPORT_SRC_C |
449 CS_PRIO(0) |
450 LS_PRIO(0) |
451 HS_PRIO(0) |
452 PS_PRIO(0) |
453 VS_PRIO(1) |
454 GS_PRIO(2) |
455 ES_PRIO(3));
456
457 sq_gpr_resource_mgmt_1 = (NUM_PS_GPRS(num_ps_gprs) |
458 NUM_VS_GPRS(num_vs_gprs) |
459 NUM_CLAUSE_TEMP_GPRS(num_temp_gprs));
460 sq_gpr_resource_mgmt_2 = (NUM_GS_GPRS(num_gs_gprs) |
461 NUM_ES_GPRS(num_es_gprs));
462 sq_gpr_resource_mgmt_3 = (NUM_HS_GPRS(num_hs_gprs) |
463 NUM_LS_GPRS(num_ls_gprs));
464 sq_thread_resource_mgmt = (NUM_PS_THREADS(num_ps_threads) |
465 NUM_VS_THREADS(num_vs_threads) |
466 NUM_GS_THREADS(num_gs_threads) |
467 NUM_ES_THREADS(num_es_threads));
468 sq_thread_resource_mgmt_2 = (NUM_HS_THREADS(num_hs_threads) |
469 NUM_LS_THREADS(num_ls_threads));
470 sq_stack_resource_mgmt_1 = (NUM_PS_STACK_ENTRIES(num_ps_stack_entries) |
471 NUM_VS_STACK_ENTRIES(num_vs_stack_entries));
472 sq_stack_resource_mgmt_2 = (NUM_GS_STACK_ENTRIES(num_gs_stack_entries) |
473 NUM_ES_STACK_ENTRIES(num_es_stack_entries));
474 sq_stack_resource_mgmt_3 = (NUM_HS_STACK_ENTRIES(num_hs_stack_entries) |
475 NUM_LS_STACK_ENTRIES(num_ls_stack_entries));
476
Alex Deucher2281a372010-10-21 13:31:38 -0400477 /* set clear context state */
478 radeon_ring_write(rdev, PACKET3(PACKET3_CLEAR_STATE, 0));
479 radeon_ring_write(rdev, 0);
Alex Deucherd7ccd8f2010-09-09 11:33:36 -0400480
481 /* disable dyn gprs */
482 radeon_ring_write(rdev, PACKET3(PACKET3_SET_CONFIG_REG, 1));
483 radeon_ring_write(rdev, (SQ_DYN_GPR_CNTL_PS_FLUSH_REQ - PACKET3_SET_CONFIG_REG_START) >> 2);
484 radeon_ring_write(rdev, 0);
485
486 /* SQ config */
487 radeon_ring_write(rdev, PACKET3(PACKET3_SET_CONFIG_REG, 11));
488 radeon_ring_write(rdev, (SQ_CONFIG - PACKET3_SET_CONFIG_REG_START) >> 2);
489 radeon_ring_write(rdev, sq_config);
490 radeon_ring_write(rdev, sq_gpr_resource_mgmt_1);
491 radeon_ring_write(rdev, sq_gpr_resource_mgmt_2);
492 radeon_ring_write(rdev, sq_gpr_resource_mgmt_3);
493 radeon_ring_write(rdev, 0);
494 radeon_ring_write(rdev, 0);
495 radeon_ring_write(rdev, sq_thread_resource_mgmt);
496 radeon_ring_write(rdev, sq_thread_resource_mgmt_2);
497 radeon_ring_write(rdev, sq_stack_resource_mgmt_1);
498 radeon_ring_write(rdev, sq_stack_resource_mgmt_2);
499 radeon_ring_write(rdev, sq_stack_resource_mgmt_3);
Alex Deucher2281a372010-10-21 13:31:38 -0400500
501 /* CONTEXT_CONTROL */
502 radeon_ring_write(rdev, 0xc0012800);
503 radeon_ring_write(rdev, 0x80000000);
504 radeon_ring_write(rdev, 0x80000000);
505
506 /* SQ_VTX_BASE_VTX_LOC */
507 radeon_ring_write(rdev, 0xc0026f00);
508 radeon_ring_write(rdev, 0x00000000);
509 radeon_ring_write(rdev, 0x00000000);
510 radeon_ring_write(rdev, 0x00000000);
511
512 /* SET_SAMPLER */
513 radeon_ring_write(rdev, 0xc0036e00);
514 radeon_ring_write(rdev, 0x00000000);
515 radeon_ring_write(rdev, 0x00000012);
516 radeon_ring_write(rdev, 0x00000000);
517 radeon_ring_write(rdev, 0x00000000);
518
Alex Deucher12920592011-02-02 12:37:40 -0500519 /* set to DX10/11 mode */
520 radeon_ring_write(rdev, PACKET3(PACKET3_MODE_CONTROL, 0));
521 radeon_ring_write(rdev, 1);
522
Alex Deucher1e644d62011-01-27 17:01:52 -0500523 /* emit an IB pointing at default state */
524 dwords = ALIGN(rdev->r600_blit.state_len, 0x10);
525 gpu_addr = rdev->r600_blit.shader_gpu_addr + rdev->r600_blit.state_offset;
526 radeon_ring_write(rdev, PACKET3(PACKET3_INDIRECT_BUFFER, 2));
527 radeon_ring_write(rdev, gpu_addr & 0xFFFFFFFC);
528 radeon_ring_write(rdev, upper_32_bits(gpu_addr) & 0xFF);
529 radeon_ring_write(rdev, dwords);
530
Alex Deucherd7ccd8f2010-09-09 11:33:36 -0400531}
532
533static inline uint32_t i2f(uint32_t input)
534{
535 u32 result, i, exponent, fraction;
536
537 if ((input & 0x3fff) == 0)
538 result = 0; /* 0 is a special case */
539 else {
540 exponent = 140; /* exponent biased by 127; */
541 fraction = (input & 0x3fff) << 10; /* cheat and only
542 handle numbers below 2^^15 */
543 for (i = 0; i < 14; i++) {
544 if (fraction & 0x800000)
545 break;
546 else {
547 fraction = fraction << 1; /* keep
548 shifting left until top bit = 1 */
549 exponent = exponent - 1;
550 }
551 }
552 result = exponent << 23 | (fraction & 0x7fffff); /* mask
553 off top bit; assumed 1 */
554 }
555 return result;
556}
557
558int evergreen_blit_init(struct radeon_device *rdev)
559{
560 u32 obj_size;
Alex Deucher0f234f5f2011-02-13 19:06:33 -0500561 int i, r, dwords;
Alex Deucherd7ccd8f2010-09-09 11:33:36 -0400562 void *ptr;
Alex Deucher1e644d62011-01-27 17:01:52 -0500563 u32 packet2s[16];
564 int num_packet2s = 0;
Alex Deucherd7ccd8f2010-09-09 11:33:36 -0400565
566 /* pin copy shader into vram if already initialized */
567 if (rdev->r600_blit.shader_obj)
568 goto done;
569
570 mutex_init(&rdev->r600_blit.mutex);
571 rdev->r600_blit.state_offset = 0;
Alex Deucher1e644d62011-01-27 17:01:52 -0500572
573 rdev->r600_blit.state_len = evergreen_default_size;
574
575 dwords = rdev->r600_blit.state_len;
576 while (dwords & 0xf) {
Alex Deucher0f234f5f2011-02-13 19:06:33 -0500577 packet2s[num_packet2s++] = cpu_to_le32(PACKET2(0));
Alex Deucher1e644d62011-01-27 17:01:52 -0500578 dwords++;
579 }
580
581 obj_size = dwords * 4;
582 obj_size = ALIGN(obj_size, 256);
Alex Deucherd7ccd8f2010-09-09 11:33:36 -0400583
584 rdev->r600_blit.vs_offset = obj_size;
585 obj_size += evergreen_vs_size * 4;
586 obj_size = ALIGN(obj_size, 256);
587
588 rdev->r600_blit.ps_offset = obj_size;
589 obj_size += evergreen_ps_size * 4;
590 obj_size = ALIGN(obj_size, 256);
591
Daniel Vetter441921d2011-02-18 17:59:16 +0100592 r = radeon_bo_create(rdev, obj_size, PAGE_SIZE, true, RADEON_GEM_DOMAIN_VRAM,
Alex Deucherd7ccd8f2010-09-09 11:33:36 -0400593 &rdev->r600_blit.shader_obj);
594 if (r) {
595 DRM_ERROR("evergreen failed to allocate shader\n");
596 return r;
597 }
598
599 DRM_DEBUG("evergreen blit allocated bo %08x vs %08x ps %08x\n",
600 obj_size,
601 rdev->r600_blit.vs_offset, rdev->r600_blit.ps_offset);
602
603 r = radeon_bo_reserve(rdev->r600_blit.shader_obj, false);
604 if (unlikely(r != 0))
605 return r;
606 r = radeon_bo_kmap(rdev->r600_blit.shader_obj, &ptr);
607 if (r) {
608 DRM_ERROR("failed to map blit object %d\n", r);
609 return r;
610 }
611
Alex Deucher1e644d62011-01-27 17:01:52 -0500612 memcpy_toio(ptr + rdev->r600_blit.state_offset,
613 evergreen_default_state, rdev->r600_blit.state_len * 4);
614
615 if (num_packet2s)
616 memcpy_toio(ptr + rdev->r600_blit.state_offset + (rdev->r600_blit.state_len * 4),
617 packet2s, num_packet2s * 4);
Alex Deucher0f234f5f2011-02-13 19:06:33 -0500618 for (i = 0; i < evergreen_vs_size; i++)
619 *(u32 *)((unsigned long)ptr + rdev->r600_blit.vs_offset + i * 4) = cpu_to_le32(evergreen_vs[i]);
620 for (i = 0; i < evergreen_ps_size; i++)
621 *(u32 *)((unsigned long)ptr + rdev->r600_blit.ps_offset + i * 4) = cpu_to_le32(evergreen_ps[i]);
Alex Deucherd7ccd8f2010-09-09 11:33:36 -0400622 radeon_bo_kunmap(rdev->r600_blit.shader_obj);
623 radeon_bo_unreserve(rdev->r600_blit.shader_obj);
624
625done:
626 r = radeon_bo_reserve(rdev->r600_blit.shader_obj, false);
627 if (unlikely(r != 0))
628 return r;
629 r = radeon_bo_pin(rdev->r600_blit.shader_obj, RADEON_GEM_DOMAIN_VRAM,
630 &rdev->r600_blit.shader_gpu_addr);
631 radeon_bo_unreserve(rdev->r600_blit.shader_obj);
632 if (r) {
633 dev_err(rdev->dev, "(%d) pin blit object failed\n", r);
634 return r;
635 }
Dave Airlie53595332011-03-14 09:47:24 +1000636 radeon_ttm_set_active_vram_size(rdev, rdev->mc.real_vram_size);
Alex Deucherd7ccd8f2010-09-09 11:33:36 -0400637 return 0;
638}
639
640void evergreen_blit_fini(struct radeon_device *rdev)
641{
642 int r;
643
Dave Airlie53595332011-03-14 09:47:24 +1000644 radeon_ttm_set_active_vram_size(rdev, rdev->mc.visible_vram_size);
Alex Deucherd7ccd8f2010-09-09 11:33:36 -0400645 if (rdev->r600_blit.shader_obj == NULL)
646 return;
647 /* If we can't reserve the bo, unref should be enough to destroy
648 * it when it becomes idle.
649 */
650 r = radeon_bo_reserve(rdev->r600_blit.shader_obj, false);
651 if (!r) {
652 radeon_bo_unpin(rdev->r600_blit.shader_obj);
653 radeon_bo_unreserve(rdev->r600_blit.shader_obj);
654 }
655 radeon_bo_unref(&rdev->r600_blit.shader_obj);
656}
657
658static int evergreen_vb_ib_get(struct radeon_device *rdev)
659{
660 int r;
661 r = radeon_ib_get(rdev, &rdev->r600_blit.vb_ib);
662 if (r) {
663 DRM_ERROR("failed to get IB for vertex buffer\n");
664 return r;
665 }
666
667 rdev->r600_blit.vb_total = 64*1024;
668 rdev->r600_blit.vb_used = 0;
669 return 0;
670}
671
672static void evergreen_vb_ib_put(struct radeon_device *rdev)
673{
674 radeon_fence_emit(rdev, rdev->r600_blit.vb_ib->fence);
675 radeon_ib_free(rdev, &rdev->r600_blit.vb_ib);
676}
677
678int evergreen_blit_prepare_copy(struct radeon_device *rdev, int size_bytes)
679{
680 int r;
681 int ring_size, line_size;
682 int max_size;
683 /* loops of emits + fence emit possible */
684 int dwords_per_loop = 74, num_loops;
685
686 r = evergreen_vb_ib_get(rdev);
687 if (r)
688 return r;
689
690 /* 8 bpp vs 32 bpp for xfer unit */
691 if (size_bytes & 3)
692 line_size = 8192;
693 else
694 line_size = 8192 * 4;
695
696 max_size = 8192 * line_size;
697
698 /* major loops cover the max size transfer */
699 num_loops = ((size_bytes + max_size) / max_size);
700 /* minor loops cover the extra non aligned bits */
701 num_loops += ((size_bytes % line_size) ? 1 : 0);
702 /* calculate number of loops correctly */
703 ring_size = num_loops * dwords_per_loop;
704 /* set default + shaders */
Alex Deucher12920592011-02-02 12:37:40 -0500705 ring_size += 52; /* shaders + def state */
Alex Deucherd7ccd8f2010-09-09 11:33:36 -0400706 ring_size += 10; /* fence emit for VB IB */
707 ring_size += 5; /* done copy */
708 ring_size += 10; /* fence emit for done copy */
709 r = radeon_ring_lock(rdev, ring_size);
710 if (r)
711 return r;
712
Alex Deucher12920592011-02-02 12:37:40 -0500713 set_default_state(rdev); /* 36 */
Alex Deucherd7ccd8f2010-09-09 11:33:36 -0400714 set_shaders(rdev); /* 16 */
715 return 0;
716}
717
718void evergreen_blit_done_copy(struct radeon_device *rdev, struct radeon_fence *fence)
719{
720 int r;
721
722 if (rdev->r600_blit.vb_ib)
723 evergreen_vb_ib_put(rdev);
724
725 if (fence)
726 r = radeon_fence_emit(rdev, fence);
727
728 radeon_ring_unlock_commit(rdev);
729}
730
731void evergreen_kms_blit_copy(struct radeon_device *rdev,
732 u64 src_gpu_addr, u64 dst_gpu_addr,
733 int size_bytes)
734{
735 int max_bytes;
736 u64 vb_gpu_addr;
737 u32 *vb;
738
739 DRM_DEBUG("emitting copy %16llx %16llx %d %d\n", src_gpu_addr, dst_gpu_addr,
740 size_bytes, rdev->r600_blit.vb_used);
741 vb = (u32 *)(rdev->r600_blit.vb_ib->ptr + rdev->r600_blit.vb_used);
742 if ((size_bytes & 3) || (src_gpu_addr & 3) || (dst_gpu_addr & 3)) {
743 max_bytes = 8192;
744
745 while (size_bytes) {
746 int cur_size = size_bytes;
747 int src_x = src_gpu_addr & 255;
748 int dst_x = dst_gpu_addr & 255;
749 int h = 1;
Alex Deucher2126d0a2010-10-06 00:13:04 -0400750 src_gpu_addr = src_gpu_addr & ~255ULL;
751 dst_gpu_addr = dst_gpu_addr & ~255ULL;
Alex Deucherd7ccd8f2010-09-09 11:33:36 -0400752
753 if (!src_x && !dst_x) {
754 h = (cur_size / max_bytes);
755 if (h > 8192)
756 h = 8192;
757 if (h == 0)
758 h = 1;
759 else
760 cur_size = max_bytes;
761 } else {
762 if (cur_size > max_bytes)
763 cur_size = max_bytes;
764 if (cur_size > (max_bytes - dst_x))
765 cur_size = (max_bytes - dst_x);
766 if (cur_size > (max_bytes - src_x))
767 cur_size = (max_bytes - src_x);
768 }
769
770 if ((rdev->r600_blit.vb_used + 48) > rdev->r600_blit.vb_total) {
771 WARN_ON(1);
772 }
773
774 vb[0] = i2f(dst_x);
775 vb[1] = 0;
776 vb[2] = i2f(src_x);
777 vb[3] = 0;
778
779 vb[4] = i2f(dst_x);
780 vb[5] = i2f(h);
781 vb[6] = i2f(src_x);
782 vb[7] = i2f(h);
783
784 vb[8] = i2f(dst_x + cur_size);
785 vb[9] = i2f(h);
786 vb[10] = i2f(src_x + cur_size);
787 vb[11] = i2f(h);
788
789 /* src 10 */
790 set_tex_resource(rdev, FMT_8,
791 src_x + cur_size, h, src_x + cur_size,
792 src_gpu_addr);
793
794 /* 5 */
795 cp_set_surface_sync(rdev,
796 PACKET3_TC_ACTION_ENA, (src_x + cur_size * h), src_gpu_addr);
797
798
799 /* dst 17 */
800 set_render_target(rdev, COLOR_8,
801 dst_x + cur_size, h,
802 dst_gpu_addr);
803
804 /* scissors 12 */
805 set_scissors(rdev, dst_x, 0, dst_x + cur_size, h);
806
807 /* 15 */
808 vb_gpu_addr = rdev->r600_blit.vb_ib->gpu_addr + rdev->r600_blit.vb_used;
809 set_vtx_resource(rdev, vb_gpu_addr);
810
811 /* draw 10 */
812 draw_auto(rdev);
813
814 /* 5 */
815 cp_set_surface_sync(rdev,
816 PACKET3_CB_ACTION_ENA | PACKET3_CB0_DEST_BASE_ENA,
817 cur_size * h, dst_gpu_addr);
818
819 vb += 12;
820 rdev->r600_blit.vb_used += 12 * 4;
821
822 src_gpu_addr += cur_size * h;
823 dst_gpu_addr += cur_size * h;
824 size_bytes -= cur_size * h;
825 }
826 } else {
827 max_bytes = 8192 * 4;
828
829 while (size_bytes) {
830 int cur_size = size_bytes;
831 int src_x = (src_gpu_addr & 255);
832 int dst_x = (dst_gpu_addr & 255);
833 int h = 1;
Alex Deucher2126d0a2010-10-06 00:13:04 -0400834 src_gpu_addr = src_gpu_addr & ~255ULL;
835 dst_gpu_addr = dst_gpu_addr & ~255ULL;
Alex Deucherd7ccd8f2010-09-09 11:33:36 -0400836
837 if (!src_x && !dst_x) {
838 h = (cur_size / max_bytes);
839 if (h > 8192)
840 h = 8192;
841 if (h == 0)
842 h = 1;
843 else
844 cur_size = max_bytes;
845 } else {
846 if (cur_size > max_bytes)
847 cur_size = max_bytes;
848 if (cur_size > (max_bytes - dst_x))
849 cur_size = (max_bytes - dst_x);
850 if (cur_size > (max_bytes - src_x))
851 cur_size = (max_bytes - src_x);
852 }
853
854 if ((rdev->r600_blit.vb_used + 48) > rdev->r600_blit.vb_total) {
855 WARN_ON(1);
856 }
857
858 vb[0] = i2f(dst_x / 4);
859 vb[1] = 0;
860 vb[2] = i2f(src_x / 4);
861 vb[3] = 0;
862
863 vb[4] = i2f(dst_x / 4);
864 vb[5] = i2f(h);
865 vb[6] = i2f(src_x / 4);
866 vb[7] = i2f(h);
867
868 vb[8] = i2f((dst_x + cur_size) / 4);
869 vb[9] = i2f(h);
870 vb[10] = i2f((src_x + cur_size) / 4);
871 vb[11] = i2f(h);
872
873 /* src 10 */
874 set_tex_resource(rdev, FMT_8_8_8_8,
875 (src_x + cur_size) / 4,
876 h, (src_x + cur_size) / 4,
877 src_gpu_addr);
878 /* 5 */
879 cp_set_surface_sync(rdev,
880 PACKET3_TC_ACTION_ENA, (src_x + cur_size * h), src_gpu_addr);
881
882 /* dst 17 */
883 set_render_target(rdev, COLOR_8_8_8_8,
884 (dst_x + cur_size) / 4, h,
885 dst_gpu_addr);
886
887 /* scissors 12 */
888 set_scissors(rdev, (dst_x / 4), 0, (dst_x + cur_size / 4), h);
889
890 /* Vertex buffer setup 15 */
891 vb_gpu_addr = rdev->r600_blit.vb_ib->gpu_addr + rdev->r600_blit.vb_used;
892 set_vtx_resource(rdev, vb_gpu_addr);
893
894 /* draw 10 */
895 draw_auto(rdev);
896
897 /* 5 */
898 cp_set_surface_sync(rdev,
899 PACKET3_CB_ACTION_ENA | PACKET3_CB0_DEST_BASE_ENA,
900 cur_size * h, dst_gpu_addr);
901
902 /* 74 ring dwords per loop */
903 vb += 12;
904 rdev->r600_blit.vb_used += 12 * 4;
905
906 src_gpu_addr += cur_size * h;
907 dst_gpu_addr += cur_size * h;
908 size_bytes -= cur_size * h;
909 }
910 }
911}
912