xref: /aosp_15_r20/external/mesa3d/src/gallium/drivers/nouveau/nv50/nv98_video_vp.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright 2011-2013 Maarten Lankhorst, Ilia Mirkin
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 shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 #include "nv50/nv98_video.h"
24 #include <sys/mman.h>
25 
26 #if NOUVEAU_VP3_DEBUG_FENCE
dump_comm_vp(struct nouveau_vp3_decoder * dec,struct comm * comm,u32 comm_seq,struct nouveau_bo * inter_bo,unsigned slice_size)27 static void dump_comm_vp(struct nouveau_vp3_decoder *dec, struct comm *comm, u32 comm_seq,
28                          struct nouveau_bo *inter_bo, unsigned slice_size)
29 {
30    unsigned i, idx = comm->pvp_cur_index & 0xf;
31    debug_printf("Status: %08x, stage: %08x\n", comm->status_vp[idx], comm->pvp_stage);
32 #if 0
33    debug_printf("Acked byte ofs: %x, bsp byte ofs: %x\n", comm->acked_byte_ofs, comm->byte_ofs);
34    debug_printf("Irq/parse indexes: %i %i\n", comm->irq_index, comm->parse_endpos_index);
35 
36    for (i = 0; i != comm->irq_index; ++i)
37       debug_printf("irq[%i] = { @ %08x -> %04x }\n", i, comm->irq_pos[i], comm->irq_470[i]);
38    for (i = 0; i != comm->parse_endpos_index; ++i)
39       debug_printf("parse_endpos[%i] = { @ %08x}\n", i, comm->parse_endpos[i]);
40 #endif
41    debug_printf("mb_y = %u\n", comm->mb_y[idx]);
42    if (comm->status_vp[idx] <= 1)
43       return;
44 
45    if ((comm->pvp_stage & 0xff) != 0xff) {
46       unsigned *map;
47       int ret = BO_MAP(nouveau_screen(dec->base.context->screen), inter_bo,
48                        NOUVEAU_BO_RD|NOUVEAU_BO_NOBLOCK, dec->client);
49       assert(ret >= 0);
50       map = inter_bo->map;
51       for (i = 0; i < comm->byte_ofs + slice_size; i += 0x10) {
52          debug_printf("%05x: %08x %08x %08x %08x\n", i, map[i/4], map[i/4+1], map[i/4+2], map[i/4+3]);
53       }
54       munmap(inter_bo->map, inter_bo->size);
55       inter_bo->map = NULL;
56    }
57    assert((comm->pvp_stage & 0xff) == 0xff);
58 }
59 #endif
60 
61 static void
nv98_decoder_kick_ref(struct nouveau_vp3_decoder * dec,struct nouveau_vp3_video_buffer * target)62 nv98_decoder_kick_ref(struct nouveau_vp3_decoder *dec, struct nouveau_vp3_video_buffer *target)
63 {
64    dec->refs[target->valid_ref].last_used = 0;
65 //   debug_printf("Unreffed %p\n", target);
66 }
67 
68 void
nv98_decoder_vp(struct nouveau_vp3_decoder * dec,union pipe_desc desc,struct nouveau_vp3_video_buffer * target,unsigned comm_seq,unsigned caps,unsigned is_ref,struct nouveau_vp3_video_buffer * refs[16])69 nv98_decoder_vp(struct nouveau_vp3_decoder *dec, union pipe_desc desc,
70                 struct nouveau_vp3_video_buffer *target, unsigned comm_seq,
71                 unsigned caps, unsigned is_ref,
72                 struct nouveau_vp3_video_buffer *refs[16])
73 {
74    struct nouveau_pushbuf *push = dec->pushbuf[1];
75    uint32_t bsp_addr, comm_addr, inter_addr, ucode_addr, pic_addr[17], last_addr, null_addr;
76    uint32_t slice_size, bucket_size, ring_size, i;
77    enum pipe_video_format codec = u_reduce_video_profile(dec->base.profile);
78    struct nouveau_bo *bsp_bo = dec->bsp_bo[comm_seq % NOUVEAU_VP3_VIDEO_QDEPTH];
79    struct nouveau_bo *inter_bo = dec->inter_bo[comm_seq & 1];
80    u32 codec_extra = 0;
81    struct nouveau_pushbuf_refn bo_refs[] = {
82       { inter_bo, NOUVEAU_BO_WR | NOUVEAU_BO_VRAM },
83       { dec->ref_bo, NOUVEAU_BO_WR | NOUVEAU_BO_VRAM },
84       { bsp_bo, NOUVEAU_BO_RD | NOUVEAU_BO_VRAM },
85 #if NOUVEAU_VP3_DEBUG_FENCE
86       { dec->fence_bo, NOUVEAU_BO_WR | NOUVEAU_BO_GART },
87 #endif
88       { dec->fw_bo, NOUVEAU_BO_RD | NOUVEAU_BO_VRAM },
89    };
90    int num_refs = ARRAY_SIZE(bo_refs) - !dec->fw_bo;
91 
92    if (codec == PIPE_VIDEO_FORMAT_MPEG4_AVC) {
93       nouveau_vp3_inter_sizes(dec, desc.h264->slice_count, &slice_size, &bucket_size, &ring_size);
94       codec_extra += 2;
95    } else
96       nouveau_vp3_inter_sizes(dec, 1, &slice_size, &bucket_size, &ring_size);
97 
98    if (dec->base.max_references > 2)
99       codec_extra += 1 + (dec->base.max_references - 2);
100 
101    pic_addr[16] = nouveau_vp3_video_addr(dec, target) >> 8;
102    last_addr = null_addr = nouveau_vp3_video_addr(dec, NULL) >> 8;
103 
104    for (i = 0; i < dec->base.max_references; ++i) {
105       if (!refs[i])
106          pic_addr[i] = last_addr;
107       else if (dec->refs[refs[i]->valid_ref].vidbuf == refs[i])
108          last_addr = pic_addr[i] = nouveau_vp3_video_addr(dec, refs[i]) >> 8;
109       else
110          pic_addr[i] = null_addr;
111    }
112    if (!is_ref && (dec->refs[target->valid_ref].decoded_top && dec->refs[target->valid_ref].decoded_bottom))
113       nv98_decoder_kick_ref(dec, target);
114 
115    PUSH_SPACE_EX(push, 32 + codec_extra, num_refs, 0);
116 
117    PUSH_REFN(push, bo_refs, num_refs);
118 
119    bsp_addr = bsp_bo->offset >> 8;
120 #if NOUVEAU_VP3_DEBUG_FENCE
121    comm_addr = (dec->fence_bo->offset + COMM_OFFSET)>>8;
122 #else
123    comm_addr = bsp_addr + (COMM_OFFSET>>8);
124 #endif
125    inter_addr = inter_bo->offset >> 8;
126    if (dec->fw_bo)
127       ucode_addr = dec->fw_bo->offset >> 8;
128    else
129       ucode_addr = 0;
130 
131    BEGIN_NV04(push, SUBC_VP(0x700), 7);
132    PUSH_DATA (push, caps); // 700
133    PUSH_DATA (push, comm_seq); // 704
134    PUSH_DATA (push, 0); // 708 fuc targets, ignored for nv98
135    PUSH_DATA (push, dec->fw_sizes); // 70c
136    PUSH_DATA (push, bsp_addr+(VP_OFFSET>>8)); // 710 picparm_addr
137    PUSH_DATA (push, inter_addr); // 714 inter_parm
138    PUSH_DATA (push, inter_addr + slice_size + bucket_size); // 718 inter_data_ofs
139 
140    if (bucket_size) {
141       uint64_t tmpimg_addr = dec->ref_bo->offset + dec->ref_stride * (dec->base.max_references+2);
142 
143       BEGIN_NV04(push, SUBC_VP(0x71c), 2);
144       PUSH_DATA (push, tmpimg_addr >> 8); // 71c
145       PUSH_DATA (push, inter_addr + slice_size); // 720 bucket_ofs
146    }
147 
148    BEGIN_NV04(push, SUBC_VP(0x724), 5);
149    PUSH_DATA (push, comm_addr); // 724
150    PUSH_DATA (push, ucode_addr); // 728
151    PUSH_DATA (push, pic_addr[16]); // 734
152    PUSH_DATA (push, pic_addr[0]); // 72c
153    PUSH_DATA (push, pic_addr[1]); // 730
154 
155    if (dec->base.max_references > 2) {
156       int i;
157 
158       BEGIN_NV04(push, SUBC_VP(0x400), dec->base.max_references - 2);
159       for (i = 2; i < dec->base.max_references; ++i) {
160          assert(0x400 + (i - 2) * 4 < 0x438);
161          PUSH_DATA (push, pic_addr[i]);
162       }
163    }
164 
165    if (codec == PIPE_VIDEO_FORMAT_MPEG4_AVC) {
166       BEGIN_NV04(push, SUBC_VP(0x438), 1);
167       PUSH_DATA (push, desc.h264->slice_count);
168    }
169 
170    //debug_printf("Decoding %08lx with %08lx and %08lx\n", pic_addr[16], pic_addr[0], pic_addr[1]);
171 
172 #if NOUVEAU_VP3_DEBUG_FENCE
173    BEGIN_NV04(push, SUBC_VP(0x240), 3);
174    PUSH_DATAh(push, (dec->fence_bo->offset + 0x10));
175    PUSH_DATA (push, (dec->fence_bo->offset + 0x10));
176    PUSH_DATA (push, dec->fence_seq);
177 
178    BEGIN_NV04(push, SUBC_VP(0x300), 1);
179    PUSH_DATA (push, 1);
180    PUSH_KICK(push);
181 
182    {
183       unsigned spin = 0;
184       do {
185          usleep(100);
186          if ((spin++ & 0xff) == 0xff) {
187             debug_printf("v%u: %u\n", dec->fence_seq, dec->fence_map[4]);
188             dump_comm_vp(dec, dec->comm, comm_seq, inter_bo, slice_size << 8);
189          }
190       } while (dec->fence_seq > dec->fence_map[4]);
191    }
192    dump_comm_vp(dec, dec->comm, comm_seq, inter_bo, slice_size << 8);
193 #else
194    BEGIN_NV04(push, SUBC_VP(0x300), 1);
195    PUSH_DATA (push, 0);
196    PUSH_KICK (push);
197 #endif
198 }
199