2 * Copyright 2007 Stephane Marchesin
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragr) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
25 #include <core/client.h>
27 #include <core/handle.h>
28 #include <core/namedb.h>
30 #include <subdev/fb.h>
31 #include <subdev/instmem.h>
32 #include <subdev/timer.h>
34 #include <engine/fifo.h>
35 #include <engine/gr.h>
40 nv04_gr_ctx_regs[] = {
45 NV04_PGRAPH_CTX_SWITCH1,
46 NV04_PGRAPH_CTX_SWITCH2,
47 NV04_PGRAPH_CTX_SWITCH3,
48 NV04_PGRAPH_CTX_SWITCH4,
49 NV04_PGRAPH_CTX_CACHE1,
50 NV04_PGRAPH_CTX_CACHE2,
51 NV04_PGRAPH_CTX_CACHE3,
52 NV04_PGRAPH_CTX_CACHE4,
82 NV04_PGRAPH_DMA_START_0,
83 NV04_PGRAPH_DMA_START_1,
84 NV04_PGRAPH_DMA_LENGTH,
86 NV04_PGRAPH_DMA_PITCH,
102 NV04_PGRAPH_BOFFSET5,
112 NV04_PGRAPH_BSWIZZLE2,
113 NV04_PGRAPH_BSWIZZLE5,
116 NV04_PGRAPH_PATT_COLOR0,
117 NV04_PGRAPH_PATT_COLOR1,
118 NV04_PGRAPH_PATT_COLORRAM+0x00,
119 NV04_PGRAPH_PATT_COLORRAM+0x04,
120 NV04_PGRAPH_PATT_COLORRAM+0x08,
121 NV04_PGRAPH_PATT_COLORRAM+0x0c,
122 NV04_PGRAPH_PATT_COLORRAM+0x10,
123 NV04_PGRAPH_PATT_COLORRAM+0x14,
124 NV04_PGRAPH_PATT_COLORRAM+0x18,
125 NV04_PGRAPH_PATT_COLORRAM+0x1c,
126 NV04_PGRAPH_PATT_COLORRAM+0x20,
127 NV04_PGRAPH_PATT_COLORRAM+0x24,
128 NV04_PGRAPH_PATT_COLORRAM+0x28,
129 NV04_PGRAPH_PATT_COLORRAM+0x2c,
130 NV04_PGRAPH_PATT_COLORRAM+0x30,
131 NV04_PGRAPH_PATT_COLORRAM+0x34,
132 NV04_PGRAPH_PATT_COLORRAM+0x38,
133 NV04_PGRAPH_PATT_COLORRAM+0x3c,
134 NV04_PGRAPH_PATT_COLORRAM+0x40,
135 NV04_PGRAPH_PATT_COLORRAM+0x44,
136 NV04_PGRAPH_PATT_COLORRAM+0x48,
137 NV04_PGRAPH_PATT_COLORRAM+0x4c,
138 NV04_PGRAPH_PATT_COLORRAM+0x50,
139 NV04_PGRAPH_PATT_COLORRAM+0x54,
140 NV04_PGRAPH_PATT_COLORRAM+0x58,
141 NV04_PGRAPH_PATT_COLORRAM+0x5c,
142 NV04_PGRAPH_PATT_COLORRAM+0x60,
143 NV04_PGRAPH_PATT_COLORRAM+0x64,
144 NV04_PGRAPH_PATT_COLORRAM+0x68,
145 NV04_PGRAPH_PATT_COLORRAM+0x6c,
146 NV04_PGRAPH_PATT_COLORRAM+0x70,
147 NV04_PGRAPH_PATT_COLORRAM+0x74,
148 NV04_PGRAPH_PATT_COLORRAM+0x78,
149 NV04_PGRAPH_PATT_COLORRAM+0x7c,
150 NV04_PGRAPH_PATT_COLORRAM+0x80,
151 NV04_PGRAPH_PATT_COLORRAM+0x84,
152 NV04_PGRAPH_PATT_COLORRAM+0x88,
153 NV04_PGRAPH_PATT_COLORRAM+0x8c,
154 NV04_PGRAPH_PATT_COLORRAM+0x90,
155 NV04_PGRAPH_PATT_COLORRAM+0x94,
156 NV04_PGRAPH_PATT_COLORRAM+0x98,
157 NV04_PGRAPH_PATT_COLORRAM+0x9c,
158 NV04_PGRAPH_PATT_COLORRAM+0xa0,
159 NV04_PGRAPH_PATT_COLORRAM+0xa4,
160 NV04_PGRAPH_PATT_COLORRAM+0xa8,
161 NV04_PGRAPH_PATT_COLORRAM+0xac,
162 NV04_PGRAPH_PATT_COLORRAM+0xb0,
163 NV04_PGRAPH_PATT_COLORRAM+0xb4,
164 NV04_PGRAPH_PATT_COLORRAM+0xb8,
165 NV04_PGRAPH_PATT_COLORRAM+0xbc,
166 NV04_PGRAPH_PATT_COLORRAM+0xc0,
167 NV04_PGRAPH_PATT_COLORRAM+0xc4,
168 NV04_PGRAPH_PATT_COLORRAM+0xc8,
169 NV04_PGRAPH_PATT_COLORRAM+0xcc,
170 NV04_PGRAPH_PATT_COLORRAM+0xd0,
171 NV04_PGRAPH_PATT_COLORRAM+0xd4,
172 NV04_PGRAPH_PATT_COLORRAM+0xd8,
173 NV04_PGRAPH_PATT_COLORRAM+0xdc,
174 NV04_PGRAPH_PATT_COLORRAM+0xe0,
175 NV04_PGRAPH_PATT_COLORRAM+0xe4,
176 NV04_PGRAPH_PATT_COLORRAM+0xe8,
177 NV04_PGRAPH_PATT_COLORRAM+0xec,
178 NV04_PGRAPH_PATT_COLORRAM+0xf0,
179 NV04_PGRAPH_PATT_COLORRAM+0xf4,
180 NV04_PGRAPH_PATT_COLORRAM+0xf8,
181 NV04_PGRAPH_PATT_COLORRAM+0xfc,
184 NV04_PGRAPH_PATTERN_SHAPE,
188 NV04_PGRAPH_BETA_AND,
189 NV04_PGRAPH_BETA_PREMULT,
190 NV04_PGRAPH_CONTROL0,
191 NV04_PGRAPH_CONTROL1,
192 NV04_PGRAPH_CONTROL2,
194 NV04_PGRAPH_STORED_FMT,
195 NV04_PGRAPH_SOURCE_COLOR,
339 NV04_PGRAPH_PASSTHRU_0,
340 NV04_PGRAPH_PASSTHRU_1,
341 NV04_PGRAPH_PASSTHRU_2,
342 NV04_PGRAPH_DVD_COLORFMT,
343 NV04_PGRAPH_SCALED_FORMAT,
344 NV04_PGRAPH_MISC24_0,
345 NV04_PGRAPH_MISC24_1,
346 NV04_PGRAPH_MISC24_2,
354 struct nv04_gr_priv {
355 struct nouveau_gr base;
356 struct nv04_gr_chan *chan[16];
360 struct nv04_gr_chan {
361 struct nouveau_object base;
363 u32 nv04[ARRAY_SIZE(nv04_gr_ctx_regs)];
367 static inline struct nv04_gr_priv *
368 nv04_gr_priv(struct nv04_gr_chan *chan)
370 return (void *)nv_object(chan)->engine;
373 /*******************************************************************************
374 * Graphics object classes
375 ******************************************************************************/
378 * Software methods, why they are needed, and how they all work:
380 * NV04 and NV05 keep most of the state in PGRAPH context itself, but some
381 * 2d engine settings are kept inside the grobjs themselves. The grobjs are
382 * 3 words long on both. grobj format on NV04 is:
386 * - bit 12: color key active
387 * - bit 13: clip rect active
388 * - bit 14: if set, destination surface is swizzled and taken from buffer 5
389 * [set by NV04_SWIZZLED_SURFACE], otherwise it's linear and taken
390 * from buffer 0 [set by NV04_CONTEXT_SURFACES_2D or
391 * NV03_CONTEXT_SURFACE_DST].
392 * - bits 15-17: 2d operation [aka patch config]
393 * - bit 24: patch valid [enables rendering using this object]
394 * - bit 25: surf3d valid [for tex_tri and multitex_tri only]
396 * - bits 0-1: mono format
397 * - bits 8-13: color format
398 * - bits 16-31: DMA_NOTIFY instance
400 * - bits 0-15: DMA_A instance
401 * - bits 16-31: DMA_B instance
407 * - bit 12: color key active
408 * - bit 13: clip rect active
409 * - bit 14: if set, destination surface is swizzled and taken from buffer 5
410 * [set by NV04_SWIZZLED_SURFACE], otherwise it's linear and taken
411 * from buffer 0 [set by NV04_CONTEXT_SURFACES_2D or
412 * NV03_CONTEXT_SURFACE_DST].
413 * - bits 15-17: 2d operation [aka patch config]
414 * - bits 20-22: dither mode
415 * - bit 24: patch valid [enables rendering using this object]
416 * - bit 25: surface_dst/surface_color/surf2d/surf3d valid
417 * - bit 26: surface_src/surface_zeta valid
418 * - bit 27: pattern valid
419 * - bit 28: rop valid
420 * - bit 29: beta1 valid
421 * - bit 30: beta4 valid
423 * - bits 0-1: mono format
424 * - bits 8-13: color format
425 * - bits 16-31: DMA_NOTIFY instance
427 * - bits 0-15: DMA_A instance
428 * - bits 16-31: DMA_B instance
430 * NV05 will set/unset the relevant valid bits when you poke the relevant
431 * object-binding methods with object of the proper type, or with the NULL
432 * type. It'll only allow rendering using the grobj if all needed objects
433 * are bound. The needed set of objects depends on selected operation: for
434 * example rop object is needed by ROP_AND, but not by SRCCOPY_AND.
436 * NV04 doesn't have these methods implemented at all, and doesn't have the
437 * relevant bits in grobj. Instead, it'll allow rendering whenever bit 24
438 * is set. So we have to emulate them in software, internally keeping the
439 * same bits as NV05 does. Since grobjs are aligned to 16 bytes on nv04,
440 * but the last word isn't actually used for anything, we abuse it for this
443 * Actually, NV05 can optionally check bit 24 too, but we disable this since
444 * there's no use for it.
446 * For unknown reasons, NV04 implements surf3d binding in hardware as an
447 * exception. Also for unknown reasons, NV04 doesn't implement the clipping
448 * methods on the surf3d object, so we have to emulate them too.
452 nv04_gr_set_ctx1(struct nouveau_object *object, u32 mask, u32 value)
454 struct nv04_gr_priv *priv = (void *)object->engine;
455 int subc = (nv_rd32(priv, NV04_PGRAPH_TRAPPED_ADDR) >> 13) & 0x7;
458 tmp = nv_ro32(object, 0x00);
461 nv_wo32(object, 0x00, tmp);
463 nv_wr32(priv, NV04_PGRAPH_CTX_SWITCH1, tmp);
464 nv_wr32(priv, NV04_PGRAPH_CTX_CACHE1 + (subc<<2), tmp);
468 nv04_gr_set_ctx_val(struct nouveau_object *object, u32 mask, u32 value)
470 int class, op, valid = 1;
473 ctx1 = nv_ro32(object, 0x00);
475 op = (ctx1 >> 15) & 7;
477 tmp = nv_ro32(object, 0x0c);
480 nv_wo32(object, 0x0c, tmp);
482 /* check for valid surf2d/surf_dst/surf_color */
483 if (!(tmp & 0x02000000))
485 /* check for valid surf_src/surf_zeta */
486 if ((class == 0x1f || class == 0x48) && !(tmp & 0x04000000))
490 /* SRCCOPY_AND, SRCCOPY: no extra objects required */
494 /* ROP_AND: requires pattern and rop */
496 if (!(tmp & 0x18000000))
499 /* BLEND_AND: requires beta1 */
501 if (!(tmp & 0x20000000))
504 /* SRCCOPY_PREMULT, BLEND_PREMULT: beta4 required */
507 if (!(tmp & 0x40000000))
512 nv04_gr_set_ctx1(object, 0x01000000, valid << 24);
516 nv04_gr_mthd_set_operation(struct nouveau_object *object, u32 mthd,
517 void *args, u32 size)
519 u32 class = nv_ro32(object, 0) & 0xff;
520 u32 data = *(u32 *)args;
523 /* Old versions of the objects only accept first three operations. */
524 if (data > 2 && class < 0x40)
526 nv04_gr_set_ctx1(object, 0x00038000, data << 15);
527 /* changing operation changes set of objects needed for validation */
528 nv04_gr_set_ctx_val(object, 0, 0);
533 nv04_gr_mthd_surf3d_clip_h(struct nouveau_object *object, u32 mthd,
534 void *args, u32 size)
536 struct nv04_gr_priv *priv = (void *)object->engine;
537 u32 data = *(u32 *)args;
538 u32 min = data & 0xffff, max;
544 /* yes, it accepts negative for some reason. */
548 nv_wr32(priv, 0x40053c, min);
549 nv_wr32(priv, 0x400544, max);
554 nv04_gr_mthd_surf3d_clip_v(struct nouveau_object *object, u32 mthd,
555 void *args, u32 size)
557 struct nv04_gr_priv *priv = (void *)object->engine;
558 u32 data = *(u32 *)args;
559 u32 min = data & 0xffff, max;
565 /* yes, it accepts negative for some reason. */
569 nv_wr32(priv, 0x400540, min);
570 nv_wr32(priv, 0x400548, max);
575 nv04_gr_mthd_bind_class(struct nouveau_object *object, u32 *args, u32 size)
577 struct nouveau_instmem *imem = nouveau_instmem(object);
578 u32 inst = *(u32 *)args << 4;
579 return nv_ro32(imem, inst);
583 nv04_gr_mthd_bind_surf2d(struct nouveau_object *object, u32 mthd,
584 void *args, u32 size)
586 switch (nv04_gr_mthd_bind_class(object, args, size)) {
588 nv04_gr_set_ctx1(object, 0x00004000, 0);
589 nv04_gr_set_ctx_val(object, 0x02000000, 0);
592 nv04_gr_set_ctx1(object, 0x00004000, 0);
593 nv04_gr_set_ctx_val(object, 0x02000000, 0x02000000);
600 nv04_gr_mthd_bind_surf2d_swzsurf(struct nouveau_object *object, u32 mthd,
601 void *args, u32 size)
603 switch (nv04_gr_mthd_bind_class(object, args, size)) {
605 nv04_gr_set_ctx1(object, 0x00004000, 0);
606 nv04_gr_set_ctx_val(object, 0x02000000, 0);
609 nv04_gr_set_ctx1(object, 0x00004000, 0);
610 nv04_gr_set_ctx_val(object, 0x02000000, 0x02000000);
613 nv04_gr_set_ctx1(object, 0x00004000, 0x00004000);
614 nv04_gr_set_ctx_val(object, 0x02000000, 0x02000000);
621 nv01_gr_mthd_bind_patt(struct nouveau_object *object, u32 mthd,
622 void *args, u32 size)
624 switch (nv04_gr_mthd_bind_class(object, args, size)) {
626 nv04_gr_set_ctx_val(object, 0x08000000, 0);
629 nv04_gr_set_ctx_val(object, 0x08000000, 0x08000000);
636 nv04_gr_mthd_bind_patt(struct nouveau_object *object, u32 mthd,
637 void *args, u32 size)
639 switch (nv04_gr_mthd_bind_class(object, args, size)) {
641 nv04_gr_set_ctx_val(object, 0x08000000, 0);
644 nv04_gr_set_ctx_val(object, 0x08000000, 0x08000000);
651 nv04_gr_mthd_bind_rop(struct nouveau_object *object, u32 mthd,
652 void *args, u32 size)
654 switch (nv04_gr_mthd_bind_class(object, args, size)) {
656 nv04_gr_set_ctx_val(object, 0x10000000, 0);
659 nv04_gr_set_ctx_val(object, 0x10000000, 0x10000000);
666 nv04_gr_mthd_bind_beta1(struct nouveau_object *object, u32 mthd,
667 void *args, u32 size)
669 switch (nv04_gr_mthd_bind_class(object, args, size)) {
671 nv04_gr_set_ctx_val(object, 0x20000000, 0);
674 nv04_gr_set_ctx_val(object, 0x20000000, 0x20000000);
681 nv04_gr_mthd_bind_beta4(struct nouveau_object *object, u32 mthd,
682 void *args, u32 size)
684 switch (nv04_gr_mthd_bind_class(object, args, size)) {
686 nv04_gr_set_ctx_val(object, 0x40000000, 0);
689 nv04_gr_set_ctx_val(object, 0x40000000, 0x40000000);
696 nv04_gr_mthd_bind_surf_dst(struct nouveau_object *object, u32 mthd,
697 void *args, u32 size)
699 switch (nv04_gr_mthd_bind_class(object, args, size)) {
701 nv04_gr_set_ctx_val(object, 0x02000000, 0);
704 nv04_gr_set_ctx_val(object, 0x02000000, 0x02000000);
711 nv04_gr_mthd_bind_surf_src(struct nouveau_object *object, u32 mthd,
712 void *args, u32 size)
714 switch (nv04_gr_mthd_bind_class(object, args, size)) {
716 nv04_gr_set_ctx_val(object, 0x04000000, 0);
719 nv04_gr_set_ctx_val(object, 0x04000000, 0x04000000);
726 nv04_gr_mthd_bind_surf_color(struct nouveau_object *object, u32 mthd,
727 void *args, u32 size)
729 switch (nv04_gr_mthd_bind_class(object, args, size)) {
731 nv04_gr_set_ctx_val(object, 0x02000000, 0);
734 nv04_gr_set_ctx_val(object, 0x02000000, 0x02000000);
741 nv04_gr_mthd_bind_surf_zeta(struct nouveau_object *object, u32 mthd,
742 void *args, u32 size)
744 switch (nv04_gr_mthd_bind_class(object, args, size)) {
746 nv04_gr_set_ctx_val(object, 0x04000000, 0);
749 nv04_gr_set_ctx_val(object, 0x04000000, 0x04000000);
756 nv01_gr_mthd_bind_clip(struct nouveau_object *object, u32 mthd,
757 void *args, u32 size)
759 switch (nv04_gr_mthd_bind_class(object, args, size)) {
761 nv04_gr_set_ctx1(object, 0x2000, 0);
764 nv04_gr_set_ctx1(object, 0x2000, 0x2000);
771 nv01_gr_mthd_bind_chroma(struct nouveau_object *object, u32 mthd,
772 void *args, u32 size)
774 switch (nv04_gr_mthd_bind_class(object, args, size)) {
776 nv04_gr_set_ctx1(object, 0x1000, 0);
778 /* Yes, for some reason even the old versions of objects
779 * accept 0x57 and not 0x17. Consistency be damned.
782 nv04_gr_set_ctx1(object, 0x1000, 0x1000);
788 static struct nouveau_omthds
789 nv03_gr_gdi_omthds[] = {
790 { 0x0184, 0x0184, nv01_gr_mthd_bind_patt },
791 { 0x0188, 0x0188, nv04_gr_mthd_bind_rop },
792 { 0x018c, 0x018c, nv04_gr_mthd_bind_beta1 },
793 { 0x0190, 0x0190, nv04_gr_mthd_bind_surf_dst },
794 { 0x02fc, 0x02fc, nv04_gr_mthd_set_operation },
798 static struct nouveau_omthds
799 nv04_gr_gdi_omthds[] = {
800 { 0x0188, 0x0188, nv04_gr_mthd_bind_patt },
801 { 0x018c, 0x018c, nv04_gr_mthd_bind_rop },
802 { 0x0190, 0x0190, nv04_gr_mthd_bind_beta1 },
803 { 0x0194, 0x0194, nv04_gr_mthd_bind_beta4 },
804 { 0x0198, 0x0198, nv04_gr_mthd_bind_surf2d },
805 { 0x02fc, 0x02fc, nv04_gr_mthd_set_operation },
809 static struct nouveau_omthds
810 nv01_gr_blit_omthds[] = {
811 { 0x0184, 0x0184, nv01_gr_mthd_bind_chroma },
812 { 0x0188, 0x0188, nv01_gr_mthd_bind_clip },
813 { 0x018c, 0x018c, nv01_gr_mthd_bind_patt },
814 { 0x0190, 0x0190, nv04_gr_mthd_bind_rop },
815 { 0x0194, 0x0194, nv04_gr_mthd_bind_beta1 },
816 { 0x0198, 0x0198, nv04_gr_mthd_bind_surf_dst },
817 { 0x019c, 0x019c, nv04_gr_mthd_bind_surf_src },
818 { 0x02fc, 0x02fc, nv04_gr_mthd_set_operation },
822 static struct nouveau_omthds
823 nv04_gr_blit_omthds[] = {
824 { 0x0184, 0x0184, nv01_gr_mthd_bind_chroma },
825 { 0x0188, 0x0188, nv01_gr_mthd_bind_clip },
826 { 0x018c, 0x018c, nv04_gr_mthd_bind_patt },
827 { 0x0190, 0x0190, nv04_gr_mthd_bind_rop },
828 { 0x0194, 0x0194, nv04_gr_mthd_bind_beta1 },
829 { 0x0198, 0x0198, nv04_gr_mthd_bind_beta4 },
830 { 0x019c, 0x019c, nv04_gr_mthd_bind_surf2d },
831 { 0x02fc, 0x02fc, nv04_gr_mthd_set_operation },
835 static struct nouveau_omthds
836 nv04_gr_iifc_omthds[] = {
837 { 0x0188, 0x0188, nv01_gr_mthd_bind_chroma },
838 { 0x018c, 0x018c, nv01_gr_mthd_bind_clip },
839 { 0x0190, 0x0190, nv04_gr_mthd_bind_patt },
840 { 0x0194, 0x0194, nv04_gr_mthd_bind_rop },
841 { 0x0198, 0x0198, nv04_gr_mthd_bind_beta1 },
842 { 0x019c, 0x019c, nv04_gr_mthd_bind_beta4 },
843 { 0x01a0, 0x01a0, nv04_gr_mthd_bind_surf2d_swzsurf },
844 { 0x03e4, 0x03e4, nv04_gr_mthd_set_operation },
848 static struct nouveau_omthds
849 nv01_gr_ifc_omthds[] = {
850 { 0x0184, 0x0184, nv01_gr_mthd_bind_chroma },
851 { 0x0188, 0x0188, nv01_gr_mthd_bind_clip },
852 { 0x018c, 0x018c, nv01_gr_mthd_bind_patt },
853 { 0x0190, 0x0190, nv04_gr_mthd_bind_rop },
854 { 0x0194, 0x0194, nv04_gr_mthd_bind_beta1 },
855 { 0x0198, 0x0198, nv04_gr_mthd_bind_surf_dst },
856 { 0x02fc, 0x02fc, nv04_gr_mthd_set_operation },
860 static struct nouveau_omthds
861 nv04_gr_ifc_omthds[] = {
862 { 0x0184, 0x0184, nv01_gr_mthd_bind_chroma },
863 { 0x0188, 0x0188, nv01_gr_mthd_bind_clip },
864 { 0x018c, 0x018c, nv04_gr_mthd_bind_patt },
865 { 0x0190, 0x0190, nv04_gr_mthd_bind_rop },
866 { 0x0194, 0x0194, nv04_gr_mthd_bind_beta1 },
867 { 0x0198, 0x0198, nv04_gr_mthd_bind_beta4 },
868 { 0x019c, 0x019c, nv04_gr_mthd_bind_surf2d },
869 { 0x02fc, 0x02fc, nv04_gr_mthd_set_operation },
873 static struct nouveau_omthds
874 nv03_gr_sifc_omthds[] = {
875 { 0x0184, 0x0184, nv01_gr_mthd_bind_chroma },
876 { 0x0188, 0x0188, nv01_gr_mthd_bind_patt },
877 { 0x018c, 0x018c, nv04_gr_mthd_bind_rop },
878 { 0x0190, 0x0190, nv04_gr_mthd_bind_beta1 },
879 { 0x0194, 0x0194, nv04_gr_mthd_bind_surf_dst },
880 { 0x02fc, 0x02fc, nv04_gr_mthd_set_operation },
884 static struct nouveau_omthds
885 nv04_gr_sifc_omthds[] = {
886 { 0x0184, 0x0184, nv01_gr_mthd_bind_chroma },
887 { 0x0188, 0x0188, nv04_gr_mthd_bind_patt },
888 { 0x018c, 0x018c, nv04_gr_mthd_bind_rop },
889 { 0x0190, 0x0190, nv04_gr_mthd_bind_beta1 },
890 { 0x0194, 0x0194, nv04_gr_mthd_bind_beta4 },
891 { 0x0198, 0x0198, nv04_gr_mthd_bind_surf2d },
892 { 0x02fc, 0x02fc, nv04_gr_mthd_set_operation },
896 static struct nouveau_omthds
897 nv03_gr_sifm_omthds[] = {
898 { 0x0188, 0x0188, nv01_gr_mthd_bind_patt },
899 { 0x018c, 0x018c, nv04_gr_mthd_bind_rop },
900 { 0x0190, 0x0190, nv04_gr_mthd_bind_beta1 },
901 { 0x0194, 0x0194, nv04_gr_mthd_bind_surf_dst },
902 { 0x0304, 0x0304, nv04_gr_mthd_set_operation },
906 static struct nouveau_omthds
907 nv04_gr_sifm_omthds[] = {
908 { 0x0188, 0x0188, nv04_gr_mthd_bind_patt },
909 { 0x018c, 0x018c, nv04_gr_mthd_bind_rop },
910 { 0x0190, 0x0190, nv04_gr_mthd_bind_beta1 },
911 { 0x0194, 0x0194, nv04_gr_mthd_bind_beta4 },
912 { 0x0198, 0x0198, nv04_gr_mthd_bind_surf2d },
913 { 0x0304, 0x0304, nv04_gr_mthd_set_operation },
917 static struct nouveau_omthds
918 nv04_gr_surf3d_omthds[] = {
919 { 0x02f8, 0x02f8, nv04_gr_mthd_surf3d_clip_h },
920 { 0x02fc, 0x02fc, nv04_gr_mthd_surf3d_clip_v },
924 static struct nouveau_omthds
925 nv03_gr_ttri_omthds[] = {
926 { 0x0188, 0x0188, nv01_gr_mthd_bind_clip },
927 { 0x018c, 0x018c, nv04_gr_mthd_bind_surf_color },
928 { 0x0190, 0x0190, nv04_gr_mthd_bind_surf_zeta },
932 static struct nouveau_omthds
933 nv01_gr_prim_omthds[] = {
934 { 0x0184, 0x0184, nv01_gr_mthd_bind_clip },
935 { 0x0188, 0x0188, nv01_gr_mthd_bind_patt },
936 { 0x018c, 0x018c, nv04_gr_mthd_bind_rop },
937 { 0x0190, 0x0190, nv04_gr_mthd_bind_beta1 },
938 { 0x0194, 0x0194, nv04_gr_mthd_bind_surf_dst },
939 { 0x02fc, 0x02fc, nv04_gr_mthd_set_operation },
943 static struct nouveau_omthds
944 nv04_gr_prim_omthds[] = {
945 { 0x0184, 0x0184, nv01_gr_mthd_bind_clip },
946 { 0x0188, 0x0188, nv04_gr_mthd_bind_patt },
947 { 0x018c, 0x018c, nv04_gr_mthd_bind_rop },
948 { 0x0190, 0x0190, nv04_gr_mthd_bind_beta1 },
949 { 0x0194, 0x0194, nv04_gr_mthd_bind_beta4 },
950 { 0x0198, 0x0198, nv04_gr_mthd_bind_surf2d },
951 { 0x02fc, 0x02fc, nv04_gr_mthd_set_operation },
956 nv04_gr_object_ctor(struct nouveau_object *parent,
957 struct nouveau_object *engine,
958 struct nouveau_oclass *oclass, void *data, u32 size,
959 struct nouveau_object **pobject)
961 struct nouveau_gpuobj *obj;
964 ret = nouveau_gpuobj_create(parent, engine, oclass, 0, parent,
966 *pobject = nv_object(obj);
970 nv_wo32(obj, 0x00, nv_mclass(obj));
972 nv_mo32(obj, 0x00, 0x00080000, 0x00080000);
974 nv_wo32(obj, 0x04, 0x00000000);
975 nv_wo32(obj, 0x08, 0x00000000);
976 nv_wo32(obj, 0x0c, 0x00000000);
980 struct nouveau_ofuncs
982 .ctor = nv04_gr_object_ctor,
983 .dtor = _nouveau_gpuobj_dtor,
984 .init = _nouveau_gpuobj_init,
985 .fini = _nouveau_gpuobj_fini,
986 .rd32 = _nouveau_gpuobj_rd32,
987 .wr32 = _nouveau_gpuobj_wr32,
990 static struct nouveau_oclass
992 { 0x0012, &nv04_gr_ofuncs }, /* beta1 */
993 { 0x0017, &nv04_gr_ofuncs }, /* chroma */
994 { 0x0018, &nv04_gr_ofuncs }, /* pattern (nv01) */
995 { 0x0019, &nv04_gr_ofuncs }, /* clip */
996 { 0x001c, &nv04_gr_ofuncs, nv01_gr_prim_omthds }, /* line */
997 { 0x001d, &nv04_gr_ofuncs, nv01_gr_prim_omthds }, /* tri */
998 { 0x001e, &nv04_gr_ofuncs, nv01_gr_prim_omthds }, /* rect */
999 { 0x001f, &nv04_gr_ofuncs, nv01_gr_blit_omthds },
1000 { 0x0021, &nv04_gr_ofuncs, nv01_gr_ifc_omthds },
1001 { 0x0030, &nv04_gr_ofuncs }, /* null */
1002 { 0x0036, &nv04_gr_ofuncs, nv03_gr_sifc_omthds },
1003 { 0x0037, &nv04_gr_ofuncs, nv03_gr_sifm_omthds },
1004 { 0x0038, &nv04_gr_ofuncs }, /* dvd subpicture */
1005 { 0x0039, &nv04_gr_ofuncs }, /* m2mf */
1006 { 0x0042, &nv04_gr_ofuncs }, /* surf2d */
1007 { 0x0043, &nv04_gr_ofuncs }, /* rop */
1008 { 0x0044, &nv04_gr_ofuncs }, /* pattern */
1009 { 0x0048, &nv04_gr_ofuncs, nv03_gr_ttri_omthds },
1010 { 0x004a, &nv04_gr_ofuncs, nv04_gr_gdi_omthds },
1011 { 0x004b, &nv04_gr_ofuncs, nv03_gr_gdi_omthds },
1012 { 0x0052, &nv04_gr_ofuncs }, /* swzsurf */
1013 { 0x0053, &nv04_gr_ofuncs, nv04_gr_surf3d_omthds },
1014 { 0x0054, &nv04_gr_ofuncs }, /* ttri */
1015 { 0x0055, &nv04_gr_ofuncs }, /* mtri */
1016 { 0x0057, &nv04_gr_ofuncs }, /* chroma */
1017 { 0x0058, &nv04_gr_ofuncs }, /* surf_dst */
1018 { 0x0059, &nv04_gr_ofuncs }, /* surf_src */
1019 { 0x005a, &nv04_gr_ofuncs }, /* surf_color */
1020 { 0x005b, &nv04_gr_ofuncs }, /* surf_zeta */
1021 { 0x005c, &nv04_gr_ofuncs, nv04_gr_prim_omthds }, /* line */
1022 { 0x005d, &nv04_gr_ofuncs, nv04_gr_prim_omthds }, /* tri */
1023 { 0x005e, &nv04_gr_ofuncs, nv04_gr_prim_omthds }, /* rect */
1024 { 0x005f, &nv04_gr_ofuncs, nv04_gr_blit_omthds },
1025 { 0x0060, &nv04_gr_ofuncs, nv04_gr_iifc_omthds },
1026 { 0x0061, &nv04_gr_ofuncs, nv04_gr_ifc_omthds },
1027 { 0x0064, &nv04_gr_ofuncs }, /* iifc (nv05) */
1028 { 0x0065, &nv04_gr_ofuncs }, /* ifc (nv05) */
1029 { 0x0066, &nv04_gr_ofuncs }, /* sifc (nv05) */
1030 { 0x0072, &nv04_gr_ofuncs }, /* beta4 */
1031 { 0x0076, &nv04_gr_ofuncs, nv04_gr_sifc_omthds },
1032 { 0x0077, &nv04_gr_ofuncs, nv04_gr_sifm_omthds },
1036 /*******************************************************************************
1038 ******************************************************************************/
1040 static struct nv04_gr_chan *
1041 nv04_gr_channel(struct nv04_gr_priv *priv)
1043 struct nv04_gr_chan *chan = NULL;
1044 if (nv_rd32(priv, NV04_PGRAPH_CTX_CONTROL) & 0x00010000) {
1045 int chid = nv_rd32(priv, NV04_PGRAPH_CTX_USER) >> 24;
1046 if (chid < ARRAY_SIZE(priv->chan))
1047 chan = priv->chan[chid];
1053 nv04_gr_load_context(struct nv04_gr_chan *chan, int chid)
1055 struct nv04_gr_priv *priv = nv04_gr_priv(chan);
1058 for (i = 0; i < ARRAY_SIZE(nv04_gr_ctx_regs); i++)
1059 nv_wr32(priv, nv04_gr_ctx_regs[i], chan->nv04[i]);
1061 nv_wr32(priv, NV04_PGRAPH_CTX_CONTROL, 0x10010100);
1062 nv_mask(priv, NV04_PGRAPH_CTX_USER, 0xff000000, chid << 24);
1063 nv_mask(priv, NV04_PGRAPH_FFINTFC_ST2, 0xfff00000, 0x00000000);
1068 nv04_gr_unload_context(struct nv04_gr_chan *chan)
1070 struct nv04_gr_priv *priv = nv04_gr_priv(chan);
1073 for (i = 0; i < ARRAY_SIZE(nv04_gr_ctx_regs); i++)
1074 chan->nv04[i] = nv_rd32(priv, nv04_gr_ctx_regs[i]);
1076 nv_wr32(priv, NV04_PGRAPH_CTX_CONTROL, 0x10000000);
1077 nv_mask(priv, NV04_PGRAPH_CTX_USER, 0xff000000, 0x0f000000);
1082 nv04_gr_context_switch(struct nv04_gr_priv *priv)
1084 struct nv04_gr_chan *prev = NULL;
1085 struct nv04_gr_chan *next = NULL;
1086 unsigned long flags;
1089 spin_lock_irqsave(&priv->lock, flags);
1092 /* If previous context is valid, we need to save it */
1093 prev = nv04_gr_channel(priv);
1095 nv04_gr_unload_context(prev);
1097 /* load context for next channel */
1098 chid = (nv_rd32(priv, NV04_PGRAPH_TRAPPED_ADDR) >> 24) & 0x0f;
1099 next = priv->chan[chid];
1101 nv04_gr_load_context(next, chid);
1103 spin_unlock_irqrestore(&priv->lock, flags);
1106 static u32 *ctx_reg(struct nv04_gr_chan *chan, u32 reg)
1110 for (i = 0; i < ARRAY_SIZE(nv04_gr_ctx_regs); i++) {
1111 if (nv04_gr_ctx_regs[i] == reg)
1112 return &chan->nv04[i];
1119 nv04_gr_context_ctor(struct nouveau_object *parent,
1120 struct nouveau_object *engine,
1121 struct nouveau_oclass *oclass, void *data, u32 size,
1122 struct nouveau_object **pobject)
1124 struct nouveau_fifo_chan *fifo = (void *)parent;
1125 struct nv04_gr_priv *priv = (void *)engine;
1126 struct nv04_gr_chan *chan;
1127 unsigned long flags;
1130 ret = nouveau_object_create(parent, engine, oclass, 0, &chan);
1131 *pobject = nv_object(chan);
1135 spin_lock_irqsave(&priv->lock, flags);
1136 if (priv->chan[fifo->chid]) {
1137 *pobject = nv_object(priv->chan[fifo->chid]);
1138 atomic_inc(&(*pobject)->refcount);
1139 spin_unlock_irqrestore(&priv->lock, flags);
1140 nouveau_object_destroy(&chan->base);
1144 *ctx_reg(chan, NV04_PGRAPH_DEBUG_3) = 0xfad4ff31;
1146 priv->chan[fifo->chid] = chan;
1147 chan->chid = fifo->chid;
1148 spin_unlock_irqrestore(&priv->lock, flags);
1153 nv04_gr_context_dtor(struct nouveau_object *object)
1155 struct nv04_gr_priv *priv = (void *)object->engine;
1156 struct nv04_gr_chan *chan = (void *)object;
1157 unsigned long flags;
1159 spin_lock_irqsave(&priv->lock, flags);
1160 priv->chan[chan->chid] = NULL;
1161 spin_unlock_irqrestore(&priv->lock, flags);
1163 nouveau_object_destroy(&chan->base);
1167 nv04_gr_context_fini(struct nouveau_object *object, bool suspend)
1169 struct nv04_gr_priv *priv = (void *)object->engine;
1170 struct nv04_gr_chan *chan = (void *)object;
1171 unsigned long flags;
1173 spin_lock_irqsave(&priv->lock, flags);
1174 nv_mask(priv, NV04_PGRAPH_FIFO, 0x00000001, 0x00000000);
1175 if (nv04_gr_channel(priv) == chan)
1176 nv04_gr_unload_context(chan);
1177 nv_mask(priv, NV04_PGRAPH_FIFO, 0x00000001, 0x00000001);
1178 spin_unlock_irqrestore(&priv->lock, flags);
1180 return nouveau_object_fini(&chan->base, suspend);
1183 static struct nouveau_oclass
1185 .handle = NV_ENGCTX(GR, 0x04),
1186 .ofuncs = &(struct nouveau_ofuncs) {
1187 .ctor = nv04_gr_context_ctor,
1188 .dtor = nv04_gr_context_dtor,
1189 .init = nouveau_object_init,
1190 .fini = nv04_gr_context_fini,
1194 /*******************************************************************************
1195 * PGRAPH engine/subdev functions
1196 ******************************************************************************/
1199 nv04_gr_idle(void *obj)
1201 struct nouveau_gr *gr = nouveau_gr(obj);
1202 u32 mask = 0xffffffff;
1204 if (nv_device(obj)->card_type == NV_40)
1205 mask &= ~NV40_PGRAPH_STATUS_SYNC_STALL;
1207 if (!nv_wait(gr, NV04_PGRAPH_STATUS, mask, 0)) {
1208 nv_error(gr, "idle timed out with status 0x%08x\n",
1209 nv_rd32(gr, NV04_PGRAPH_STATUS));
1216 static const struct nouveau_bitfield
1217 nv04_gr_intr_name[] = {
1218 { NV_PGRAPH_INTR_NOTIFY, "NOTIFY" },
1222 static const struct nouveau_bitfield
1223 nv04_gr_nstatus[] = {
1224 { NV04_PGRAPH_NSTATUS_STATE_IN_USE, "STATE_IN_USE" },
1225 { NV04_PGRAPH_NSTATUS_INVALID_STATE, "INVALID_STATE" },
1226 { NV04_PGRAPH_NSTATUS_BAD_ARGUMENT, "BAD_ARGUMENT" },
1227 { NV04_PGRAPH_NSTATUS_PROTECTION_FAULT, "PROTECTION_FAULT" },
1231 const struct nouveau_bitfield
1232 nv04_gr_nsource[] = {
1233 { NV03_PGRAPH_NSOURCE_NOTIFICATION, "NOTIFICATION" },
1234 { NV03_PGRAPH_NSOURCE_DATA_ERROR, "DATA_ERROR" },
1235 { NV03_PGRAPH_NSOURCE_PROTECTION_ERROR, "PROTECTION_ERROR" },
1236 { NV03_PGRAPH_NSOURCE_RANGE_EXCEPTION, "RANGE_EXCEPTION" },
1237 { NV03_PGRAPH_NSOURCE_LIMIT_COLOR, "LIMIT_COLOR" },
1238 { NV03_PGRAPH_NSOURCE_LIMIT_ZETA, "LIMIT_ZETA" },
1239 { NV03_PGRAPH_NSOURCE_ILLEGAL_MTHD, "ILLEGAL_MTHD" },
1240 { NV03_PGRAPH_NSOURCE_DMA_R_PROTECTION, "DMA_R_PROTECTION" },
1241 { NV03_PGRAPH_NSOURCE_DMA_W_PROTECTION, "DMA_W_PROTECTION" },
1242 { NV03_PGRAPH_NSOURCE_FORMAT_EXCEPTION, "FORMAT_EXCEPTION" },
1243 { NV03_PGRAPH_NSOURCE_PATCH_EXCEPTION, "PATCH_EXCEPTION" },
1244 { NV03_PGRAPH_NSOURCE_STATE_INVALID, "STATE_INVALID" },
1245 { NV03_PGRAPH_NSOURCE_DOUBLE_NOTIFY, "DOUBLE_NOTIFY" },
1246 { NV03_PGRAPH_NSOURCE_NOTIFY_IN_USE, "NOTIFY_IN_USE" },
1247 { NV03_PGRAPH_NSOURCE_METHOD_CNT, "METHOD_CNT" },
1248 { NV03_PGRAPH_NSOURCE_BFR_NOTIFICATION, "BFR_NOTIFICATION" },
1249 { NV03_PGRAPH_NSOURCE_DMA_VTX_PROTECTION, "DMA_VTX_PROTECTION" },
1250 { NV03_PGRAPH_NSOURCE_DMA_WIDTH_A, "DMA_WIDTH_A" },
1251 { NV03_PGRAPH_NSOURCE_DMA_WIDTH_B, "DMA_WIDTH_B" },
1256 nv04_gr_intr(struct nouveau_subdev *subdev)
1258 struct nv04_gr_priv *priv = (void *)subdev;
1259 struct nv04_gr_chan *chan = NULL;
1260 struct nouveau_namedb *namedb = NULL;
1261 struct nouveau_handle *handle = NULL;
1262 u32 stat = nv_rd32(priv, NV03_PGRAPH_INTR);
1263 u32 nsource = nv_rd32(priv, NV03_PGRAPH_NSOURCE);
1264 u32 nstatus = nv_rd32(priv, NV03_PGRAPH_NSTATUS);
1265 u32 addr = nv_rd32(priv, NV04_PGRAPH_TRAPPED_ADDR);
1266 u32 chid = (addr & 0x0f000000) >> 24;
1267 u32 subc = (addr & 0x0000e000) >> 13;
1268 u32 mthd = (addr & 0x00001ffc);
1269 u32 data = nv_rd32(priv, NV04_PGRAPH_TRAPPED_DATA);
1270 u32 class = nv_rd32(priv, 0x400180 + subc * 4) & 0xff;
1271 u32 inst = (nv_rd32(priv, 0x40016c) & 0xffff) << 4;
1273 unsigned long flags;
1275 spin_lock_irqsave(&priv->lock, flags);
1276 chan = priv->chan[chid];
1278 namedb = (void *)nv_pclass(nv_object(chan), NV_NAMEDB_CLASS);
1279 spin_unlock_irqrestore(&priv->lock, flags);
1281 if (stat & NV_PGRAPH_INTR_NOTIFY) {
1282 if (chan && (nsource & NV03_PGRAPH_NSOURCE_ILLEGAL_MTHD)) {
1283 handle = nouveau_namedb_get_vinst(namedb, inst);
1284 if (handle && !nv_call(handle->object, mthd, data))
1285 show &= ~NV_PGRAPH_INTR_NOTIFY;
1289 if (stat & NV_PGRAPH_INTR_CONTEXT_SWITCH) {
1290 nv_wr32(priv, NV03_PGRAPH_INTR, NV_PGRAPH_INTR_CONTEXT_SWITCH);
1291 stat &= ~NV_PGRAPH_INTR_CONTEXT_SWITCH;
1292 show &= ~NV_PGRAPH_INTR_CONTEXT_SWITCH;
1293 nv04_gr_context_switch(priv);
1296 nv_wr32(priv, NV03_PGRAPH_INTR, stat);
1297 nv_wr32(priv, NV04_PGRAPH_FIFO, 0x00000001);
1300 nv_error(priv, "%s", "");
1301 nouveau_bitfield_print(nv04_gr_intr_name, show);
1302 pr_cont(" nsource:");
1303 nouveau_bitfield_print(nv04_gr_nsource, nsource);
1304 pr_cont(" nstatus:");
1305 nouveau_bitfield_print(nv04_gr_nstatus, nstatus);
1308 "ch %d [%s] subc %d class 0x%04x mthd 0x%04x data 0x%08x\n",
1309 chid, nouveau_client_name(chan), subc, class, mthd,
1313 nouveau_namedb_put(handle);
1317 nv04_gr_ctor(struct nouveau_object *parent, struct nouveau_object *engine,
1318 struct nouveau_oclass *oclass, void *data, u32 size,
1319 struct nouveau_object **pobject)
1321 struct nv04_gr_priv *priv;
1324 ret = nouveau_gr_create(parent, engine, oclass, true, &priv);
1325 *pobject = nv_object(priv);
1329 nv_subdev(priv)->unit = 0x00001000;
1330 nv_subdev(priv)->intr = nv04_gr_intr;
1331 nv_engine(priv)->cclass = &nv04_gr_cclass;
1332 nv_engine(priv)->sclass = nv04_gr_sclass;
1333 spin_lock_init(&priv->lock);
1338 nv04_gr_init(struct nouveau_object *object)
1340 struct nouveau_engine *engine = nv_engine(object);
1341 struct nv04_gr_priv *priv = (void *)engine;
1344 ret = nouveau_gr_init(&priv->base);
1348 /* Enable PGRAPH interrupts */
1349 nv_wr32(priv, NV03_PGRAPH_INTR, 0xFFFFFFFF);
1350 nv_wr32(priv, NV03_PGRAPH_INTR_EN, 0xFFFFFFFF);
1352 nv_wr32(priv, NV04_PGRAPH_VALID1, 0);
1353 nv_wr32(priv, NV04_PGRAPH_VALID2, 0);
1354 /*nv_wr32(priv, NV04_PGRAPH_DEBUG_0, 0x000001FF);
1355 nv_wr32(priv, NV04_PGRAPH_DEBUG_0, 0x001FFFFF);*/
1356 nv_wr32(priv, NV04_PGRAPH_DEBUG_0, 0x1231c000);
1357 /*1231C000 blob, 001 haiku*/
1358 /*V_WRITE(NV04_PGRAPH_DEBUG_1, 0xf2d91100);*/
1359 nv_wr32(priv, NV04_PGRAPH_DEBUG_1, 0x72111100);
1360 /*0x72111100 blob , 01 haiku*/
1361 /*nv_wr32(priv, NV04_PGRAPH_DEBUG_2, 0x11d5f870);*/
1362 nv_wr32(priv, NV04_PGRAPH_DEBUG_2, 0x11d5f071);
1365 /*nv_wr32(priv, NV04_PGRAPH_DEBUG_3, 0xfad4ff31);*/
1366 nv_wr32(priv, NV04_PGRAPH_DEBUG_3, 0xf0d4ff31);
1367 /*haiku and blob 10d4*/
1369 nv_wr32(priv, NV04_PGRAPH_STATE , 0xFFFFFFFF);
1370 nv_wr32(priv, NV04_PGRAPH_CTX_CONTROL , 0x10000100);
1371 nv_mask(priv, NV04_PGRAPH_CTX_USER, 0xff000000, 0x0f000000);
1373 /* These don't belong here, they're part of a per-channel context */
1374 nv_wr32(priv, NV04_PGRAPH_PATTERN_SHAPE, 0x00000000);
1375 nv_wr32(priv, NV04_PGRAPH_BETA_AND , 0xFFFFFFFF);
1379 struct nouveau_oclass
1381 .handle = NV_ENGINE(GR, 0x04),
1382 .ofuncs = &(struct nouveau_ofuncs) {
1383 .ctor = nv04_gr_ctor,
1384 .dtor = _nouveau_gr_dtor,
1385 .init = nv04_gr_init,
1386 .fini = _nouveau_gr_fini,