1bf215546Sopenharmony_ci/* -*- mesa-c++  -*-
2bf215546Sopenharmony_ci *
3bf215546Sopenharmony_ci * Copyright (c) 2019 Collabora LTD
4bf215546Sopenharmony_ci *
5bf215546Sopenharmony_ci * Author: Gert Wollny <gert.wollny@collabora.com>
6bf215546Sopenharmony_ci *
7bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
8bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
9bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
10bf215546Sopenharmony_ci * on the rights to use, copy, modify, merge, publish, distribute, sub
11bf215546Sopenharmony_ci * license, and/or sell copies of the Software, and to permit persons to whom
12bf215546Sopenharmony_ci * the Software is furnished to do so, subject to the following conditions:
13bf215546Sopenharmony_ci *
14bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
15bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
16bf215546Sopenharmony_ci * Software.
17bf215546Sopenharmony_ci *
18bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21bf215546Sopenharmony_ci * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
22bf215546Sopenharmony_ci * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23bf215546Sopenharmony_ci * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24bf215546Sopenharmony_ci * USE OR OTHER DEALINGS IN THE SOFTWARE.
25bf215546Sopenharmony_ci */
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#include "sfn_callstack.h"
28bf215546Sopenharmony_ci
29bf215546Sopenharmony_cinamespace r600 {
30bf215546Sopenharmony_ci
31bf215546Sopenharmony_ciCallStack::CallStack(r600_bytecode& bc):
32bf215546Sopenharmony_ci   m_bc(bc)
33bf215546Sopenharmony_ci{
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_ci}
36bf215546Sopenharmony_ci
37bf215546Sopenharmony_ciCallStack::~CallStack()
38bf215546Sopenharmony_ci{
39bf215546Sopenharmony_ci}
40bf215546Sopenharmony_ci
41bf215546Sopenharmony_ciint CallStack::push(unsigned type)
42bf215546Sopenharmony_ci{
43bf215546Sopenharmony_ci   switch (type) {
44bf215546Sopenharmony_ci   case FC_PUSH_VPM:
45bf215546Sopenharmony_ci      ++m_bc.stack.push;
46bf215546Sopenharmony_ci      break;
47bf215546Sopenharmony_ci   case FC_PUSH_WQM:
48bf215546Sopenharmony_ci      ++m_bc.stack.push_wqm;
49bf215546Sopenharmony_ci      break;
50bf215546Sopenharmony_ci   case FC_LOOP:
51bf215546Sopenharmony_ci      ++m_bc.stack.loop;
52bf215546Sopenharmony_ci      break;
53bf215546Sopenharmony_ci   default:
54bf215546Sopenharmony_ci      assert(0);
55bf215546Sopenharmony_ci	}
56bf215546Sopenharmony_ci
57bf215546Sopenharmony_ci   return update_max_depth(type);
58bf215546Sopenharmony_ci}
59bf215546Sopenharmony_ci
60bf215546Sopenharmony_civoid CallStack::pop(unsigned type)
61bf215546Sopenharmony_ci{
62bf215546Sopenharmony_ci   switch(type) {
63bf215546Sopenharmony_ci   case FC_PUSH_VPM:
64bf215546Sopenharmony_ci      --m_bc.stack.push;
65bf215546Sopenharmony_ci      assert(m_bc.stack.push >= 0);
66bf215546Sopenharmony_ci      break;
67bf215546Sopenharmony_ci   case FC_PUSH_WQM:
68bf215546Sopenharmony_ci      --m_bc.stack.push_wqm;
69bf215546Sopenharmony_ci      assert(m_bc.stack.push_wqm >= 0);
70bf215546Sopenharmony_ci      break;
71bf215546Sopenharmony_ci   case FC_LOOP:
72bf215546Sopenharmony_ci      --m_bc.stack.loop;
73bf215546Sopenharmony_ci      assert(m_bc.stack.loop >= 0);
74bf215546Sopenharmony_ci      break;
75bf215546Sopenharmony_ci   default:
76bf215546Sopenharmony_ci      assert(0);
77bf215546Sopenharmony_ci      break;
78bf215546Sopenharmony_ci   }
79bf215546Sopenharmony_ci}
80bf215546Sopenharmony_ci
81bf215546Sopenharmony_ciint CallStack::update_max_depth(unsigned type)
82bf215546Sopenharmony_ci{
83bf215546Sopenharmony_ci
84bf215546Sopenharmony_ci   r600_stack_info& stack = m_bc.stack;
85bf215546Sopenharmony_ci   int elements;
86bf215546Sopenharmony_ci   int entries;
87bf215546Sopenharmony_ci
88bf215546Sopenharmony_ci   int entry_size = stack.entry_size;
89bf215546Sopenharmony_ci
90bf215546Sopenharmony_ci   elements = (stack.loop + stack.push_wqm ) * entry_size;
91bf215546Sopenharmony_ci   elements += stack.push;
92bf215546Sopenharmony_ci
93bf215546Sopenharmony_ci   switch (m_bc.gfx_level) {
94bf215546Sopenharmony_ci   case R600:
95bf215546Sopenharmony_ci   case R700:
96bf215546Sopenharmony_ci     /* pre-r8xx: if any non-WQM PUSH instruction is invoked, 2 elements on
97bf215546Sopenharmony_ci      * the stack must be reserved to hold the current active/continue
98bf215546Sopenharmony_ci      * masks */
99bf215546Sopenharmony_ci     if (type == FC_PUSH_VPM || stack.push > 0) {
100bf215546Sopenharmony_ci       elements += 2;
101bf215546Sopenharmony_ci     }
102bf215546Sopenharmony_ci     break;
103bf215546Sopenharmony_ci   case CAYMAN:
104bf215546Sopenharmony_ci     /* r9xx: any stack operation on empty stack consumes 2 additional
105bf215546Sopenharmony_ci      * elements */
106bf215546Sopenharmony_ci     elements += 2;
107bf215546Sopenharmony_ci     break;
108bf215546Sopenharmony_ci   case EVERGREEN:
109bf215546Sopenharmony_ci     /* r8xx+: 2 extra elements are not always required, but one extra
110bf215546Sopenharmony_ci      * element must be added for each of the following cases:
111bf215546Sopenharmony_ci      * 1. There is an ALU_ELSE_AFTER instruction at the point of greatest
112bf215546Sopenharmony_ci      *    stack usage.
113bf215546Sopenharmony_ci      *    (Currently we don't use ALU_ELSE_AFTER.)
114bf215546Sopenharmony_ci      * 2. There are LOOP/WQM frames on the stack when any flavor of non-WQM
115bf215546Sopenharmony_ci      *    PUSH instruction executed.
116bf215546Sopenharmony_ci      *
117bf215546Sopenharmony_ci      *    NOTE: it seems we also need to reserve additional element in some
118bf215546Sopenharmony_ci      *    other cases, e.g. when we have 4 levels of PUSH_VPM in the shader,
119bf215546Sopenharmony_ci      *    then STACK_SIZE should be 2 instead of 1 */
120bf215546Sopenharmony_ci     if (type == FC_PUSH_VPM || stack.push > 0) {
121bf215546Sopenharmony_ci       elements += 1;
122bf215546Sopenharmony_ci     }
123bf215546Sopenharmony_ci     break;
124bf215546Sopenharmony_ci   default:
125bf215546Sopenharmony_ci     assert(0);
126bf215546Sopenharmony_ci     break;
127bf215546Sopenharmony_ci   }
128bf215546Sopenharmony_ci
129bf215546Sopenharmony_ci   entry_size = 4;
130bf215546Sopenharmony_ci
131bf215546Sopenharmony_ci   entries = (elements + (entry_size - 1)) / entry_size;
132bf215546Sopenharmony_ci
133bf215546Sopenharmony_ci   if (entries > stack.max_entries)
134bf215546Sopenharmony_ci      stack.max_entries = entries;
135bf215546Sopenharmony_ci
136bf215546Sopenharmony_ci   return elements;
137bf215546Sopenharmony_ci}
138bf215546Sopenharmony_ci
139bf215546Sopenharmony_ci}
140