hdlcd.cc (9939:735d73e394d3) hdlcd.cc (10565:23593fdaadcd)
1/*
2 * Copyright (c) 2010-2013 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 * Authors: Chris Emmons
38 */
39
40#include "base/vnc/vncinput.hh"
41#include "base/bitmap.hh"
42#include "base/output.hh"
43#include "base/trace.hh"
44#include "debug/HDLcd.hh"
45#include "debug/Uart.hh"
46#include "dev/arm/amba_device.hh"
47#include "dev/arm/base_gic.hh"
48#include "dev/arm/hdlcd.hh"
49#include "mem/packet.hh"
50#include "mem/packet_access.hh"
51#include "sim/system.hh"
52
53using std::vector;
54
55
56// initialize hdlcd registers
57HDLcd::HDLcd(const Params *p)
58 : AmbaDmaDevice(p), version(VERSION_RESETV),
59 int_rawstat(0), int_clear(0), int_mask(0), int_status(0),
60 fb_base(0), fb_line_length(0), fb_line_count(0), fb_line_pitch(0),
61 bus_options(BUS_OPTIONS_RESETV),
62 v_sync(0), v_back_porch(0), v_data(0), v_front_porch(0),
63 h_sync(0), h_back_porch(0), h_data(0), h_front_porch(0),
64 polarities(0), command(0), pixel_format(0),
65 red_select(0), green_select(0), blue_select(0),
66 pixelClock(p->pixel_clock), vnc(p->vnc), bmp(NULL), pic(NULL),
67 frameReadStartTime(0),
68 dmaStartAddr(0), dmaCurAddr(0), dmaMaxAddr(0), dmaPendingNum(0),
69 frameUnderrun(false), virtualDisplayBuffer(NULL), pixelBufferSize(0),
70 pixelIndex(0), doUpdateParams(false), frameUnderway(false),
71 dmaBytesInFlight(0),
72 startFrameEvent(this), endFrameEvent(this), renderPixelEvent(this),
73 fillPixelBufferEvent(this), intEvent(this),
74 dmaDoneEventAll(MAX_OUTSTANDING_DMA_REQ_CAPACITY, this),
75 dmaDoneEventFree(MAX_OUTSTANDING_DMA_REQ_CAPACITY),
76 enableCapture(p->enable_capture)
77{
78 pioSize = 0xFFFF;
79
80 for (int i = 0; i < MAX_OUTSTANDING_DMA_REQ_CAPACITY; ++i)
81 dmaDoneEventFree[i] = &dmaDoneEventAll[i];
82
83 if (vnc)
84 vnc->setFramebufferAddr(NULL);
85}
86
87HDLcd::~HDLcd()
88{
89 if (virtualDisplayBuffer)
90 delete [] virtualDisplayBuffer;
91}
92
93// read registers and frame buffer
94Tick
95HDLcd::read(PacketPtr pkt)
96{
97 uint32_t data = 0;
98 const Addr daddr = pkt->getAddr() - pioAddr;
99
100 DPRINTF(HDLcd, "read register BASE+0x%04x size=%d\n", daddr,
101 pkt->getSize());
102
103 assert(pkt->getAddr() >= pioAddr &&
104 pkt->getAddr() < pioAddr + pioSize &&
105 pkt->getSize() == 4);
106
1/*
2 * Copyright (c) 2010-2013 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 * Authors: Chris Emmons
38 */
39
40#include "base/vnc/vncinput.hh"
41#include "base/bitmap.hh"
42#include "base/output.hh"
43#include "base/trace.hh"
44#include "debug/HDLcd.hh"
45#include "debug/Uart.hh"
46#include "dev/arm/amba_device.hh"
47#include "dev/arm/base_gic.hh"
48#include "dev/arm/hdlcd.hh"
49#include "mem/packet.hh"
50#include "mem/packet_access.hh"
51#include "sim/system.hh"
52
53using std::vector;
54
55
56// initialize hdlcd registers
57HDLcd::HDLcd(const Params *p)
58 : AmbaDmaDevice(p), version(VERSION_RESETV),
59 int_rawstat(0), int_clear(0), int_mask(0), int_status(0),
60 fb_base(0), fb_line_length(0), fb_line_count(0), fb_line_pitch(0),
61 bus_options(BUS_OPTIONS_RESETV),
62 v_sync(0), v_back_porch(0), v_data(0), v_front_porch(0),
63 h_sync(0), h_back_porch(0), h_data(0), h_front_porch(0),
64 polarities(0), command(0), pixel_format(0),
65 red_select(0), green_select(0), blue_select(0),
66 pixelClock(p->pixel_clock), vnc(p->vnc), bmp(NULL), pic(NULL),
67 frameReadStartTime(0),
68 dmaStartAddr(0), dmaCurAddr(0), dmaMaxAddr(0), dmaPendingNum(0),
69 frameUnderrun(false), virtualDisplayBuffer(NULL), pixelBufferSize(0),
70 pixelIndex(0), doUpdateParams(false), frameUnderway(false),
71 dmaBytesInFlight(0),
72 startFrameEvent(this), endFrameEvent(this), renderPixelEvent(this),
73 fillPixelBufferEvent(this), intEvent(this),
74 dmaDoneEventAll(MAX_OUTSTANDING_DMA_REQ_CAPACITY, this),
75 dmaDoneEventFree(MAX_OUTSTANDING_DMA_REQ_CAPACITY),
76 enableCapture(p->enable_capture)
77{
78 pioSize = 0xFFFF;
79
80 for (int i = 0; i < MAX_OUTSTANDING_DMA_REQ_CAPACITY; ++i)
81 dmaDoneEventFree[i] = &dmaDoneEventAll[i];
82
83 if (vnc)
84 vnc->setFramebufferAddr(NULL);
85}
86
87HDLcd::~HDLcd()
88{
89 if (virtualDisplayBuffer)
90 delete [] virtualDisplayBuffer;
91}
92
93// read registers and frame buffer
94Tick
95HDLcd::read(PacketPtr pkt)
96{
97 uint32_t data = 0;
98 const Addr daddr = pkt->getAddr() - pioAddr;
99
100 DPRINTF(HDLcd, "read register BASE+0x%04x size=%d\n", daddr,
101 pkt->getSize());
102
103 assert(pkt->getAddr() >= pioAddr &&
104 pkt->getAddr() < pioAddr + pioSize &&
105 pkt->getSize() == 4);
106
107 pkt->allocate();
108
109 switch (daddr) {
110 case Version:
111 data = version;
112 break;
113 case Int_RawStat:
114 data = int_rawstat;
115 break;
116 case Int_Clear:
117 panic("HDLCD INT_CLEAR register is Write-Only\n");
118 break;
119 case Int_Mask:
120 data = int_mask;
121 break;
122 case Int_Status:
123 data = int_status;
124 break;
125 case Fb_Base:
126 data = fb_base;
127 break;
128 case Fb_Line_Length:
129 data = fb_line_length;
130 break;
131 case Fb_Line_Count:
132 data = fb_line_count;
133 break;
134 case Fb_Line_Pitch:
135 data = fb_line_pitch;
136 break;
137 case Bus_Options:
138 data = bus_options;
139 break;
140 case V_Sync:
141 data = v_sync;
142 break;
143 case V_Back_Porch:
144 data = v_back_porch;
145 break;
146 case V_Data:
147 data = v_data;
148 break;
149 case V_Front_Porch:
150 data = v_front_porch;
151 break;
152 case H_Sync:
153 data = h_sync;
154 break;
155 case H_Back_Porch:
156 data = h_back_porch;
157 break;
158 case H_Data:
159 data = h_data;
160 break;
161 case H_Front_Porch:
162 data = h_front_porch;
163 break;
164 case Polarities:
165 data = polarities;
166 break;
167 case Command:
168 data = command;
169 break;
170 case Pixel_Format:
171 data = pixel_format;
172 break;
173 case Red_Select:
174 data = red_select;
175 break;
176 case Green_Select:
177 data = green_select;
178 break;
179 case Blue_Select:
180 data = blue_select;
181 break;
182 default:
183 panic("Tried to read HDLCD register that doesn't exist\n", daddr);
184 break;
185 }
186
187 pkt->set<uint32_t>(data);
188 pkt->makeAtomicResponse();
189 return pioDelay;
190}
191
192// write registers and frame buffer
193Tick
194HDLcd::write(PacketPtr pkt)
195{
196 assert(pkt->getAddr() >= pioAddr &&
197 pkt->getAddr() < pioAddr + pioSize &&
198 pkt->getSize() == 4);
199
200 const uint32_t data = pkt->get<uint32_t>();
201 const Addr daddr = pkt->getAddr() - pioAddr;
202
203 DPRINTF(HDLcd, "write register BASE+%0x04x <= 0x%08x\n", daddr,
204 pkt->get<uint32_t>());
205
206 switch (daddr) {
207 case Version:
208 panic("HDLCD VERSION register is read-Only\n");
209 break;
210 case Int_RawStat:
211 int_rawstat = data;
212 break;
213 case Int_Clear:
214 int_clear = data;
215 break;
216 case Int_Mask:
217 int_mask = data;
218 break;
219 case Int_Status:
220 panic("HDLCD INT_STATUS register is read-Only\n");
221 break;
222 case Fb_Base:
223 fb_base = data;
224 DPRINTF(HDLcd, "HDLCD Frame Buffer located at addr 0x%08x\n", fb_base);
225 break;
226 case Fb_Line_Length:
227 fb_line_length = data;
228 DPRINTF(HDLcd, "HDLCD res = %d x %d\n", width(), height());
229 break;
230 case Fb_Line_Count:
231 fb_line_count = data;
232 DPRINTF(HDLcd, "HDLCD res = %d x %d\n", width(), height());
233 break;
234 case Fb_Line_Pitch:
235 fb_line_pitch = data;
236 break;
237 case Bus_Options: {
238 BusOptsReg old_bus_options;
239 old_bus_options = bus_options;
240 bus_options = data;
241 if (bus_options.max_outstanding != old_bus_options.max_outstanding)
242 DPRINTF(HDLcd,
243 "Changing HDLcd outstanding dma transactions from %d to %d\n",
244 old_bus_options.max_outstanding, bus_options.max_outstanding);
245 if (bus_options.burst_len != old_bus_options.burst_len)
246 DPRINTF(HDLcd,
247 "Changing HDLcd dma burst length from %d bytes to %d bytes\n",
248 old_bus_options.burst_len, bus_options.burst_len); }
249 break;
250 case V_Sync:
251 v_sync = data;
252 break;
253 case V_Back_Porch:
254 v_back_porch = data;
255 break;
256 case V_Data:
257 v_data = data;
258 break;
259 case V_Front_Porch:
260 v_front_porch = data;
261 break;
262 case H_Sync:
263 h_sync = data;
264 break;
265 case H_Back_Porch:
266 h_back_porch = data;
267 break;
268 case H_Data:
269 h_data = data;
270 break;
271 case H_Front_Porch:
272 h_front_porch = data;
273 break;
274 case Polarities:
275 polarities = data;
276 break;
277 case Command: {
278 CommandReg new_command;
279 new_command = data;
280 if (new_command.enable != command.enable) {
281 DPRINTF(HDLcd, "HDLCD switched %s\n",
282 new_command.enable==0 ? "off" : "on");
283 if (new_command.enable) {
284 doUpdateParams = true;
285 if (!frameUnderway) {
286 schedule(startFrameEvent, clockEdge());
287 }
288 }
289 }
290 command = new_command; }
291 break;
292 case Pixel_Format:
293 pixel_format = data;
294 DPRINTF(HDLcd, "HDLCD res = %d x %d\n", width(), height());
295 DPRINTF(HDLcd, "HDLCD bytes per pixel = %d\n", bytesPerPixel());
296 DPRINTF(HDLcd, "HDLCD endianness = %s\n",
297 pixel_format.big_endian ? "big" : "little");
298 break;
299 case Red_Select:
300 red_select = data;
301 break;
302 case Green_Select:
303 green_select = data;
304 break;
305 case Blue_Select:
306 blue_select = data;
307 break;
308 default:
309 panic("Tried to write HDLCD register that doesn't exist\n", daddr);
310 break;
311 }
312
313 pkt->makeAtomicResponse();
314 return pioDelay;
315}
316
317void
318HDLcd::updateVideoParams(bool unserializing = false)
319{
320 const uint16_t bpp = bytesPerPixel() << 3;
321 const size_t buffer_size = bytesPerPixel() * width() * height();
322
323 // updating these parameters while LCD is enabled is not supported
324 if (frameUnderway && !unserializing)
325 panic("Attempting to change some HDLCD parameters while the controller"
326 " is active is not allowed");
327
328 // resize the virtualDisplayBuffer unless we are unserializing - it may
329 // have changed size
330 // there must be no outstanding DMA transactions for this to work
331 if (!unserializing) {
332 assert(dmaPendingNum == 0);
333 if (virtualDisplayBuffer)
334 delete [] virtualDisplayBuffer;
335 virtualDisplayBuffer = new uint8_t[buffer_size];
336 memset(virtualDisplayBuffer, 0, buffer_size);
337 }
338
339 assert(virtualDisplayBuffer);
340 if (vnc)
341 vnc->setFramebufferAddr(virtualDisplayBuffer);
342
343 if (bmp)
344 delete bmp;
345
346 DPRINTF(HDLcd, "bpp = %d\n", bpp);
347 DPRINTF(HDLcd, "display size = %d x %d\n", width(), height());
348#if TRACING_ON
349 const size_t totalLinesPerFrame = v_back_porch.val + 1 +
350 v_data.val + 1 +
351 v_front_porch.val + 1 +
352 v_sync.val + 1;
353 const double fps = (double)SimClock::Frequency /
354 (double)(PClksPerLine() * totalLinesPerFrame * pixelClock);
355#endif
356 DPRINTF(HDLcd, "simulated refresh rate ~ %.1ffps generating ~ %.1fMB/s "
357 "traffic ([%.1fMHz, T=%d sim clocks] pclk, %d bpp => %.1fMB/s peak requirement)\n",
358 fps,
359 fps * buffer_size / 1024 / 1024,
360 (double)SimClock::Frequency / pixelClock / 1000000.0,
361 pixelClock,
362 bpp,
363 (double)(SimClock::Frequency / pixelClock * (bpp / 8)) / 1024 / 1024);
364
365 if (pixel_format.big_endian)
366 panic("Big Endian pixel format not implemented by HDLcd controller");
367
368 if (vnc) {
369 if ((bpp == 24) &&
370 (red_select.size == 8) &&
371 (blue_select.size == 8) &&
372 (green_select.size == 8) &&
373 (green_select.offset == 8)) {
374 if ((blue_select.offset == 0) &&
375 (red_select.offset == 16)) {
376 vnc->setFrameBufferParams(VideoConvert::rgb8888, width(),
377 height());
378 bmp = new Bitmap(VideoConvert::rgb8888, width(), height(),
379 virtualDisplayBuffer);
380 DPRINTF(HDLcd, "color mode: rgb888\n");
381 } else if ((red_select.offset == 0) &&
382 (blue_select.offset == 16)) {
383 vnc->setFrameBufferParams(VideoConvert::bgr8888, width(),
384 height());
385 bmp = new Bitmap(VideoConvert::bgr8888, width(), height(),
386 virtualDisplayBuffer);
387 DPRINTF(HDLcd, "color mode: bgr888\n");
388 }
389 } else if ((bpp == 16) &&
390 (red_select.size == 5) &&
391 (blue_select.size == 5) &&
392 (green_select.size == 6) &&
393 (green_select.offset == 5)) {
394 if ((blue_select.offset == 0) &&
395 (red_select.offset == 11)) {
396 vnc->setFrameBufferParams(VideoConvert::rgb565, width(),
397 height());
398 bmp = new Bitmap(VideoConvert::rgb565, width(), height(),
399 virtualDisplayBuffer);
400 DPRINTF(HDLcd, "color mode: rgb565\n");
401 } else if ((red_select.offset == 0) &&
402 (blue_select.offset == 11)) {
403 vnc->setFrameBufferParams(VideoConvert::bgr565, width(),
404 height());
405 bmp = new Bitmap(VideoConvert::bgr565, width(), height(),
406 virtualDisplayBuffer);
407 DPRINTF(HDLcd, "color mode: bgr565\n");
408 }
409 } else {
410 DPRINTF(HDLcd, "color mode: undefined\n");
411 panic("Unimplemented video mode\n");
412 }
413 }
414}
415
416void
417HDLcd::startFrame()
418{
419 // 0. Check that we are in the appropriate state
420 assert(!frameUnderway);
421 if (!command.enable)
422 return;
423 DPRINTF(HDLcd, "Frame read started\n");
424 if (doUpdateParams) {
425 updateVideoParams();
426 doUpdateParams = false;
427 }
428 frameUnderway = true;
429 assert(virtualDisplayBuffer);
430 assert(pixelBufferSize == 0);
431 assert(dmaBytesInFlight == 0);
432 assert(dmaPendingNum == 0);
433 assert(dmaDoneEventFree.size() == dmaDoneEventAll.size());
434 assert(!renderPixelEvent.scheduled());
435 // currently only support positive line pitches equal to the line length
436 assert(width() * bytesPerPixel() == fb_line_pitch);
437
438 // 1. Start DMA'ing the frame; subsequent transactions created as we go
439 dmaCurAddr = dmaStartAddr = fb_base;
440 dmaMaxAddr = static_cast<Addr>(width() * height() * bytesPerPixel()) +
441 dmaCurAddr;
442 frameReadStartTime = curTick();
443 pixelIndex = 0;
444 frameUnderrun = false;
445 fillPixelBuffer();
446
447 // 2. Schedule first pixelclock read; subsequent reads generated as we go
448 Tick firstPixelReadTick = curTick() + pixelClock * (
449 PClksPerLine() * (v_sync.val + 1 +
450 v_back_porch.val + 1) +
451 h_sync.val + 1 +
452 h_back_porch.val + 1);
453 schedule(renderPixelEvent, firstPixelReadTick);
454}
455
456void
457HDLcd::fillPixelBuffer()
458{
459 // - am I under the LCD dma transaction total?
460 // - do I have more data to transfer?
461 // - have I not yet underrun for this frame?
462 // - is there room to put the data in the pixel buffer including any
463 // outstanding dma transfers in flight?
464 while ((dmaPendingNum < maxOutstandingDma()) &&
465 (dmaMaxAddr > dmaCurAddr) &&
466 !frameUnderrun &&
467 bytesFreeInPixelBuffer() > dmaBurstLength() * AXI_PORT_WIDTH) {
468 // try largest transaction size allowed first but switch to smaller
469 // sizes for trailing bytes
470 size_t transaction_size = dmaBurstLength() * AXI_PORT_WIDTH;
471 while (transaction_size > (dmaMaxAddr - dmaCurAddr))
472 transaction_size >>= 1;
473 assert(transaction_size > 0);
474
475 // concurrent dma reads need different dma done events
476 // due to assertion in scheduling state
477 ++dmaPendingNum;
478
479 assert(!dmaDoneEventFree.empty());
480 DmaDoneEvent *event(dmaDoneEventFree.back());
481 dmaDoneEventFree.pop_back();
482 assert(event);
483 assert(!event->scheduled());
484
485 // We use a uncachable request here because the requests from the CPU
486 // will be uncacheable as well. If we have uncacheable and cacheable
487 // requests in the memory system for the same address it won't be
488 // pleased
489 event->setTransactionSize(transaction_size);
490 dmaPort.dmaAction(MemCmd::ReadReq, dmaCurAddr, transaction_size, event,
491 virtualDisplayBuffer + dmaCurAddr - dmaStartAddr,
492 0, Request::UNCACHEABLE);
493 dmaCurAddr += transaction_size;
494 dmaBytesInFlight += transaction_size;
495 }
496}
497
498void
499HDLcd::renderPixel()
500{
501 // try to handle multiple pixels at a time; doing so reduces the accuracy
502 // of the underrun detection but lowers simulation overhead
503 const size_t count = 32;
504 assert(width() % count == 0); // not set up to handle trailing pixels
505
506 // have we underrun on this frame anytime before?
507 if (frameUnderrun) {
508 // the LCD controller gives up on a frame if an underrun occurs and
509 // resumes regular operation on the next frame
510 pixelBufferSize = 0;
511 } else {
512 // did we underrun on this set of pixels?
513 if (pixelBufferSize < bytesPerPixel() * count) {
514 warn("HDLcd controller buffer underrun\n");
515 frameUnderrun = true;
516 int_rawstat.underrun = 1;
517 if (!intEvent.scheduled())
518 schedule(intEvent, clockEdge());
519 } else {
520 // emulate the pixel read from the internal buffer
521 pixelBufferSize -= bytesPerPixel() * count;
522 }
523 }
524
525 // the DMA may have previously stalled due to the buffer being full;
526 // give it a kick; it knows not to fill if at end of frame, underrun, etc
527 if (!fillPixelBufferEvent.scheduled())
528 schedule(fillPixelBufferEvent, clockEdge());
529
530 // schedule the next pixel read according to where it is in the frame
531 pixelIndex += count;
532 assert(pixelIndex <= width() * height());
533 size_t x = pixelIndex % width();
534 Tick nextEventTick = curTick();
535 if (x == 0) {
536 // start of new line
537 nextEventTick += pixelClock * ((h_front_porch.val + 1) +
538 (h_back_porch.val + 1) +
539 (h_sync.val + 1));
540 if (pixelIndex == width() * height()) {
541 // end of frame
542 nextEventTick += PClksPerLine() * (v_front_porch.val + 1) *
543 pixelClock;
544 schedule(endFrameEvent, nextEventTick);
545 return;
546 }
547 } else {
548 nextEventTick += pixelClock * count;
549 }
550
551 schedule(renderPixelEvent, nextEventTick);
552}
553
554void
555HDLcd::endFrame() {
556 assert(pixelBufferSize == 0);
557 assert(dmaPendingNum == 0);
558 assert(dmaBytesInFlight == 0);
559 assert(dmaDoneEventFree.size() == dmaDoneEventAll.size());
560
561 if (vnc)
562 vnc->setDirty();
563
564 if (enableCapture) {
565 if (!pic)
566 pic = simout.create(csprintf("%s.framebuffer.bmp", sys->name()), true);
567
568 assert(bmp);
569 assert(pic);
570 pic->seekp(0);
571 bmp->write(pic);
572 }
573
574 // start the next frame
575 frameUnderway = false;
576 startFrame();
577}
578
579void
580HDLcd::dmaDone(DmaDoneEvent *event)
581{
582 const size_t transactionLength = event->getTransactionSize();
583 assert(pixelBufferSize + transactionLength < PIXEL_BUFFER_CAPACITY);
584 assert(dmaCurAddr <= dmaMaxAddr);
585
586 dmaDoneEventFree.push_back(event);
587 --dmaPendingNum;
588 assert(MAX_OUTSTANDING_DMA_REQ_CAPACITY - dmaDoneEventFree.size() ==
589 dmaPendingNum);
590
591 // add the data to the pixel buffer
592 dmaBytesInFlight -= transactionLength;
593 pixelBufferSize += transactionLength;
594
595 // schedule another dma transaction if:
596 // - we're not done reading the frame
597 // - there is sufficient room in the pixel buffer for another transaction
598 // - another fillPixelBufferEvent is not already scheduled
599 const size_t targetTransSize = dmaBurstLength() * AXI_PORT_WIDTH;
600 if ((dmaCurAddr < dmaMaxAddr) &&
601 (bytesFreeInPixelBuffer() + targetTransSize < PIXEL_BUFFER_CAPACITY) &&
602 !fillPixelBufferEvent.scheduled()) {
603 schedule(fillPixelBufferEvent, clockEdge());
604 }
605}
606
607void
608HDLcd::serialize(std::ostream &os)
609{
610 DPRINTF(HDLcd, "Serializing ARM HDLCD\n");
611
612 const uint32_t version_serial = version;
613 SERIALIZE_SCALAR(version_serial);
614 const uint32_t int_rawstat_serial = int_rawstat;
615 SERIALIZE_SCALAR(int_rawstat_serial);
616 const uint32_t int_clear_serial = int_clear;
617 SERIALIZE_SCALAR(int_clear_serial);
618 const uint32_t int_mask_serial = int_mask;
619 SERIALIZE_SCALAR(int_mask_serial);
620 const uint32_t int_status_serial = int_status;
621 SERIALIZE_SCALAR(int_status_serial);
622
623 SERIALIZE_SCALAR(fb_base);
624 SERIALIZE_SCALAR(fb_line_length);
625
626 const uint32_t fb_line_count_serial = fb_line_count;
627 SERIALIZE_SCALAR(fb_line_count_serial);
628
629 SERIALIZE_SCALAR(fb_line_pitch);
630
631 const uint32_t bus_options_serial = bus_options;
632 SERIALIZE_SCALAR(bus_options_serial);
633 const uint32_t v_sync_serial = v_sync;
634 SERIALIZE_SCALAR(v_sync_serial);
635 const uint32_t v_back_porch_serial = v_back_porch;
636 SERIALIZE_SCALAR(v_back_porch_serial);
637 const uint32_t v_data_serial = v_data;
638 SERIALIZE_SCALAR(v_data_serial);
639 const uint32_t v_front_porch_serial = v_front_porch;
640 SERIALIZE_SCALAR(v_front_porch_serial);
641 const uint32_t h_sync_serial = h_sync;
642 SERIALIZE_SCALAR(h_sync_serial);
643 const uint32_t h_back_porch_serial = h_back_porch;
644 SERIALIZE_SCALAR(h_back_porch_serial);
645 const uint32_t h_data_serial = h_data;
646 SERIALIZE_SCALAR(h_data_serial);
647 const uint32_t h_front_porch_serial = h_front_porch;
648 SERIALIZE_SCALAR(h_front_porch_serial);
649 const uint32_t polarities_serial = polarities;
650 SERIALIZE_SCALAR(polarities_serial);
651 const uint32_t command_serial = command;
652 SERIALIZE_SCALAR(command_serial);
653 const uint32_t pixel_format_serial = pixel_format;
654 SERIALIZE_SCALAR(pixel_format_serial);
655 const uint32_t red_select_serial = red_select;
656 SERIALIZE_SCALAR(red_select_serial);
657 const uint32_t green_select_serial = green_select;
658 SERIALIZE_SCALAR(green_select_serial);
659 const uint32_t blue_select_serial = blue_select;
660 SERIALIZE_SCALAR(blue_select_serial);
661
662 SERIALIZE_SCALAR(frameReadStartTime);
663 SERIALIZE_SCALAR(dmaStartAddr);
664 SERIALIZE_SCALAR(dmaCurAddr);
665 SERIALIZE_SCALAR(dmaMaxAddr);
666 SERIALIZE_SCALAR(dmaPendingNum);
667 SERIALIZE_SCALAR(frameUnderrun);
668
669 const size_t buffer_size = bytesPerPixel() * width() * height();
670 SERIALIZE_ARRAY(virtualDisplayBuffer, buffer_size);
671
672 SERIALIZE_SCALAR(pixelBufferSize);
673 SERIALIZE_SCALAR(pixelIndex);
674 SERIALIZE_SCALAR(doUpdateParams);
675 SERIALIZE_SCALAR(frameUnderway);
676 SERIALIZE_SCALAR(dmaBytesInFlight);
677
678 Tick start_event_time = 0;
679 Tick end_event_time = 0;
680 Tick render_pixel_event_time = 0;
681 Tick fill_pixel_buffer_event_time = 0;
682 Tick int_event_time = 0;
683 if (startFrameEvent.scheduled())
684 start_event_time = startFrameEvent.when();
685 if (endFrameEvent.scheduled())
686 end_event_time = endFrameEvent.when();
687 if (renderPixelEvent.scheduled())
688 render_pixel_event_time = renderPixelEvent.when();
689 if (fillPixelBufferEvent.scheduled())
690 fill_pixel_buffer_event_time = fillPixelBufferEvent.when();
691 if (intEvent.scheduled())
692 int_event_time = intEvent.when();
693 SERIALIZE_SCALAR(start_event_time);
694 SERIALIZE_SCALAR(end_event_time);
695 SERIALIZE_SCALAR(render_pixel_event_time);
696 SERIALIZE_SCALAR(fill_pixel_buffer_event_time);
697 SERIALIZE_SCALAR(int_event_time);
698
699 vector<Tick> dma_done_event_tick(MAX_OUTSTANDING_DMA_REQ_CAPACITY);
700 vector<size_t> dma_done_event_burst_len(MAX_OUTSTANDING_DMA_REQ_CAPACITY);
701 for (int x = 0; x < MAX_OUTSTANDING_DMA_REQ_CAPACITY; ++x) {
702 dma_done_event_tick[x] = dmaDoneEventAll[x].scheduled() ?
703 dmaDoneEventAll[x].when() : 0;
704 dma_done_event_burst_len[x] = dmaDoneEventAll[x].scheduled() ?
705 dmaDoneEventAll[x].getTransactionSize() : 0;
706 }
707 arrayParamOut(os, "dma_done_event_tick", dma_done_event_tick);
708 arrayParamOut(os, "dma_done_event_burst_length", dma_done_event_burst_len);
709}
710
711void
712HDLcd::unserialize(Checkpoint *cp, const std::string &section)
713{
714 uint32_t version_serial, int_rawstat_serial, int_clear_serial,
715 int_mask_serial, int_status_serial, fb_line_count_serial,
716 bus_options_serial, v_sync_serial, v_back_porch_serial,
717 v_data_serial, v_front_porch_serial, h_sync_serial,
718 h_back_porch_serial, h_data_serial, h_front_porch_serial,
719 polarities_serial, command_serial, pixel_format_serial,
720 red_select_serial, green_select_serial, blue_select_serial;
721
722 DPRINTF(HDLcd, "Unserializing ARM HDLCD\n");
723
724 UNSERIALIZE_SCALAR(version_serial);
725 version = version_serial;
726 UNSERIALIZE_SCALAR(int_rawstat_serial);
727 int_rawstat = int_rawstat_serial;
728 UNSERIALIZE_SCALAR(int_clear_serial);
729 int_clear = int_clear_serial;
730 UNSERIALIZE_SCALAR(int_mask_serial);
731 int_mask = int_mask_serial;
732 UNSERIALIZE_SCALAR(int_status_serial);
733 int_status = int_status_serial;
734
735 UNSERIALIZE_SCALAR(fb_base);
736 UNSERIALIZE_SCALAR(fb_line_length);
737
738 UNSERIALIZE_SCALAR(fb_line_count_serial);
739 fb_line_count = fb_line_count_serial;
740
741 UNSERIALIZE_SCALAR(fb_line_pitch);
742
743 UNSERIALIZE_SCALAR(bus_options_serial);
744 bus_options = bus_options_serial;
745 UNSERIALIZE_SCALAR(v_sync_serial);
746 v_sync = v_sync_serial;
747 UNSERIALIZE_SCALAR(v_back_porch_serial);
748 v_back_porch = v_back_porch_serial;
749 UNSERIALIZE_SCALAR(v_data_serial);
750 v_data = v_data_serial;
751 UNSERIALIZE_SCALAR(v_front_porch_serial);
752 v_front_porch = v_front_porch_serial;
753 UNSERIALIZE_SCALAR(h_sync_serial);
754 h_sync = h_sync_serial;
755 UNSERIALIZE_SCALAR(h_back_porch_serial);
756 h_back_porch = h_back_porch_serial;
757 UNSERIALIZE_SCALAR(h_data_serial);
758 h_data = h_data_serial;
759 UNSERIALIZE_SCALAR(h_front_porch_serial);
760 h_front_porch = h_front_porch_serial;
761 UNSERIALIZE_SCALAR(polarities_serial);
762 polarities = polarities_serial;
763 UNSERIALIZE_SCALAR(command_serial);
764 command = command_serial;
765 UNSERIALIZE_SCALAR(pixel_format_serial);
766 pixel_format = pixel_format_serial;
767 UNSERIALIZE_SCALAR(red_select_serial);
768 red_select = red_select_serial;
769 UNSERIALIZE_SCALAR(green_select_serial);
770 green_select = green_select_serial;
771 UNSERIALIZE_SCALAR(blue_select_serial);
772 blue_select = blue_select_serial;
773
774 UNSERIALIZE_SCALAR(frameReadStartTime);
775 UNSERIALIZE_SCALAR(dmaStartAddr);
776 UNSERIALIZE_SCALAR(dmaCurAddr);
777 UNSERIALIZE_SCALAR(dmaMaxAddr);
778 UNSERIALIZE_SCALAR(dmaPendingNum);
779 UNSERIALIZE_SCALAR(frameUnderrun);
780 UNSERIALIZE_SCALAR(dmaBytesInFlight);
781
782 const size_t buffer_size = bytesPerPixel() * width() * height();
783 virtualDisplayBuffer = new uint8_t[buffer_size];
784 UNSERIALIZE_ARRAY(virtualDisplayBuffer, buffer_size);
785
786 UNSERIALIZE_SCALAR(pixelBufferSize);
787 UNSERIALIZE_SCALAR(pixelIndex);
788 UNSERIALIZE_SCALAR(doUpdateParams);
789 UNSERIALIZE_SCALAR(frameUnderway);
790
791 Tick start_event_time = 0;
792 Tick end_event_time = 0;
793 Tick render_pixel_event_time = 0;
794 Tick fill_pixel_buffer_event_time = 0;
795 Tick int_event_time = 0;
796 UNSERIALIZE_SCALAR(start_event_time);
797 UNSERIALIZE_SCALAR(end_event_time);
798 UNSERIALIZE_SCALAR(render_pixel_event_time);
799 UNSERIALIZE_SCALAR(fill_pixel_buffer_event_time);
800 UNSERIALIZE_SCALAR(int_event_time);
801 if (start_event_time)
802 schedule(startFrameEvent, start_event_time);
803 if (end_event_time)
804 schedule(endFrameEvent, end_event_time);
805 if (render_pixel_event_time)
806 schedule(renderPixelEvent, render_pixel_event_time);
807 if (fill_pixel_buffer_event_time)
808 schedule(fillPixelBufferEvent, fill_pixel_buffer_event_time);
809 if (int_event_time)
810 schedule(intEvent, int_event_time);
811
812 vector<Tick> dma_done_event_tick(MAX_OUTSTANDING_DMA_REQ_CAPACITY);
813 vector<Tick> dma_done_event_burst_len(MAX_OUTSTANDING_DMA_REQ_CAPACITY);
814 arrayParamIn(cp, section, "dma_done_event_tick", dma_done_event_tick);
815 arrayParamIn(cp, section, "dma_done_event_burst_length", dma_done_event_burst_len);
816 dmaDoneEventFree.clear();
817 for (int x = 0; x < MAX_OUTSTANDING_DMA_REQ_CAPACITY; ++x) {
818 if (dma_done_event_tick[x]) {
819 dmaDoneEventAll[x].setTransactionSize(dma_done_event_burst_len[x]);
820 schedule(dmaDoneEventAll[x], dma_done_event_tick[x]);
821 } else
822 dmaDoneEventFree.push_back(&dmaDoneEventAll[x]);
823 }
824 assert(MAX_OUTSTANDING_DMA_REQ_CAPACITY - dmaDoneEventFree.size() == dmaPendingNum);
825
826 if (frameUnderway) {
827 updateVideoParams(true);
828 if (vnc)
829 vnc->setDirty();
830 }
831}
832
833void
834HDLcd::generateInterrupt()
835{
836 int_status = int_rawstat & int_mask;
837 DPRINTF(HDLcd, "Generate Interrupt: int_rawstat=0x%08x int_mask=0x%08x "
838 "int_status=0x%08x\n",
839 (uint32_t)int_rawstat, (uint32_t)int_mask, (uint32_t)int_status);
840
841 if (int_status != 0) {
842 gic->sendInt(intNum);
843 DPRINTF(HDLcd, " -- Generated\n");
844 }
845}
846
847AddrRangeList
848HDLcd::getAddrRanges() const
849{
850 AddrRangeList ranges;
851 ranges.push_back(RangeSize(pioAddr, pioSize));
852 return ranges;
853}
854
855HDLcd *
856HDLcdParams::create()
857{
858 return new HDLcd(this);
859}
107 switch (daddr) {
108 case Version:
109 data = version;
110 break;
111 case Int_RawStat:
112 data = int_rawstat;
113 break;
114 case Int_Clear:
115 panic("HDLCD INT_CLEAR register is Write-Only\n");
116 break;
117 case Int_Mask:
118 data = int_mask;
119 break;
120 case Int_Status:
121 data = int_status;
122 break;
123 case Fb_Base:
124 data = fb_base;
125 break;
126 case Fb_Line_Length:
127 data = fb_line_length;
128 break;
129 case Fb_Line_Count:
130 data = fb_line_count;
131 break;
132 case Fb_Line_Pitch:
133 data = fb_line_pitch;
134 break;
135 case Bus_Options:
136 data = bus_options;
137 break;
138 case V_Sync:
139 data = v_sync;
140 break;
141 case V_Back_Porch:
142 data = v_back_porch;
143 break;
144 case V_Data:
145 data = v_data;
146 break;
147 case V_Front_Porch:
148 data = v_front_porch;
149 break;
150 case H_Sync:
151 data = h_sync;
152 break;
153 case H_Back_Porch:
154 data = h_back_porch;
155 break;
156 case H_Data:
157 data = h_data;
158 break;
159 case H_Front_Porch:
160 data = h_front_porch;
161 break;
162 case Polarities:
163 data = polarities;
164 break;
165 case Command:
166 data = command;
167 break;
168 case Pixel_Format:
169 data = pixel_format;
170 break;
171 case Red_Select:
172 data = red_select;
173 break;
174 case Green_Select:
175 data = green_select;
176 break;
177 case Blue_Select:
178 data = blue_select;
179 break;
180 default:
181 panic("Tried to read HDLCD register that doesn't exist\n", daddr);
182 break;
183 }
184
185 pkt->set<uint32_t>(data);
186 pkt->makeAtomicResponse();
187 return pioDelay;
188}
189
190// write registers and frame buffer
191Tick
192HDLcd::write(PacketPtr pkt)
193{
194 assert(pkt->getAddr() >= pioAddr &&
195 pkt->getAddr() < pioAddr + pioSize &&
196 pkt->getSize() == 4);
197
198 const uint32_t data = pkt->get<uint32_t>();
199 const Addr daddr = pkt->getAddr() - pioAddr;
200
201 DPRINTF(HDLcd, "write register BASE+%0x04x <= 0x%08x\n", daddr,
202 pkt->get<uint32_t>());
203
204 switch (daddr) {
205 case Version:
206 panic("HDLCD VERSION register is read-Only\n");
207 break;
208 case Int_RawStat:
209 int_rawstat = data;
210 break;
211 case Int_Clear:
212 int_clear = data;
213 break;
214 case Int_Mask:
215 int_mask = data;
216 break;
217 case Int_Status:
218 panic("HDLCD INT_STATUS register is read-Only\n");
219 break;
220 case Fb_Base:
221 fb_base = data;
222 DPRINTF(HDLcd, "HDLCD Frame Buffer located at addr 0x%08x\n", fb_base);
223 break;
224 case Fb_Line_Length:
225 fb_line_length = data;
226 DPRINTF(HDLcd, "HDLCD res = %d x %d\n", width(), height());
227 break;
228 case Fb_Line_Count:
229 fb_line_count = data;
230 DPRINTF(HDLcd, "HDLCD res = %d x %d\n", width(), height());
231 break;
232 case Fb_Line_Pitch:
233 fb_line_pitch = data;
234 break;
235 case Bus_Options: {
236 BusOptsReg old_bus_options;
237 old_bus_options = bus_options;
238 bus_options = data;
239 if (bus_options.max_outstanding != old_bus_options.max_outstanding)
240 DPRINTF(HDLcd,
241 "Changing HDLcd outstanding dma transactions from %d to %d\n",
242 old_bus_options.max_outstanding, bus_options.max_outstanding);
243 if (bus_options.burst_len != old_bus_options.burst_len)
244 DPRINTF(HDLcd,
245 "Changing HDLcd dma burst length from %d bytes to %d bytes\n",
246 old_bus_options.burst_len, bus_options.burst_len); }
247 break;
248 case V_Sync:
249 v_sync = data;
250 break;
251 case V_Back_Porch:
252 v_back_porch = data;
253 break;
254 case V_Data:
255 v_data = data;
256 break;
257 case V_Front_Porch:
258 v_front_porch = data;
259 break;
260 case H_Sync:
261 h_sync = data;
262 break;
263 case H_Back_Porch:
264 h_back_porch = data;
265 break;
266 case H_Data:
267 h_data = data;
268 break;
269 case H_Front_Porch:
270 h_front_porch = data;
271 break;
272 case Polarities:
273 polarities = data;
274 break;
275 case Command: {
276 CommandReg new_command;
277 new_command = data;
278 if (new_command.enable != command.enable) {
279 DPRINTF(HDLcd, "HDLCD switched %s\n",
280 new_command.enable==0 ? "off" : "on");
281 if (new_command.enable) {
282 doUpdateParams = true;
283 if (!frameUnderway) {
284 schedule(startFrameEvent, clockEdge());
285 }
286 }
287 }
288 command = new_command; }
289 break;
290 case Pixel_Format:
291 pixel_format = data;
292 DPRINTF(HDLcd, "HDLCD res = %d x %d\n", width(), height());
293 DPRINTF(HDLcd, "HDLCD bytes per pixel = %d\n", bytesPerPixel());
294 DPRINTF(HDLcd, "HDLCD endianness = %s\n",
295 pixel_format.big_endian ? "big" : "little");
296 break;
297 case Red_Select:
298 red_select = data;
299 break;
300 case Green_Select:
301 green_select = data;
302 break;
303 case Blue_Select:
304 blue_select = data;
305 break;
306 default:
307 panic("Tried to write HDLCD register that doesn't exist\n", daddr);
308 break;
309 }
310
311 pkt->makeAtomicResponse();
312 return pioDelay;
313}
314
315void
316HDLcd::updateVideoParams(bool unserializing = false)
317{
318 const uint16_t bpp = bytesPerPixel() << 3;
319 const size_t buffer_size = bytesPerPixel() * width() * height();
320
321 // updating these parameters while LCD is enabled is not supported
322 if (frameUnderway && !unserializing)
323 panic("Attempting to change some HDLCD parameters while the controller"
324 " is active is not allowed");
325
326 // resize the virtualDisplayBuffer unless we are unserializing - it may
327 // have changed size
328 // there must be no outstanding DMA transactions for this to work
329 if (!unserializing) {
330 assert(dmaPendingNum == 0);
331 if (virtualDisplayBuffer)
332 delete [] virtualDisplayBuffer;
333 virtualDisplayBuffer = new uint8_t[buffer_size];
334 memset(virtualDisplayBuffer, 0, buffer_size);
335 }
336
337 assert(virtualDisplayBuffer);
338 if (vnc)
339 vnc->setFramebufferAddr(virtualDisplayBuffer);
340
341 if (bmp)
342 delete bmp;
343
344 DPRINTF(HDLcd, "bpp = %d\n", bpp);
345 DPRINTF(HDLcd, "display size = %d x %d\n", width(), height());
346#if TRACING_ON
347 const size_t totalLinesPerFrame = v_back_porch.val + 1 +
348 v_data.val + 1 +
349 v_front_porch.val + 1 +
350 v_sync.val + 1;
351 const double fps = (double)SimClock::Frequency /
352 (double)(PClksPerLine() * totalLinesPerFrame * pixelClock);
353#endif
354 DPRINTF(HDLcd, "simulated refresh rate ~ %.1ffps generating ~ %.1fMB/s "
355 "traffic ([%.1fMHz, T=%d sim clocks] pclk, %d bpp => %.1fMB/s peak requirement)\n",
356 fps,
357 fps * buffer_size / 1024 / 1024,
358 (double)SimClock::Frequency / pixelClock / 1000000.0,
359 pixelClock,
360 bpp,
361 (double)(SimClock::Frequency / pixelClock * (bpp / 8)) / 1024 / 1024);
362
363 if (pixel_format.big_endian)
364 panic("Big Endian pixel format not implemented by HDLcd controller");
365
366 if (vnc) {
367 if ((bpp == 24) &&
368 (red_select.size == 8) &&
369 (blue_select.size == 8) &&
370 (green_select.size == 8) &&
371 (green_select.offset == 8)) {
372 if ((blue_select.offset == 0) &&
373 (red_select.offset == 16)) {
374 vnc->setFrameBufferParams(VideoConvert::rgb8888, width(),
375 height());
376 bmp = new Bitmap(VideoConvert::rgb8888, width(), height(),
377 virtualDisplayBuffer);
378 DPRINTF(HDLcd, "color mode: rgb888\n");
379 } else if ((red_select.offset == 0) &&
380 (blue_select.offset == 16)) {
381 vnc->setFrameBufferParams(VideoConvert::bgr8888, width(),
382 height());
383 bmp = new Bitmap(VideoConvert::bgr8888, width(), height(),
384 virtualDisplayBuffer);
385 DPRINTF(HDLcd, "color mode: bgr888\n");
386 }
387 } else if ((bpp == 16) &&
388 (red_select.size == 5) &&
389 (blue_select.size == 5) &&
390 (green_select.size == 6) &&
391 (green_select.offset == 5)) {
392 if ((blue_select.offset == 0) &&
393 (red_select.offset == 11)) {
394 vnc->setFrameBufferParams(VideoConvert::rgb565, width(),
395 height());
396 bmp = new Bitmap(VideoConvert::rgb565, width(), height(),
397 virtualDisplayBuffer);
398 DPRINTF(HDLcd, "color mode: rgb565\n");
399 } else if ((red_select.offset == 0) &&
400 (blue_select.offset == 11)) {
401 vnc->setFrameBufferParams(VideoConvert::bgr565, width(),
402 height());
403 bmp = new Bitmap(VideoConvert::bgr565, width(), height(),
404 virtualDisplayBuffer);
405 DPRINTF(HDLcd, "color mode: bgr565\n");
406 }
407 } else {
408 DPRINTF(HDLcd, "color mode: undefined\n");
409 panic("Unimplemented video mode\n");
410 }
411 }
412}
413
414void
415HDLcd::startFrame()
416{
417 // 0. Check that we are in the appropriate state
418 assert(!frameUnderway);
419 if (!command.enable)
420 return;
421 DPRINTF(HDLcd, "Frame read started\n");
422 if (doUpdateParams) {
423 updateVideoParams();
424 doUpdateParams = false;
425 }
426 frameUnderway = true;
427 assert(virtualDisplayBuffer);
428 assert(pixelBufferSize == 0);
429 assert(dmaBytesInFlight == 0);
430 assert(dmaPendingNum == 0);
431 assert(dmaDoneEventFree.size() == dmaDoneEventAll.size());
432 assert(!renderPixelEvent.scheduled());
433 // currently only support positive line pitches equal to the line length
434 assert(width() * bytesPerPixel() == fb_line_pitch);
435
436 // 1. Start DMA'ing the frame; subsequent transactions created as we go
437 dmaCurAddr = dmaStartAddr = fb_base;
438 dmaMaxAddr = static_cast<Addr>(width() * height() * bytesPerPixel()) +
439 dmaCurAddr;
440 frameReadStartTime = curTick();
441 pixelIndex = 0;
442 frameUnderrun = false;
443 fillPixelBuffer();
444
445 // 2. Schedule first pixelclock read; subsequent reads generated as we go
446 Tick firstPixelReadTick = curTick() + pixelClock * (
447 PClksPerLine() * (v_sync.val + 1 +
448 v_back_porch.val + 1) +
449 h_sync.val + 1 +
450 h_back_porch.val + 1);
451 schedule(renderPixelEvent, firstPixelReadTick);
452}
453
454void
455HDLcd::fillPixelBuffer()
456{
457 // - am I under the LCD dma transaction total?
458 // - do I have more data to transfer?
459 // - have I not yet underrun for this frame?
460 // - is there room to put the data in the pixel buffer including any
461 // outstanding dma transfers in flight?
462 while ((dmaPendingNum < maxOutstandingDma()) &&
463 (dmaMaxAddr > dmaCurAddr) &&
464 !frameUnderrun &&
465 bytesFreeInPixelBuffer() > dmaBurstLength() * AXI_PORT_WIDTH) {
466 // try largest transaction size allowed first but switch to smaller
467 // sizes for trailing bytes
468 size_t transaction_size = dmaBurstLength() * AXI_PORT_WIDTH;
469 while (transaction_size > (dmaMaxAddr - dmaCurAddr))
470 transaction_size >>= 1;
471 assert(transaction_size > 0);
472
473 // concurrent dma reads need different dma done events
474 // due to assertion in scheduling state
475 ++dmaPendingNum;
476
477 assert(!dmaDoneEventFree.empty());
478 DmaDoneEvent *event(dmaDoneEventFree.back());
479 dmaDoneEventFree.pop_back();
480 assert(event);
481 assert(!event->scheduled());
482
483 // We use a uncachable request here because the requests from the CPU
484 // will be uncacheable as well. If we have uncacheable and cacheable
485 // requests in the memory system for the same address it won't be
486 // pleased
487 event->setTransactionSize(transaction_size);
488 dmaPort.dmaAction(MemCmd::ReadReq, dmaCurAddr, transaction_size, event,
489 virtualDisplayBuffer + dmaCurAddr - dmaStartAddr,
490 0, Request::UNCACHEABLE);
491 dmaCurAddr += transaction_size;
492 dmaBytesInFlight += transaction_size;
493 }
494}
495
496void
497HDLcd::renderPixel()
498{
499 // try to handle multiple pixels at a time; doing so reduces the accuracy
500 // of the underrun detection but lowers simulation overhead
501 const size_t count = 32;
502 assert(width() % count == 0); // not set up to handle trailing pixels
503
504 // have we underrun on this frame anytime before?
505 if (frameUnderrun) {
506 // the LCD controller gives up on a frame if an underrun occurs and
507 // resumes regular operation on the next frame
508 pixelBufferSize = 0;
509 } else {
510 // did we underrun on this set of pixels?
511 if (pixelBufferSize < bytesPerPixel() * count) {
512 warn("HDLcd controller buffer underrun\n");
513 frameUnderrun = true;
514 int_rawstat.underrun = 1;
515 if (!intEvent.scheduled())
516 schedule(intEvent, clockEdge());
517 } else {
518 // emulate the pixel read from the internal buffer
519 pixelBufferSize -= bytesPerPixel() * count;
520 }
521 }
522
523 // the DMA may have previously stalled due to the buffer being full;
524 // give it a kick; it knows not to fill if at end of frame, underrun, etc
525 if (!fillPixelBufferEvent.scheduled())
526 schedule(fillPixelBufferEvent, clockEdge());
527
528 // schedule the next pixel read according to where it is in the frame
529 pixelIndex += count;
530 assert(pixelIndex <= width() * height());
531 size_t x = pixelIndex % width();
532 Tick nextEventTick = curTick();
533 if (x == 0) {
534 // start of new line
535 nextEventTick += pixelClock * ((h_front_porch.val + 1) +
536 (h_back_porch.val + 1) +
537 (h_sync.val + 1));
538 if (pixelIndex == width() * height()) {
539 // end of frame
540 nextEventTick += PClksPerLine() * (v_front_porch.val + 1) *
541 pixelClock;
542 schedule(endFrameEvent, nextEventTick);
543 return;
544 }
545 } else {
546 nextEventTick += pixelClock * count;
547 }
548
549 schedule(renderPixelEvent, nextEventTick);
550}
551
552void
553HDLcd::endFrame() {
554 assert(pixelBufferSize == 0);
555 assert(dmaPendingNum == 0);
556 assert(dmaBytesInFlight == 0);
557 assert(dmaDoneEventFree.size() == dmaDoneEventAll.size());
558
559 if (vnc)
560 vnc->setDirty();
561
562 if (enableCapture) {
563 if (!pic)
564 pic = simout.create(csprintf("%s.framebuffer.bmp", sys->name()), true);
565
566 assert(bmp);
567 assert(pic);
568 pic->seekp(0);
569 bmp->write(pic);
570 }
571
572 // start the next frame
573 frameUnderway = false;
574 startFrame();
575}
576
577void
578HDLcd::dmaDone(DmaDoneEvent *event)
579{
580 const size_t transactionLength = event->getTransactionSize();
581 assert(pixelBufferSize + transactionLength < PIXEL_BUFFER_CAPACITY);
582 assert(dmaCurAddr <= dmaMaxAddr);
583
584 dmaDoneEventFree.push_back(event);
585 --dmaPendingNum;
586 assert(MAX_OUTSTANDING_DMA_REQ_CAPACITY - dmaDoneEventFree.size() ==
587 dmaPendingNum);
588
589 // add the data to the pixel buffer
590 dmaBytesInFlight -= transactionLength;
591 pixelBufferSize += transactionLength;
592
593 // schedule another dma transaction if:
594 // - we're not done reading the frame
595 // - there is sufficient room in the pixel buffer for another transaction
596 // - another fillPixelBufferEvent is not already scheduled
597 const size_t targetTransSize = dmaBurstLength() * AXI_PORT_WIDTH;
598 if ((dmaCurAddr < dmaMaxAddr) &&
599 (bytesFreeInPixelBuffer() + targetTransSize < PIXEL_BUFFER_CAPACITY) &&
600 !fillPixelBufferEvent.scheduled()) {
601 schedule(fillPixelBufferEvent, clockEdge());
602 }
603}
604
605void
606HDLcd::serialize(std::ostream &os)
607{
608 DPRINTF(HDLcd, "Serializing ARM HDLCD\n");
609
610 const uint32_t version_serial = version;
611 SERIALIZE_SCALAR(version_serial);
612 const uint32_t int_rawstat_serial = int_rawstat;
613 SERIALIZE_SCALAR(int_rawstat_serial);
614 const uint32_t int_clear_serial = int_clear;
615 SERIALIZE_SCALAR(int_clear_serial);
616 const uint32_t int_mask_serial = int_mask;
617 SERIALIZE_SCALAR(int_mask_serial);
618 const uint32_t int_status_serial = int_status;
619 SERIALIZE_SCALAR(int_status_serial);
620
621 SERIALIZE_SCALAR(fb_base);
622 SERIALIZE_SCALAR(fb_line_length);
623
624 const uint32_t fb_line_count_serial = fb_line_count;
625 SERIALIZE_SCALAR(fb_line_count_serial);
626
627 SERIALIZE_SCALAR(fb_line_pitch);
628
629 const uint32_t bus_options_serial = bus_options;
630 SERIALIZE_SCALAR(bus_options_serial);
631 const uint32_t v_sync_serial = v_sync;
632 SERIALIZE_SCALAR(v_sync_serial);
633 const uint32_t v_back_porch_serial = v_back_porch;
634 SERIALIZE_SCALAR(v_back_porch_serial);
635 const uint32_t v_data_serial = v_data;
636 SERIALIZE_SCALAR(v_data_serial);
637 const uint32_t v_front_porch_serial = v_front_porch;
638 SERIALIZE_SCALAR(v_front_porch_serial);
639 const uint32_t h_sync_serial = h_sync;
640 SERIALIZE_SCALAR(h_sync_serial);
641 const uint32_t h_back_porch_serial = h_back_porch;
642 SERIALIZE_SCALAR(h_back_porch_serial);
643 const uint32_t h_data_serial = h_data;
644 SERIALIZE_SCALAR(h_data_serial);
645 const uint32_t h_front_porch_serial = h_front_porch;
646 SERIALIZE_SCALAR(h_front_porch_serial);
647 const uint32_t polarities_serial = polarities;
648 SERIALIZE_SCALAR(polarities_serial);
649 const uint32_t command_serial = command;
650 SERIALIZE_SCALAR(command_serial);
651 const uint32_t pixel_format_serial = pixel_format;
652 SERIALIZE_SCALAR(pixel_format_serial);
653 const uint32_t red_select_serial = red_select;
654 SERIALIZE_SCALAR(red_select_serial);
655 const uint32_t green_select_serial = green_select;
656 SERIALIZE_SCALAR(green_select_serial);
657 const uint32_t blue_select_serial = blue_select;
658 SERIALIZE_SCALAR(blue_select_serial);
659
660 SERIALIZE_SCALAR(frameReadStartTime);
661 SERIALIZE_SCALAR(dmaStartAddr);
662 SERIALIZE_SCALAR(dmaCurAddr);
663 SERIALIZE_SCALAR(dmaMaxAddr);
664 SERIALIZE_SCALAR(dmaPendingNum);
665 SERIALIZE_SCALAR(frameUnderrun);
666
667 const size_t buffer_size = bytesPerPixel() * width() * height();
668 SERIALIZE_ARRAY(virtualDisplayBuffer, buffer_size);
669
670 SERIALIZE_SCALAR(pixelBufferSize);
671 SERIALIZE_SCALAR(pixelIndex);
672 SERIALIZE_SCALAR(doUpdateParams);
673 SERIALIZE_SCALAR(frameUnderway);
674 SERIALIZE_SCALAR(dmaBytesInFlight);
675
676 Tick start_event_time = 0;
677 Tick end_event_time = 0;
678 Tick render_pixel_event_time = 0;
679 Tick fill_pixel_buffer_event_time = 0;
680 Tick int_event_time = 0;
681 if (startFrameEvent.scheduled())
682 start_event_time = startFrameEvent.when();
683 if (endFrameEvent.scheduled())
684 end_event_time = endFrameEvent.when();
685 if (renderPixelEvent.scheduled())
686 render_pixel_event_time = renderPixelEvent.when();
687 if (fillPixelBufferEvent.scheduled())
688 fill_pixel_buffer_event_time = fillPixelBufferEvent.when();
689 if (intEvent.scheduled())
690 int_event_time = intEvent.when();
691 SERIALIZE_SCALAR(start_event_time);
692 SERIALIZE_SCALAR(end_event_time);
693 SERIALIZE_SCALAR(render_pixel_event_time);
694 SERIALIZE_SCALAR(fill_pixel_buffer_event_time);
695 SERIALIZE_SCALAR(int_event_time);
696
697 vector<Tick> dma_done_event_tick(MAX_OUTSTANDING_DMA_REQ_CAPACITY);
698 vector<size_t> dma_done_event_burst_len(MAX_OUTSTANDING_DMA_REQ_CAPACITY);
699 for (int x = 0; x < MAX_OUTSTANDING_DMA_REQ_CAPACITY; ++x) {
700 dma_done_event_tick[x] = dmaDoneEventAll[x].scheduled() ?
701 dmaDoneEventAll[x].when() : 0;
702 dma_done_event_burst_len[x] = dmaDoneEventAll[x].scheduled() ?
703 dmaDoneEventAll[x].getTransactionSize() : 0;
704 }
705 arrayParamOut(os, "dma_done_event_tick", dma_done_event_tick);
706 arrayParamOut(os, "dma_done_event_burst_length", dma_done_event_burst_len);
707}
708
709void
710HDLcd::unserialize(Checkpoint *cp, const std::string &section)
711{
712 uint32_t version_serial, int_rawstat_serial, int_clear_serial,
713 int_mask_serial, int_status_serial, fb_line_count_serial,
714 bus_options_serial, v_sync_serial, v_back_porch_serial,
715 v_data_serial, v_front_porch_serial, h_sync_serial,
716 h_back_porch_serial, h_data_serial, h_front_porch_serial,
717 polarities_serial, command_serial, pixel_format_serial,
718 red_select_serial, green_select_serial, blue_select_serial;
719
720 DPRINTF(HDLcd, "Unserializing ARM HDLCD\n");
721
722 UNSERIALIZE_SCALAR(version_serial);
723 version = version_serial;
724 UNSERIALIZE_SCALAR(int_rawstat_serial);
725 int_rawstat = int_rawstat_serial;
726 UNSERIALIZE_SCALAR(int_clear_serial);
727 int_clear = int_clear_serial;
728 UNSERIALIZE_SCALAR(int_mask_serial);
729 int_mask = int_mask_serial;
730 UNSERIALIZE_SCALAR(int_status_serial);
731 int_status = int_status_serial;
732
733 UNSERIALIZE_SCALAR(fb_base);
734 UNSERIALIZE_SCALAR(fb_line_length);
735
736 UNSERIALIZE_SCALAR(fb_line_count_serial);
737 fb_line_count = fb_line_count_serial;
738
739 UNSERIALIZE_SCALAR(fb_line_pitch);
740
741 UNSERIALIZE_SCALAR(bus_options_serial);
742 bus_options = bus_options_serial;
743 UNSERIALIZE_SCALAR(v_sync_serial);
744 v_sync = v_sync_serial;
745 UNSERIALIZE_SCALAR(v_back_porch_serial);
746 v_back_porch = v_back_porch_serial;
747 UNSERIALIZE_SCALAR(v_data_serial);
748 v_data = v_data_serial;
749 UNSERIALIZE_SCALAR(v_front_porch_serial);
750 v_front_porch = v_front_porch_serial;
751 UNSERIALIZE_SCALAR(h_sync_serial);
752 h_sync = h_sync_serial;
753 UNSERIALIZE_SCALAR(h_back_porch_serial);
754 h_back_porch = h_back_porch_serial;
755 UNSERIALIZE_SCALAR(h_data_serial);
756 h_data = h_data_serial;
757 UNSERIALIZE_SCALAR(h_front_porch_serial);
758 h_front_porch = h_front_porch_serial;
759 UNSERIALIZE_SCALAR(polarities_serial);
760 polarities = polarities_serial;
761 UNSERIALIZE_SCALAR(command_serial);
762 command = command_serial;
763 UNSERIALIZE_SCALAR(pixel_format_serial);
764 pixel_format = pixel_format_serial;
765 UNSERIALIZE_SCALAR(red_select_serial);
766 red_select = red_select_serial;
767 UNSERIALIZE_SCALAR(green_select_serial);
768 green_select = green_select_serial;
769 UNSERIALIZE_SCALAR(blue_select_serial);
770 blue_select = blue_select_serial;
771
772 UNSERIALIZE_SCALAR(frameReadStartTime);
773 UNSERIALIZE_SCALAR(dmaStartAddr);
774 UNSERIALIZE_SCALAR(dmaCurAddr);
775 UNSERIALIZE_SCALAR(dmaMaxAddr);
776 UNSERIALIZE_SCALAR(dmaPendingNum);
777 UNSERIALIZE_SCALAR(frameUnderrun);
778 UNSERIALIZE_SCALAR(dmaBytesInFlight);
779
780 const size_t buffer_size = bytesPerPixel() * width() * height();
781 virtualDisplayBuffer = new uint8_t[buffer_size];
782 UNSERIALIZE_ARRAY(virtualDisplayBuffer, buffer_size);
783
784 UNSERIALIZE_SCALAR(pixelBufferSize);
785 UNSERIALIZE_SCALAR(pixelIndex);
786 UNSERIALIZE_SCALAR(doUpdateParams);
787 UNSERIALIZE_SCALAR(frameUnderway);
788
789 Tick start_event_time = 0;
790 Tick end_event_time = 0;
791 Tick render_pixel_event_time = 0;
792 Tick fill_pixel_buffer_event_time = 0;
793 Tick int_event_time = 0;
794 UNSERIALIZE_SCALAR(start_event_time);
795 UNSERIALIZE_SCALAR(end_event_time);
796 UNSERIALIZE_SCALAR(render_pixel_event_time);
797 UNSERIALIZE_SCALAR(fill_pixel_buffer_event_time);
798 UNSERIALIZE_SCALAR(int_event_time);
799 if (start_event_time)
800 schedule(startFrameEvent, start_event_time);
801 if (end_event_time)
802 schedule(endFrameEvent, end_event_time);
803 if (render_pixel_event_time)
804 schedule(renderPixelEvent, render_pixel_event_time);
805 if (fill_pixel_buffer_event_time)
806 schedule(fillPixelBufferEvent, fill_pixel_buffer_event_time);
807 if (int_event_time)
808 schedule(intEvent, int_event_time);
809
810 vector<Tick> dma_done_event_tick(MAX_OUTSTANDING_DMA_REQ_CAPACITY);
811 vector<Tick> dma_done_event_burst_len(MAX_OUTSTANDING_DMA_REQ_CAPACITY);
812 arrayParamIn(cp, section, "dma_done_event_tick", dma_done_event_tick);
813 arrayParamIn(cp, section, "dma_done_event_burst_length", dma_done_event_burst_len);
814 dmaDoneEventFree.clear();
815 for (int x = 0; x < MAX_OUTSTANDING_DMA_REQ_CAPACITY; ++x) {
816 if (dma_done_event_tick[x]) {
817 dmaDoneEventAll[x].setTransactionSize(dma_done_event_burst_len[x]);
818 schedule(dmaDoneEventAll[x], dma_done_event_tick[x]);
819 } else
820 dmaDoneEventFree.push_back(&dmaDoneEventAll[x]);
821 }
822 assert(MAX_OUTSTANDING_DMA_REQ_CAPACITY - dmaDoneEventFree.size() == dmaPendingNum);
823
824 if (frameUnderway) {
825 updateVideoParams(true);
826 if (vnc)
827 vnc->setDirty();
828 }
829}
830
831void
832HDLcd::generateInterrupt()
833{
834 int_status = int_rawstat & int_mask;
835 DPRINTF(HDLcd, "Generate Interrupt: int_rawstat=0x%08x int_mask=0x%08x "
836 "int_status=0x%08x\n",
837 (uint32_t)int_rawstat, (uint32_t)int_mask, (uint32_t)int_status);
838
839 if (int_status != 0) {
840 gic->sendInt(intNum);
841 DPRINTF(HDLcd, " -- Generated\n");
842 }
843}
844
845AddrRangeList
846HDLcd::getAddrRanges() const
847{
848 AddrRangeList ranges;
849 ranges.push_back(RangeSize(pioAddr, pioSize));
850 return ranges;
851}
852
853HDLcd *
854HDLcdParams::create()
855{
856 return new HDLcd(this);
857}