hdlcd.hh revision 10839
1/*
2 * Copyright (c) 2010-2013, 2015 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
41/** @file
42 * Implementiation of the ARM HDLcd controller.
43 *
44 * This implementation aims to have sufficient detail such that underrun
45 * conditions are reasonable / behave similar to reality.  There are two
46 * 'engines' going at once.  First, the DMA engine running at LCD clock
47 * frequency is responsible for filling the controller's internal buffer.
48 * The second engine runs at the pixel clock frequency and reads the pixels
49 * out of the internal buffer.  The pixel rendering engine uses front / back
50 * porch and sync delays between lines and frames.
51 *
52 * If the pixel rendering engine does not have a pixel to display, it will
53 * cause an underrun event.  The HDLcd controller, per spec, will stop
54 * issuing DMA requests for the rest of the frame and resume normal behavior
55 * on the subsequent frame.  What pixels are rendered upon an underrun
56 * condition is different than the real hardware; while the user will see
57 * artifacts (previous frame mixed with current frame), it is not the same
58 * behavior as real hardware which repeats the last pixel value for the rest
59 * of the current frame.  This compromise was made to save on memory and
60 * complexity and assumes that it is not important to accurately model the
61 * content of an underrun frame.
62 *
63 * KNOWN ISSUES
64 * 1.  The default kernel driver used in testing sets the line count to one
65 * less than the expected 768.  However, it also sets the v_count to 767.
66 * The controller specifies that 1 should be added to v_count but does not
67 * specify adding 1 to the line count.  The driver is probably wrong.
68 * However, to sync these two numbers up, this model uses fb_line_count and
69 * fb_line_length rather than using v_data or h_data values to determine the
70 * width and height of the frame; those values are ignored.
71 * 2.  The HDLcd is implemented here as an AmbaDmaDevice, but it doesn't have
72 * an AMBA ID as far as I know.  That is the only bit of the AmbaDmaDevice
73 * interface that is irrelevant to it, so a fake AMBA ID is used for now.
74 * I didn't think inserting an extra layer of hierachy between AmbaDmaDevice
75 * and DmaDevice would be helpful to anyone else, but that may be the right
76 * answer.
77 * 3.  The internal buffer size is either 1 or 2 KB depending on which
78 * specification is referenced for the different Versatile Express tiles.
79 * This implementation uses the larger 2 KB buffer by default.
80 */
81
82#ifndef __DEV_ARM_HDLCD_HH__
83#define __DEV_ARM_HDLCD_HH__
84
85#include <fstream>
86#include <memory>
87
88#include "base/bitmap.hh"
89#include "base/framebuffer.hh"
90#include "dev/arm/amba_device.hh"
91#include "params/HDLcd.hh"
92#include "sim/serialize.hh"
93
94class VncInput;
95
96class HDLcd: public AmbaDmaDevice
97{
98  protected:
99    /** fake AMBA ID -- unused */
100    static const uint64_t AMBA_ID       = ULL(0xb105f00d00141000);
101
102    /** ARM HDLcd register offsets */
103    enum RegisterOffset {
104        Version          = 0x0000,
105        Int_RawStat      = 0x0010,
106        Int_Clear        = 0x0014,
107        Int_Mask         = 0x0018,
108        Int_Status       = 0x001C,
109        Fb_Base          = 0x0100,
110        Fb_Line_Length   = 0x0104,
111        Fb_Line_Count    = 0x0108,
112        Fb_Line_Pitch    = 0x010C,
113        Bus_Options      = 0x0110,
114        V_Sync           = 0x0200,
115        V_Back_Porch     = 0x0204,
116        V_Data           = 0x0208,
117        V_Front_Porch    = 0x020C,
118        H_Sync           = 0x0210,
119        H_Back_Porch     = 0x0214,
120        H_Data           = 0x0218,
121        H_Front_Porch    = 0x021C,
122        Polarities       = 0x0220,
123        Command          = 0x0230,
124        Pixel_Format     = 0x0240,
125        Red_Select       = 0x0244,
126        Green_Select     = 0x0248,
127        Blue_Select      = 0x024C };
128
129    /** Reset value for Bus_Options register */
130    static const size_t BUS_OPTIONS_RESETV = 0x408;
131
132    /** Reset value for Version register */
133    static const size_t VERSION_RESETV = 0x1CDC0000;
134
135    /** max number of outstanding DMA requests possible */
136    static const size_t MAX_OUTSTANDING_DMA_REQ_CAPACITY = 16;
137
138    /** max number of beats delivered in one dma burst */
139    static const size_t MAX_BURST_LEN = 16;
140
141    /** size of internal buffer in bytes */
142    static const size_t PIXEL_BUFFER_CAPACITY = 2048;
143
144    /** AXI port width in bytes */
145    static const size_t AXI_PORT_WIDTH = 8;
146
147    static const size_t MAX_BURST_SIZE = MAX_BURST_LEN * AXI_PORT_WIDTH;
148
149    /**
150     * @name RegisterFieldLayouts
151     * Bit layout declarations for multi-field registers.
152     */
153    /**@{*/
154    BitUnion32(VersionReg)
155        Bitfield<7,0>   version_minor;
156        Bitfield<15,8>  version_major;
157        Bitfield<31,16> product_id;
158    EndBitUnion(VersionReg)
159
160    BitUnion32(InterruptReg)
161        Bitfield<0> dma_end;
162        Bitfield<1> bus_error;
163        Bitfield<2> vsync;
164        Bitfield<3> underrun;
165    EndBitUnion(InterruptReg)
166
167    BitUnion32(FbLineCountReg)
168        Bitfield<11,0>  fb_line_count;
169        Bitfield<31,12> reserved_31_12;
170    EndBitUnion(FbLineCountReg)
171
172    BitUnion32(BusOptsReg)
173        Bitfield<4,0>   burst_len;
174        Bitfield<7,5>   reserved_7_5;
175        Bitfield<11,8>  max_outstanding;
176        Bitfield<31,12> reserved_31_12;
177    EndBitUnion(BusOptsReg)
178
179    BitUnion32(TimingReg)
180        Bitfield<11,0>  val;
181        Bitfield<31,12> reserved_31_12;
182    EndBitUnion(TimingReg)
183
184    BitUnion32(PolaritiesReg)
185        Bitfield<0>    vsync_polarity;
186        Bitfield<1>    hsync_polarity;
187        Bitfield<2>    dataen_polarity;
188        Bitfield<3>    data_polarity;
189        Bitfield<4>    pxlclk_polarity;
190        Bitfield<31,5> reserved_31_5;
191    EndBitUnion(PolaritiesReg)
192
193    BitUnion32(CommandReg)
194        Bitfield<0>    enable;
195        Bitfield<31,1> reserved_31_1;
196    EndBitUnion(CommandReg)
197
198    BitUnion32(PixelFormatReg)
199        Bitfield<2,0>  reserved_2_0;
200        Bitfield<4,3>  bytes_per_pixel;
201        Bitfield<30,5> reserved_30_5;
202        Bitfield<31>   big_endian;
203    EndBitUnion(PixelFormatReg)
204
205    BitUnion32(ColorSelectReg)
206        Bitfield<4,0>   offset;
207        Bitfield<7,5>   reserved_7_5;
208        Bitfield<11,8>  size;
209        Bitfield<15,12> reserved_15_12;
210        Bitfield<23,16> default_color;
211        Bitfield<31,24> reserved_31_24;
212    EndBitUnion(ColorSelectReg)
213    /**@}*/
214
215    /**
216     * @name HDLCDRegisters
217     * HDLCD register contents.
218     */
219    /**@{*/
220    VersionReg version;             /**< Version register */
221    InterruptReg int_rawstat;       /**< Interrupt raw status register */
222    InterruptReg int_clear;         /**< Interrupt clear register */
223    InterruptReg int_mask;          /**< Interrupt mask register */
224    InterruptReg int_status;        /**< Interrupt status register */
225    uint32_t fb_base;               /**< Frame buffer base address register */
226    uint32_t fb_line_length;        /**< Frame buffer Line length register */
227    FbLineCountReg fb_line_count;   /**< Frame buffer Line count register */
228    uint32_t fb_line_pitch;         /**< Frame buffer Line pitch register */
229    BusOptsReg bus_options;         /**< Bus options register */
230    TimingReg v_sync;               /**< Vertical sync width register */
231    TimingReg v_back_porch;         /**< Vertical back porch width register */
232    TimingReg v_data;               /**< Vertical data width register */
233    TimingReg v_front_porch;        /**< Vertical front porch width register */
234    TimingReg h_sync;               /**< Horizontal sync width register */
235    TimingReg h_back_porch;         /**< Horizontal back porch width register */
236    TimingReg h_data;               /**< Horizontal data width register */
237    TimingReg h_front_porch;        /**< Horizontal front porch width reg */
238    PolaritiesReg polarities;       /**< Polarities register */
239    CommandReg command;             /**< Command register */
240    PixelFormatReg pixel_format;    /**< Pixel format register */
241    ColorSelectReg red_select;      /**< Red color select register */
242    ColorSelectReg green_select;    /**< Green color select register */
243    ColorSelectReg blue_select;     /**< Blue color select register */
244    /** @} */
245
246    /** Pixel clock period */
247    const Tick pixelClock;
248
249    FrameBuffer fb;
250
251    /** VNC server */
252    VncInput *vnc;
253
254    /** Helper to write out bitmaps */
255    Bitmap bmp;
256
257    /** Picture of what the current frame buffer looks like */
258    std::ostream *pic;
259
260    /**
261     * Event wrapper for dmaDone()
262     *
263     * This event call pushes its this pointer onto the freeDoneEvent vector
264     * and calls dmaDone() when triggered.  While most of the time the burst
265     * length of a transaction will be the max burst length set by the driver,
266     * any trailing bytes must be handled with smaller lengths thus requiring
267     * the configurable burst length option.
268     */
269    class DmaDoneEvent : public Event
270    {
271      private:
272        /** Reference to HDLCD that issued the corresponding DMA transaction */
273        HDLcd &obj;
274
275        /** Transaction size */
276        size_t transSize;
277
278      public:
279        /**
280         * Constructor.
281         *
282         * @param _obj HDLCD that issued the corresponding DMA transaction
283         */
284        DmaDoneEvent(HDLcd *_obj)
285            : Event(), obj(*_obj), transSize(0) {}
286
287        /**
288         * Sets the size of this transaction.
289         *
290         * @param len size of the transaction in bytes
291         */
292        void setTransactionSize(size_t len) {
293            transSize = len;
294        }
295
296        /**
297         * Gets the size of this transaction.
298         *
299         * @return size of this transaction in bytes
300         */
301        size_t getTransactionSize() const {
302            return transSize;
303        }
304
305        void process() {
306            obj.dmaDone(this);
307        }
308
309        const std::string name() const {
310            return obj.name() + ".DmaDoneEvent";
311        }
312    };
313
314    /** Start time for frame buffer dma read */
315    Tick frameReadStartTime;
316
317    /** Starting address for the current frame */
318    Addr dmaStartAddr;
319
320    /** Next address the dma should read from */
321    Addr dmaCurAddr;
322
323    /** One byte past the address of the last byte the dma should read
324      * from */
325    Addr dmaMaxAddr;
326
327    /** Number of pending dma reads */
328    size_t dmaPendingNum;
329
330    /** Flag indicating whether current frame has underrun */
331    bool frameUnderrun;
332
333    /** HDLcd virtual display buffer */
334    std::vector<uint8_t> virtualDisplayBuffer;
335
336    /** Size of the pixel buffer */
337    size_t pixelBufferSize;
338
339    /** Index of the next pixel to render */
340    size_t pixelIndex;
341
342    /** Flag indicating whether video parameters need updating */
343    bool doUpdateParams;
344
345    /** Flag indicating whether a frame read / display is in progress */
346    bool frameUnderway;
347
348    /**
349     * Number of bytes in flight from DMA that have not reached the pixel
350     * buffer yet
351     */
352    uint32_t dmaBytesInFlight;
353
354    /**
355     * Gets the number of oustanding DMA transactions allowed on the bus at a
356     * time.
357     *
358     * @return gets the driver-specified number of outstanding DMA transactions
359     *         from the hdlcd controller that are allowed on the bus at a time
360     */
361    inline uint16_t maxOutstandingDma() const {
362        return bus_options.max_outstanding;
363    }
364
365    /**
366     * Gets the number of bytes free in the pixel buffer.
367     *
368     * @return number of bytes free in the internal pixel buffer
369     */
370    inline uint32_t bytesFreeInPixelBuffer() const {
371        return PIXEL_BUFFER_CAPACITY - (pixelBufferSize + dmaBytesInFlight);
372    }
373
374    /**
375     * Gets the number of beats-per-burst for bus transactions.
376     *
377     * @return number of beats-per-burst per HDLcd DMA transaction
378     */
379    inline size_t dmaBurstLength() const {
380        assert(bus_options.burst_len <= MAX_BURST_LEN);
381        return bus_options.burst_len;
382    }
383
384    /**
385     * Gets the number of bytes per pixel.
386     *
387     * @return bytes per pixel
388     */
389    inline size_t bytesPerPixel() const {
390        return pixel_format.bytes_per_pixel + 1;
391    }
392
393    /**
394     * Gets frame buffer width.
395     *
396     * @return frame buffer width (pixels per line)
397     */
398    inline size_t width() const {
399        return fb_line_length / bytesPerPixel();
400    }
401
402    /**
403     * Gets frame buffer height.
404     *
405     * @return frame buffer height (lines per panel)
406     */
407    inline size_t height() const {
408        return fb_line_count.fb_line_count;
409    }
410
411    inline size_t area() const { return height() * width(); }
412
413    /**
414     * Gets the total number of pixel clocks per display line.
415     *
416     * @return number of pixel clocks per display line including porch delays
417     *         and horizontal sync time
418     */
419    inline uint64_t PClksPerLine() const {
420        return h_back_porch.val + 1 +
421               h_data.val + 1 +
422               h_front_porch.val + 1 +
423               h_sync.val + 1;
424    }
425
426    /** Send updated parameters to the vnc server */
427    void updateVideoParams(bool unserializing);
428
429    /** Generates an interrupt */
430    void generateInterrupt();
431
432    /** Start reading the next frame */
433    void startFrame();
434
435    /** End of frame reached */
436    void endFrame();
437
438    /** Generate DMA read requests from frame buffer into pixel buffer */
439    void fillPixelBuffer();
440
441    /** DMA done event */
442    void dmaDone(DmaDoneEvent *event);
443
444    /** Called when it is time to render a pixel */
445    void renderPixel();
446
447    PixelConverter pixelConverter() const;
448
449    /** Start of frame event */
450    EventWrapper<HDLcd, &HDLcd::startFrame> startFrameEvent;
451
452    /** End of frame event */
453    EventWrapper<HDLcd, &HDLcd::endFrame> endFrameEvent;
454
455    /** Pixel render event */
456    EventWrapper<HDLcd, &HDLcd::renderPixel> renderPixelEvent;
457
458    /** Fill fifo */
459    EventWrapper<HDLcd, &HDLcd::fillPixelBuffer> fillPixelBufferEvent;
460
461    /** Wrapper to create an event out of the interrupt */
462    EventWrapper<HDLcd, &HDLcd::generateInterrupt> intEvent;
463
464    /**@{*/
465    /**
466     * All pre-allocated DMA done events
467     *
468     * The HDLCD model preallocates maxOutstandingDma() number of
469     * DmaDoneEvents to avoid having to heap allocate every single
470     * event when it is needed. In order to keep track of which events
471     * are in flight and which are ready to be used, we use two
472     * different vectors. dmaDoneEventAll contains <i>all</i>
473     * DmaDoneEvents that the object may use, while dmaDoneEventFree
474     * contains a list of currently <i>unused</i> events. When an
475     * event needs to be scheduled, the last element of the
476     * dmaDoneEventFree is used and removed from the list. When an
477     * event fires, it is added to the end of the
478     * dmaEventFreeList. dmaDoneEventAll is never used except for in
479     * initialization and serialization.
480     */
481    std::vector<DmaDoneEvent> dmaDoneEventAll;
482
483    /** Unused DMA done events that are ready to be scheduled */
484    std::vector<DmaDoneEvent *> dmaDoneEventFree;
485    /**@}*/
486
487    bool enableCapture;
488
489  public:
490    typedef HDLcdParams Params;
491
492    const Params *
493    params() const
494    {
495        return dynamic_cast<const Params *>(_params);
496    }
497    HDLcd(const Params *p);
498    ~HDLcd();
499
500    virtual Tick read(PacketPtr pkt);
501    virtual Tick write(PacketPtr pkt);
502
503    virtual void serialize(std::ostream &os);
504    virtual void unserialize(Checkpoint *cp, const std::string &section);
505
506    /**
507     * Determine the address ranges that this device responds to.
508     *
509     * @return a list of non-overlapping address ranges
510     */
511    AddrRangeList getAddrRanges() const;
512};
513
514#endif
515