r300_cs.h revision 8cc570a48c2e8e18622027cbd76f16a746b430bc
178b599fb4cac469f4208ae3057b2a33e3e9913c6Corbin Simpson/*
278b599fb4cac469f4208ae3057b2a33e3e9913c6Corbin Simpson * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
378b599fb4cac469f4208ae3057b2a33e3e9913c6Corbin Simpson *
478b599fb4cac469f4208ae3057b2a33e3e9913c6Corbin Simpson * Permission is hereby granted, free of charge, to any person obtaining a
578b599fb4cac469f4208ae3057b2a33e3e9913c6Corbin Simpson * copy of this software and associated documentation files (the "Software"),
678b599fb4cac469f4208ae3057b2a33e3e9913c6Corbin Simpson * to deal in the Software without restriction, including without limitation
778b599fb4cac469f4208ae3057b2a33e3e9913c6Corbin Simpson * on the rights to use, copy, modify, merge, publish, distribute, sub
878b599fb4cac469f4208ae3057b2a33e3e9913c6Corbin Simpson * license, and/or sell copies of the Software, and to permit persons to whom
978b599fb4cac469f4208ae3057b2a33e3e9913c6Corbin Simpson * the Software is furnished to do so, subject to the following conditions:
1078b599fb4cac469f4208ae3057b2a33e3e9913c6Corbin Simpson *
1178b599fb4cac469f4208ae3057b2a33e3e9913c6Corbin Simpson * The above copyright notice and this permission notice (including the next
1278b599fb4cac469f4208ae3057b2a33e3e9913c6Corbin Simpson * paragraph) shall be included in all copies or substantial portions of the
1378b599fb4cac469f4208ae3057b2a33e3e9913c6Corbin Simpson * Software.
1478b599fb4cac469f4208ae3057b2a33e3e9913c6Corbin Simpson *
1578b599fb4cac469f4208ae3057b2a33e3e9913c6Corbin Simpson * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1678b599fb4cac469f4208ae3057b2a33e3e9913c6Corbin Simpson * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1778b599fb4cac469f4208ae3057b2a33e3e9913c6Corbin Simpson * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
1878b599fb4cac469f4208ae3057b2a33e3e9913c6Corbin Simpson * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
1978b599fb4cac469f4208ae3057b2a33e3e9913c6Corbin Simpson * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
2078b599fb4cac469f4208ae3057b2a33e3e9913c6Corbin Simpson * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
2178b599fb4cac469f4208ae3057b2a33e3e9913c6Corbin Simpson * USE OR OTHER DEALINGS IN THE SOFTWARE. */
2278b599fb4cac469f4208ae3057b2a33e3e9913c6Corbin Simpson
2378b599fb4cac469f4208ae3057b2a33e3e9913c6Corbin Simpson#ifndef R300_CS_H
2478b599fb4cac469f4208ae3057b2a33e3e9913c6Corbin Simpson#define R300_CS_H
2578b599fb4cac469f4208ae3057b2a33e3e9913c6Corbin Simpson
26f176c94e49a50b05b3af9f748a67e2ebd2e1b8fdCorbin Simpson#include "util/u_math.h"
27f176c94e49a50b05b3af9f748a67e2ebd2e1b8fdCorbin Simpson
2874288078eab1971cc6ce3ae00fa55eb917b5826aCorbin Simpson#include "r300_reg.h"
294f77b0103d5f150845300ee8bddcef20d11a9820Corbin Simpson
304f77b0103d5f150845300ee8bddcef20d11a9820Corbin Simpson#include "radeon_winsys.h"
31fbeeb6675733f5b2da36d40b0142dadf8cc953b4Corbin Simpson
3278b599fb4cac469f4208ae3057b2a33e3e9913c6Corbin Simpson/* Yes, I know macros are ugly. However, they are much prettier than the code
3378b599fb4cac469f4208ae3057b2a33e3e9913c6Corbin Simpson * that they neatly hide away, and don't have the cost of function setup,so
3478b599fb4cac469f4208ae3057b2a33e3e9913c6Corbin Simpson * we're going to use them. */
3578b599fb4cac469f4208ae3057b2a33e3e9913c6Corbin Simpson
3678b599fb4cac469f4208ae3057b2a33e3e9913c6Corbin Simpson#define MAX_CS_SIZE 64 * 1024 / 4
3778b599fb4cac469f4208ae3057b2a33e3e9913c6Corbin Simpson
38c7dfffc5d5078e3cf1c28c230177cbbb43b91131Maciej Cencora#define VERY_VERBOSE_CS 1
39c7dfffc5d5078e3cf1c28c230177cbbb43b91131Maciej Cencora#define VERY_VERBOSE_REGISTERS 1
40ffbe28d25d755bc51d9f865ac176a110f6f8f5e0Corbin Simpson
41fbeeb6675733f5b2da36d40b0142dadf8cc953b4Corbin Simpson/* XXX stolen from radeon_drm.h */
42fbeeb6675733f5b2da36d40b0142dadf8cc953b4Corbin Simpson#define RADEON_GEM_DOMAIN_CPU  0x1
43fbeeb6675733f5b2da36d40b0142dadf8cc953b4Corbin Simpson#define RADEON_GEM_DOMAIN_GTT  0x2
44fbeeb6675733f5b2da36d40b0142dadf8cc953b4Corbin Simpson#define RADEON_GEM_DOMAIN_VRAM 0x4
45fbeeb6675733f5b2da36d40b0142dadf8cc953b4Corbin Simpson
467961974fc28257b293961d35f15c0ce7a85f2669Corbin Simpson/* XXX stolen from radeon_reg.h */
477961974fc28257b293961d35f15c0ce7a85f2669Corbin Simpson#define RADEON_CP_PACKET0 0x0
487961974fc28257b293961d35f15c0ce7a85f2669Corbin Simpson
4978b599fb4cac469f4208ae3057b2a33e3e9913c6Corbin Simpson#define CP_PACKET0(register, count) \
5078b599fb4cac469f4208ae3057b2a33e3e9913c6Corbin Simpson    (RADEON_CP_PACKET0 | ((count) << 16) | ((register) >> 2))
5178b599fb4cac469f4208ae3057b2a33e3e9913c6Corbin Simpson
5278b599fb4cac469f4208ae3057b2a33e3e9913c6Corbin Simpson#define CS_LOCALS(context) \
534b01e6f614052e48971f2b2ff474fb66afc4f752Nicolai Hähnle    struct r300_context* const cs_context_copy = (context); \
544f77b0103d5f150845300ee8bddcef20d11a9820Corbin Simpson    struct radeon_winsys* cs_winsys = cs_context_copy->winsys; \
5580dc1801409f9913cc37b8fc8e68c692bc8a22caCorbin Simpson    int cs_count = 0;
5678b599fb4cac469f4208ae3057b2a33e3e9913c6Corbin Simpson
57fbeeb6675733f5b2da36d40b0142dadf8cc953b4Corbin Simpson#define CHECK_CS(size) \
587679447b5835fd73ab44b3d77b12a034c95af5c5Marek Olšák    assert(cs_winsys->check_cs(cs_winsys, (size)))
5978b599fb4cac469f4208ae3057b2a33e3e9913c6Corbin Simpson
6078b599fb4cac469f4208ae3057b2a33e3e9913c6Corbin Simpson#define BEGIN_CS(size) do { \
6178b599fb4cac469f4208ae3057b2a33e3e9913c6Corbin Simpson    CHECK_CS(size); \
6290bfff0a295ce28143ecde98ed91eb6d8cfba23cCorbin Simpson    if (VERY_VERBOSE_CS) { \
634b01e6f614052e48971f2b2ff474fb66afc4f752Nicolai Hähnle        DBG(cs_context_copy, DBG_CS, "r300: BEGIN_CS, count %d, in %s (%s:%d)\n", \
6490bfff0a295ce28143ecde98ed91eb6d8cfba23cCorbin Simpson                size, __FUNCTION__, __FILE__, __LINE__); \
6590bfff0a295ce28143ecde98ed91eb6d8cfba23cCorbin Simpson    } \
66d7f4ac9f34a72efe53a1a140557f1822afbadf16Corbin Simpson    cs_winsys->begin_cs(cs_winsys, (size), \
67d7f4ac9f34a72efe53a1a140557f1822afbadf16Corbin Simpson            __FILE__, __FUNCTION__, __LINE__); \
6884ec4d6bedf33bf03ff7a778632eef7b209944cbCorbin Simpson    cs_count = size; \
6978b599fb4cac469f4208ae3057b2a33e3e9913c6Corbin Simpson} while (0)
7078b599fb4cac469f4208ae3057b2a33e3e9913c6Corbin Simpson
71412cf4d38be628200982208b7f93bb17530bb6dbCorbin Simpson#define OUT_CS(value) do { \
72a6eb593072298d60286f49a09e6d3a849b684dfbMaciej Cencora    if (VERY_VERBOSE_CS || VERY_VERBOSE_REGISTERS) { \
73a6eb593072298d60286f49a09e6d3a849b684dfbMaciej Cencora        DBG(cs_context_copy, DBG_CS, "r300: writing %08x\n", value); \
74a6eb593072298d60286f49a09e6d3a849b684dfbMaciej Cencora    } \
75d7f4ac9f34a72efe53a1a140557f1822afbadf16Corbin Simpson    cs_winsys->write_cs_dword(cs_winsys, (value)); \
7680dc1801409f9913cc37b8fc8e68c692bc8a22caCorbin Simpson    cs_count--; \
77412cf4d38be628200982208b7f93bb17530bb6dbCorbin Simpson} while (0)
7878b599fb4cac469f4208ae3057b2a33e3e9913c6Corbin Simpson
79412cf4d38be628200982208b7f93bb17530bb6dbCorbin Simpson#define OUT_CS_32F(value) do { \
80a6eb593072298d60286f49a09e6d3a849b684dfbMaciej Cencora    if (VERY_VERBOSE_CS || VERY_VERBOSE_REGISTERS) { \
81a6eb593072298d60286f49a09e6d3a849b684dfbMaciej Cencora        DBG(cs_context_copy, DBG_CS, "r300: writing %f\n", value); \
82a6eb593072298d60286f49a09e6d3a849b684dfbMaciej Cencora    } \
83d7f4ac9f34a72efe53a1a140557f1822afbadf16Corbin Simpson    cs_winsys->write_cs_dword(cs_winsys, fui(value)); \
8480dc1801409f9913cc37b8fc8e68c692bc8a22caCorbin Simpson    cs_count--; \
85412cf4d38be628200982208b7f93bb17530bb6dbCorbin Simpson} while (0)
8622877265f4fdf66c75df391d6de95bd5c1584ea3Corbin Simpson
8778b599fb4cac469f4208ae3057b2a33e3e9913c6Corbin Simpson#define OUT_CS_REG(register, value) do { \
88ffbe28d25d755bc51d9f865ac176a110f6f8f5e0Corbin Simpson    if (VERY_VERBOSE_REGISTERS) \
894b01e6f614052e48971f2b2ff474fb66afc4f752Nicolai Hähnle        DBG(cs_context_copy, DBG_CS, "r300: writing 0x%08X to register 0x%04X\n", \
90ffbe28d25d755bc51d9f865ac176a110f6f8f5e0Corbin Simpson            value, register); \
91c199f330322921e01c8c30e3ea69a2a5291ae8eeCorbin Simpson    assert(register); \
92a6eb593072298d60286f49a09e6d3a849b684dfbMaciej Cencora    cs_winsys->write_cs_dword(cs_winsys, CP_PACKET0(register, 0)); \
93a6eb593072298d60286f49a09e6d3a849b684dfbMaciej Cencora    cs_winsys->write_cs_dword(cs_winsys, value); \
94a6eb593072298d60286f49a09e6d3a849b684dfbMaciej Cencora    cs_count -= 2; \
95412cf4d38be628200982208b7f93bb17530bb6dbCorbin Simpson} while (0)
9678b599fb4cac469f4208ae3057b2a33e3e9913c6Corbin Simpson
9722877265f4fdf66c75df391d6de95bd5c1584ea3Corbin Simpson/* Note: This expects count to be the number of registers,
9822877265f4fdf66c75df391d6de95bd5c1584ea3Corbin Simpson * not the actual packet0 count! */
9922877265f4fdf66c75df391d6de95bd5c1584ea3Corbin Simpson#define OUT_CS_REG_SEQ(register, count) do { \
100ffbe28d25d755bc51d9f865ac176a110f6f8f5e0Corbin Simpson    if (VERY_VERBOSE_REGISTERS) \
1014b01e6f614052e48971f2b2ff474fb66afc4f752Nicolai Hähnle        DBG(cs_context_copy, DBG_CS, "r300: writing register sequence of %d to 0x%04X\n", \
102ffbe28d25d755bc51d9f865ac176a110f6f8f5e0Corbin Simpson            count, register); \
103c199f330322921e01c8c30e3ea69a2a5291ae8eeCorbin Simpson    assert(register); \
104a6eb593072298d60286f49a09e6d3a849b684dfbMaciej Cencora    cs_winsys->write_cs_dword(cs_winsys, CP_PACKET0((register), ((count) - 1))); \
105a6eb593072298d60286f49a09e6d3a849b684dfbMaciej Cencora    cs_count--; \
10622877265f4fdf66c75df391d6de95bd5c1584ea3Corbin Simpson} while (0)
10722877265f4fdf66c75df391d6de95bd5c1584ea3Corbin Simpson
10878b599fb4cac469f4208ae3057b2a33e3e9913c6Corbin Simpson#define OUT_CS_RELOC(bo, offset, rd, wd, flags) do { \
1094b01e6f614052e48971f2b2ff474fb66afc4f752Nicolai Hähnle    DBG(cs_context_copy, DBG_CS, "r300: writing relocation for buffer %p, offset %d, " \
1104550423211063010a2fa482037d8233bb80e3773Corbin Simpson            "domains (%d, %d, %d)\n", \
1114550423211063010a2fa482037d8233bb80e3773Corbin Simpson        bo, offset, rd, wd, flags); \
112c199f330322921e01c8c30e3ea69a2a5291ae8eeCorbin Simpson    assert(bo); \
113a6eb593072298d60286f49a09e6d3a849b684dfbMaciej Cencora    cs_winsys->write_cs_dword(cs_winsys, offset); \
114d7f4ac9f34a72efe53a1a140557f1822afbadf16Corbin Simpson    cs_winsys->write_cs_reloc(cs_winsys, bo, rd, wd, flags); \
115a6eb593072298d60286f49a09e6d3a849b684dfbMaciej Cencora    cs_count -= 3; \
11678b599fb4cac469f4208ae3057b2a33e3e9913c6Corbin Simpson} while (0)
11778b599fb4cac469f4208ae3057b2a33e3e9913c6Corbin Simpson
1188cc570a48c2e8e18622027cbd76f16a746b430bcMarek Olšák#define OUT_CS_RELOC_NO_OFFSET(bo, rd, wd, flags) do { \
1198cc570a48c2e8e18622027cbd76f16a746b430bcMarek Olšák    DBG(cs_context_copy, DBG_CS, "r300: writing relocation for buffer %p, " \
1208cc570a48c2e8e18622027cbd76f16a746b430bcMarek Olšák            "domains (%d, %d, %d)\n", \
1218cc570a48c2e8e18622027cbd76f16a746b430bcMarek Olšák        bo, rd, wd, flags); \
1228cc570a48c2e8e18622027cbd76f16a746b430bcMarek Olšák    assert(bo); \
1238cc570a48c2e8e18622027cbd76f16a746b430bcMarek Olšák    cs_winsys->write_cs_reloc(cs_winsys, bo, rd, wd, flags); \
1248cc570a48c2e8e18622027cbd76f16a746b430bcMarek Olšák    cs_count -= 2; \
1258cc570a48c2e8e18622027cbd76f16a746b430bcMarek Olšák} while (0)
1268cc570a48c2e8e18622027cbd76f16a746b430bcMarek Olšák
127902b1be0ea2a7fea27cea26915db5977f9a8f76cCorbin Simpson#define END_CS do { \
12890bfff0a295ce28143ecde98ed91eb6d8cfba23cCorbin Simpson    if (VERY_VERBOSE_CS) { \
1294b01e6f614052e48971f2b2ff474fb66afc4f752Nicolai Hähnle        DBG(cs_context_copy, DBG_CS, "r300: END_CS in %s (%s:%d)\n", __FUNCTION__, \
13090bfff0a295ce28143ecde98ed91eb6d8cfba23cCorbin Simpson                __FILE__, __LINE__); \
13190bfff0a295ce28143ecde98ed91eb6d8cfba23cCorbin Simpson    } \
1322cb90c8e805d010ba4594264dd9edbbb7f95513aCorbin Simpson    if (cs_count != 0) \
1332cb90c8e805d010ba4594264dd9edbbb7f95513aCorbin Simpson        debug_printf("r300: Warning: cs_count off by %d\n", cs_count); \
134d7f4ac9f34a72efe53a1a140557f1822afbadf16Corbin Simpson    cs_winsys->end_cs(cs_winsys, __FILE__, __FUNCTION__, __LINE__); \
135902b1be0ea2a7fea27cea26915db5977f9a8f76cCorbin Simpson} while (0)
13678b599fb4cac469f4208ae3057b2a33e3e9913c6Corbin Simpson
137902b1be0ea2a7fea27cea26915db5977f9a8f76cCorbin Simpson#define FLUSH_CS do { \
13890bfff0a295ce28143ecde98ed91eb6d8cfba23cCorbin Simpson    if (VERY_VERBOSE_CS) { \
1394b01e6f614052e48971f2b2ff474fb66afc4f752Nicolai Hähnle        DBG(cs_context_copy, DBG_CS, "r300: FLUSH_CS in %s (%s:%d)\n\n", __FUNCTION__, \
14090bfff0a295ce28143ecde98ed91eb6d8cfba23cCorbin Simpson                __FILE__, __LINE__); \
14190bfff0a295ce28143ecde98ed91eb6d8cfba23cCorbin Simpson    } \
142d7f4ac9f34a72efe53a1a140557f1822afbadf16Corbin Simpson    cs_winsys->flush_cs(cs_winsys); \
143902b1be0ea2a7fea27cea26915db5977f9a8f76cCorbin Simpson} while (0)
144432ab001d042b816b5892398064e5735d0293955Corbin Simpson
145edfaa686091a4f6238b8f315a475d90ff2c2f5f5Corbin Simpson#define RADEON_ONE_REG_WR        (1 << 15)
146edfaa686091a4f6238b8f315a475d90ff2c2f5f5Corbin Simpson
147edfaa686091a4f6238b8f315a475d90ff2c2f5f5Corbin Simpson#define OUT_CS_ONE_REG(register, count) do { \
148edfaa686091a4f6238b8f315a475d90ff2c2f5f5Corbin Simpson    if (VERY_VERBOSE_REGISTERS) \
1494b01e6f614052e48971f2b2ff474fb66afc4f752Nicolai Hähnle        DBG(cs_context_copy, DBG_CS, "r300: writing data sequence of %d to 0x%04X\n", \
150edfaa686091a4f6238b8f315a475d90ff2c2f5f5Corbin Simpson            count, register); \
151edfaa686091a4f6238b8f315a475d90ff2c2f5f5Corbin Simpson    assert(register); \
152a6eb593072298d60286f49a09e6d3a849b684dfbMaciej Cencora    cs_winsys->write_cs_dword(cs_winsys, CP_PACKET0((register), ((count) - 1)) | RADEON_ONE_REG_WR); \
153a6eb593072298d60286f49a09e6d3a849b684dfbMaciej Cencora    cs_count--; \
154edfaa686091a4f6238b8f315a475d90ff2c2f5f5Corbin Simpson} while (0)
155edfaa686091a4f6238b8f315a475d90ff2c2f5f5Corbin Simpson
156edfaa686091a4f6238b8f315a475d90ff2c2f5f5Corbin Simpson#define CP_PACKET3(op, count) \
157edfaa686091a4f6238b8f315a475d90ff2c2f5f5Corbin Simpson    (RADEON_CP_PACKET3 | (op) | ((count) << 16))
158edfaa686091a4f6238b8f315a475d90ff2c2f5f5Corbin Simpson
159f411a66c0679c1aa7a9ee3d1eb633a8cbf3ef5f2Corbin Simpson#define OUT_CS_PKT3(op, count) do { \
160a6eb593072298d60286f49a09e6d3a849b684dfbMaciej Cencora    cs_winsys->write_cs_dword(cs_winsys, CP_PACKET3(op, count)); \
161a6eb593072298d60286f49a09e6d3a849b684dfbMaciej Cencora    cs_count--; \
162edfaa686091a4f6238b8f315a475d90ff2c2f5f5Corbin Simpson} while (0)
16329a4f5493529042d1068a7d35da1e7f542474503Corbin Simpson
1648648c2685870174cf620ef15de70ef030a8d5a20Corbin Simpson#define OUT_CS_INDEX_RELOC(bo, offset, count, rd, wd, flags) do { \
1654b01e6f614052e48971f2b2ff474fb66afc4f752Nicolai Hähnle    DBG(cs_context_copy, DBG_CS, "r300: writing relocation for index buffer %p," \
1668648c2685870174cf620ef15de70ef030a8d5a20Corbin Simpson            "offset %d\n", bo, offset); \
1678648c2685870174cf620ef15de70ef030a8d5a20Corbin Simpson    assert(bo); \
168a6eb593072298d60286f49a09e6d3a849b684dfbMaciej Cencora    cs_winsys->write_cs_dword(cs_winsys, offset); \
169a6eb593072298d60286f49a09e6d3a849b684dfbMaciej Cencora    cs_winsys->write_cs_dword(cs_winsys, count); \
170d7f4ac9f34a72efe53a1a140557f1822afbadf16Corbin Simpson    cs_winsys->write_cs_reloc(cs_winsys, bo, rd, wd, flags); \
171a6eb593072298d60286f49a09e6d3a849b684dfbMaciej Cencora    cs_count -= 4; \
1728648c2685870174cf620ef15de70ef030a8d5a20Corbin Simpson} while (0)
1738648c2685870174cf620ef15de70ef030a8d5a20Corbin Simpson
17422877265f4fdf66c75df391d6de95bd5c1584ea3Corbin Simpson#endif /* R300_CS_H */
175