summaryrefslogtreecommitdiff
path: root/shared-core/nv10_fifo.c
blob: 6d50b6ca9e6c3101a865eb8445c7b39ccc60f85c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
 * Copyright (C) 2007 Ben Skeggs.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */

#include "drmP.h"
#include "drm.h"
#include "nouveau_drv.h"


#define RAMFC_WR(offset,val) INSTANCE_WR(chan->ramfc->gpuobj, \
					 NV10_RAMFC_##offset/4, (val))
#define RAMFC_RD(offset)     INSTANCE_RD(chan->ramfc->gpuobj, \
					 NV10_RAMFC_##offset/4)
#define NV10_RAMFC(c) (dev_priv->ramfc_offset + ((c) * NV10_RAMFC__SIZE))
#define NV10_RAMFC__SIZE ((dev_priv->chipset) >= 0x17 ? 64 : 32)

int
nv10_fifo_channel_id(struct drm_device *dev)
{
	struct drm_nouveau_private *dev_priv = dev->dev_private;

	return (NV_READ(NV03_PFIFO_CACHE1_PUSH1) &
			NV10_PFIFO_CACHE1_PUSH1_CHID_MASK);
}

int
nv10_fifo_create_context(struct nouveau_channel *chan)
{
	struct drm_device *dev = chan->dev;
	struct drm_nouveau_private *dev_priv = dev->dev_private;
	int ret;

	if ((ret = nouveau_gpuobj_new_fake(dev, NV10_RAMFC(chan->id), ~0,
						NV10_RAMFC__SIZE,
						NVOBJ_FLAG_ZERO_ALLOC |
						NVOBJ_FLAG_ZERO_FREE,
						NULL, &chan->ramfc)))
		return ret;

	/* Fill entries that are seen filled in dumps of nvidia driver just
	 * after channel's is put into DMA mode
	 */
	RAMFC_WR(DMA_PUT       , chan->pushbuf_base);
	RAMFC_WR(DMA_GET       , chan->pushbuf_base);
	RAMFC_WR(DMA_INSTANCE  , chan->pushbuf->instance >> 4);
	RAMFC_WR(DMA_FETCH     , NV_PFIFO_CACHE1_DMA_FETCH_TRIG_128_BYTES |
				 NV_PFIFO_CACHE1_DMA_FETCH_SIZE_128_BYTES |
				 NV_PFIFO_CACHE1_DMA_FETCH_MAX_REQS_8 |
#ifdef __BIG_ENDIAN
				 NV_PFIFO_CACHE1_BIG_ENDIAN |
#endif
				 0);

	/* enable the fifo dma operation */
	NV_WRITE(NV04_PFIFO_MODE,NV_READ(NV04_PFIFO_MODE)|(1<<chan->id));
	return 0;
}

void
nv10_fifo_destroy_context(struct nouveau_channel *chan)
{
	struct drm_device *dev = chan->dev;
	struct drm_nouveau_private *dev_priv = dev->dev_private;

	NV_WRITE(NV04_PFIFO_MODE, NV_READ(NV04_PFIFO_MODE)&~(1<<chan->id));

	nouveau_gpuobj_ref_del(dev, &chan->ramfc);
}

int
nv10_fifo_load_context(struct nouveau_channel *chan)
{
	struct drm_device *dev = chan->dev;
	struct drm_nouveau_private *dev_priv = dev->dev_private;
	uint32_t tmp;

	NV_WRITE(NV03_PFIFO_CACHE1_PUSH1,
		 NV03_PFIFO_CACHE1_PUSH1_DMA | chan->id);

	NV_WRITE(NV04_PFIFO_CACHE1_DMA_GET          , RAMFC_RD(DMA_GET));
	NV_WRITE(NV04_PFIFO_CACHE1_DMA_PUT          , RAMFC_RD(DMA_PUT));
	NV_WRITE(NV10_PFIFO_CACHE1_REF_CNT          , RAMFC_RD(REF_CNT));

	tmp = RAMFC_RD(DMA_INSTANCE);
	NV_WRITE(NV04_PFIFO_CACHE1_DMA_INSTANCE     , tmp & 0xFFFF);
	NV_WRITE(NV04_PFIFO_CACHE1_DMA_DCOUNT       , tmp >> 16);

	NV_WRITE(NV04_PFIFO_CACHE1_DMA_STATE        , RAMFC_RD(DMA_STATE));
	NV_WRITE(NV04_PFIFO_CACHE1_DMA_FETCH        , RAMFC_RD(DMA_FETCH));
	NV_WRITE(NV04_PFIFO_CACHE1_ENGINE           , RAMFC_RD(ENGINE));
	NV_WRITE(NV04_PFIFO_CACHE1_PULL1            , RAMFC_RD(PULL1_ENGINE));

	if (dev_priv->chipset >= 0x17) {
		NV_WRITE(NV10_PFIFO_CACHE1_ACQUIRE_VALUE,
			 RAMFC_RD(ACQUIRE_VALUE));
		NV_WRITE(NV10_PFIFO_CACHE1_ACQUIRE_TIMESTAMP,
			 RAMFC_RD(ACQUIRE_TIMESTAMP));
		NV_WRITE(NV10_PFIFO_CACHE1_ACQUIRE_TIMEOUT,
			 RAMFC_RD(ACQUIRE_TIMEOUT));
		NV_WRITE(NV10_PFIFO_CACHE1_SEMAPHORE,
			 RAMFC_RD(SEMAPHORE));
		NV_WRITE(NV10_PFIFO_CACHE1_DMA_SUBROUTINE,
			 RAMFC_RD(DMA_SUBROUTINE));
	}

	/* Reset NV04_PFIFO_CACHE1_DMA_CTL_AT_INFO to INVALID */
	tmp = NV_READ(NV04_PFIFO_CACHE1_DMA_CTL) & ~(1<<31);
	NV_WRITE(NV04_PFIFO_CACHE1_DMA_CTL, tmp);

	return 0;
}

int
nv10_fifo_save_context(struct nouveau_channel *chan)
{
	struct drm_device *dev = chan->dev;
	struct drm_nouveau_private *dev_priv = dev->dev_private;
	uint32_t tmp;

	RAMFC_WR(DMA_PUT          , NV_READ(NV04_PFIFO_CACHE1_DMA_PUT));
	RAMFC_WR(DMA_GET          , NV_READ(NV04_PFIFO_CACHE1_DMA_GET));
	RAMFC_WR(REF_CNT          , NV_READ(NV10_PFIFO_CACHE1_REF_CNT));

	tmp  = NV_READ(NV04_PFIFO_CACHE1_DMA_INSTANCE) & 0xFFFF;
	tmp |= (NV_READ(NV04_PFIFO_CACHE1_DMA_DCOUNT) << 16);
	RAMFC_WR(DMA_INSTANCE     , tmp);

	RAMFC_WR(DMA_STATE        , NV_READ(NV04_PFIFO_CACHE1_DMA_STATE));
	RAMFC_WR(DMA_FETCH	  , NV_READ(NV04_PFIFO_CACHE1_DMA_FETCH));
	RAMFC_WR(ENGINE           , NV_READ(NV04_PFIFO_CACHE1_ENGINE));
	RAMFC_WR(PULL1_ENGINE     , NV_READ(NV04_PFIFO_CACHE1_PULL1));

	if (dev_priv->chipset >= 0x17) {
		RAMFC_WR(ACQUIRE_VALUE,
			 NV_READ(NV10_PFIFO_CACHE1_ACQUIRE_VALUE));
		RAMFC_WR(ACQUIRE_TIMESTAMP,
			 NV_READ(NV10_PFIFO_CACHE1_ACQUIRE_TIMESTAMP));
		RAMFC_WR(ACQUIRE_TIMEOUT,
			 NV_READ(NV10_PFIFO_CACHE1_ACQUIRE_TIMEOUT));
		RAMFC_WR(SEMAPHORE,
			 NV_READ(NV10_PFIFO_CACHE1_SEMAPHORE));
		RAMFC_WR(DMA_SUBROUTINE,
			 NV_READ(NV04_PFIFO_CACHE1_DMA_GET));
	}

	return 0;
}
span> 16); int r = req.commit_sync(); FAIL_IF(r, "initial plane setup failed"); m_current_fb = fb; } void queue_next() { if (m_queued_fb) return; if (s_ready_fbs.size() == 0) return; auto fb = s_ready_fbs.back(); s_ready_fbs.pop_back(); AtomicReq req(m_card); req.add(m_plane, "FB_ID", fb->id()); int r = req.commit(this); if (r) EXIT("Flip commit failed: %d\n", r); m_queued_fb = fb; } private: void handle_page_flip(uint32_t frame, double time) { if (m_queued_fb) { if (m_current_fb) s_free_fbs.insert(s_free_fbs.begin(), m_current_fb); m_current_fb = m_queued_fb; m_queued_fb = nullptr; } queue_next(); } Card& m_card; Crtc* m_crtc; Plane* m_plane; DumbFramebuffer* m_current_fb = nullptr; DumbFramebuffer* m_queued_fb = nullptr; }; class BarFlipState : private PageFlipHandlerBase { public: BarFlipState(Card& card, Crtc* crtc) : m_card(card), m_crtc(crtc) { m_plane = m_crtc->get_primary_plane(); uint32_t w = m_crtc->mode().hdisplay; uint32_t h = m_crtc->mode().vdisplay; for (unsigned i = 0; i < s_num_buffers; ++i) m_fbs[i] = new DumbFramebuffer(card, w, h, PixelFormat::XRGB8888); } ~BarFlipState() { for (unsigned i = 0; i < s_num_buffers; ++i) delete m_fbs[i]; } void start_flipping() { m_frame_num = 0; queue_next(); } private: void handle_page_flip(uint32_t frame, double time) { m_frame_num++; queue_next(); } static unsigned get_bar_pos(DumbFramebuffer* fb, unsigned frame_num) { return (frame_num * bar_speed) % (fb->width() - bar_width + 1); } void draw_bar(DumbFramebuffer* fb, unsigned frame_num) { int old_xpos = frame_num < s_num_buffers ? -1 : get_bar_pos(fb, frame_num - s_num_buffers); int new_xpos = get_bar_pos(fb, frame_num); draw_color_bar(*fb, old_xpos, new_xpos, bar_width); draw_text(*fb, fb->width() / 2, 0, to_string(frame_num), RGB(255, 255, 255)); } void queue_next() { AtomicReq req(m_card); unsigned cur = m_frame_num % s_num_buffers; auto fb = m_fbs[cur]; draw_bar(fb, m_frame_num); req.add(m_plane, { { "FB_ID", fb->id() }, }); int r = req.commit(this); if (r) EXIT("Flip commit failed: %d\n", r); } static const unsigned s_num_buffers = 3; DumbFramebuffer* m_fbs[s_num_buffers]; Card& m_card; Crtc* m_crtc; Plane* m_plane; unsigned m_frame_num; static const unsigned bar_width = 20; static const unsigned bar_speed = 8; }; static const char* usage_str = "Usage: wbcap [OPTIONS]\n\n" "Options:\n" " -s, --src=CONN Source connector\n" " -d, --dst=CONN Destination connector\n" " -f, --format=4CC Format" " -h, --help Print this help\n" ; int main(int argc, char** argv) { string src_conn_name = "unknown"; string dst_conn_name = "hdmi"; PixelFormat pixfmt = PixelFormat::XRGB8888; OptionSet optionset = { Option("s|src=", [&](string s) { src_conn_name = s; }), Option("d|dst=", [&](string s) { dst_conn_name = s; }), Option("f|format=", [&](string s) { pixfmt = FourCCToPixelFormat(s); }), Option("h|help", [&]() { puts(usage_str); exit(-1); }), }; optionset.parse(argc, argv); if (optionset.params().size() > 0) { puts(usage_str); exit(-1); } VideoDevice vid("/dev/video11"); Card card; ResourceManager resman(card); auto src_conn = resman.reserve_connector(src_conn_name); auto src_crtc = resman.reserve_crtc(src_conn); uint32_t src_width = src_crtc->mode().hdisplay; uint32_t src_height = src_crtc->mode().vdisplay; printf("src %s, crtc %ux%u\n", src_conn->fullname().c_str(), src_width, src_height); auto dst_conn = resman.reserve_connector(dst_conn_name); auto dst_crtc = resman.reserve_crtc(dst_conn); auto dst_plane = resman.reserve_overlay_plane(dst_crtc, pixfmt); FAIL_IF(!dst_plane, "Plane not found"); uint32_t dst_width = min((uint32_t)dst_crtc->mode().hdisplay, src_width); uint32_t dst_height = min((uint32_t)dst_crtc->mode().vdisplay, src_height); printf("dst %s, crtc %ux%u, plane %ux%u\n", dst_conn->fullname().c_str(), dst_crtc->mode().hdisplay, dst_crtc->mode().vdisplay, dst_width, dst_height); for (int i = 0; i < CAMERA_BUF_QUEUE_SIZE; ++i) { auto fb = new DumbFramebuffer(card, src_width, src_height, pixfmt); s_fbs.push_back(fb); s_free_fbs.push_back(fb); } // get one fb for initial setup s_ready_fbs.push_back(s_free_fbs.back()); s_free_fbs.pop_back(); // This draws a moving bar to SRC display BarFlipState barflipper(card, src_crtc); barflipper.start_flipping(); // This shows the captures SRC frames on DST display WBFlipState wbflipper(card, dst_crtc, dst_plane); wbflipper.setup(0, 0, dst_width, dst_height); WBStreamer wb(vid.get_capture_streamer(), src_crtc, src_width, src_height, pixfmt); wb.start_streaming(); vector<pollfd> fds(3); fds[0].fd = 0; fds[0].events = POLLIN; fds[1].fd = wb.fd(); fds[1].events = POLLIN; fds[2].fd = card.fd(); fds[2].events = POLLIN; while (true) { int r = poll(fds.data(), fds.size(), -1); ASSERT(r > 0); if (fds[0].revents != 0) break; if (fds[1].revents) { fds[1].revents = 0; wb.Dequeue(); wbflipper.queue_next(); } if (fds[2].revents) { fds[2].revents = 0; card.call_page_flip_handlers(); wb.Queue(); } } printf("exiting...\n"); }