1/***************************************************************************
2
3Copyright 2001 Intel Corporation.  All Rights Reserved.
4
5Permission is hereby granted, free of charge, to any person obtaining a
6copy of this software and associated documentation files (the
7"Software"), to deal in the Software without restriction, including
8without limitation the rights to use, copy, modify, merge, publish,
9distribute, sub license, and/or sell copies of the Software, and to
10permit persons to whom the Software is furnished to do so, subject to
11the following conditions:
12
13The above copyright notice and this permission notice (including the
14next paragraph) shall be included in all copies or substantial portions
15of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
20IN NO EVENT SHALL INTEL, AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
21DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
22OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
23THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
25**************************************************************************/
26
27/***************************************************************************
28 * libI810XvMC.h: MC Driver i810 includes
29 *
30 * Authors:
31 *      Matt Sottek <matthew.j.sottek@intel.com>
32 *
33 *
34 ***************************************************************************/
35
36
37#ifndef I810XVMC_H
38#define I810XVMC_H
39
40/* #define XVMC_DEBUG(x) do {x; }while(0); */
41#define XVMC_DEBUG(x)
42
43#include <stdint.h>
44#include "xf86drm.h"
45#include "../i810_common.h"
46#include <X11/Xlibint.h>
47
48
49/***************************************************************************
50// i810OverlayRec: Structure that is used to reference the overlay
51//  register memory. A i810OverlayRecPtr is set to the address of the
52//  allocated overlay registers.
53***************************************************************************/
54typedef struct _i810OverlayRec {
55  unsigned int OBUF_0Y;
56  unsigned int OBUF_1Y;
57  unsigned int OBUF_0U;
58  unsigned int OBUF_0V;
59  unsigned int OBUF_1U;
60  unsigned int OBUF_1V;
61  unsigned int OV0STRIDE;
62  unsigned int YRGB_VPH;
63  unsigned int UV_VPH;
64  unsigned int HORZ_PH;
65  unsigned int INIT_PH;
66  unsigned int DWINPOS;
67  unsigned int DWINSZ;
68  unsigned int SWID;
69  unsigned int SWIDQW;
70  unsigned int SHEIGHT;
71  unsigned int YRGBSCALE;
72  unsigned int UVSCALE;
73  unsigned int OV0CLRC0;
74  unsigned int OV0CLRC1;
75  unsigned int DCLRKV;
76  unsigned int DCLRKM;
77  unsigned int SCLRKVH;
78  unsigned int SCLRKVL;
79  unsigned int SCLRKM;
80  unsigned int OV0CONF;
81  unsigned int OV0CMD;
82} i810OverlayRec, *i810OverlayRecPtr;
83
84
85/***************************************************************************
86// i810XvMCDrmMap: Holds the data about the DRM maps
87***************************************************************************/
88typedef struct _i810XvMCDrmMap {
89  drm_handle_t offset;
90  drmAddress address;
91  unsigned int size;
92} i810XvMCDrmMap, *i810XvMCDrmMapPtr;
93
94/***************************************************************************
95// i810XvMCContext: Private Context data referenced via the privData
96//  pointer in the XvMCContext structure.
97***************************************************************************/
98typedef struct _i810XvMCContext {
99  int fd;   /* File descriptor for /dev/dri */
100  i810XvMCDrmMap overlay;
101  i810XvMCDrmMap surfaces;
102  drmBufMapPtr dmabufs;  /* Data structure to hold available dma buffers */
103  drm_context_t drmcontext;
104  unsigned int last_render;
105  unsigned int last_flip;
106  unsigned short ref;
107  unsigned short current;
108  int lock;   /* Lightweight lock to avoid locking twice */
109  char busIdString[10]; /* PCI:0:1:0 or PCI:0:2:0 */
110  i810OverlayRecPtr oregs;
111  unsigned int dual_prime; /* Flag to identify when dual prime is in use. */
112  unsigned int fb_base;
113  Atom xv_colorkey;
114  Atom xv_brightness;
115  Atom xv_contrast;
116  Atom xv_saturation;
117  int brightness;
118  int saturation;
119  int contrast;
120  int colorkey;
121} i810XvMCContext;
122
123/***************************************************************************
124// i810XvMCSurface: Private data structure for each XvMCSurface. This
125//  structure is referenced by the privData pointer in the XvMCSurface
126//  structure.
127***************************************************************************/
128typedef struct _i810XvMCSurface {
129  unsigned int pitch;
130  unsigned int dbi1y;  /* Destination buffer info command word 1 for Y */
131  unsigned int dbi1u; /* Destination buffer info command word 1 for U */
132  unsigned int dbi1v; /* Destination buffer info command word 1 for V */
133  unsigned int dbv1;  /* Destination buffer variables command word 1 */
134  unsigned int mi1y;   /* Map Info command word 1 (Minus bit 28) for Y */
135  unsigned int mi1u;   /* Map Info command word 1 (Minus bit 28) for U */
136  unsigned int mi1v;   /* Map Info command word 1 (Minus bit 28) for V */
137  unsigned int mi2y;   /* Map info command word 2 for y */
138  unsigned int mi2u;   /* Map info command word 2 for y */
139  unsigned int mi2v;   /* Map info command word 2 for y */
140  unsigned int mi3y;   /* Map info command word 3 */
141  unsigned int mi3u;   /* Map info command word 3 */
142  unsigned int mi3v;   /* Map info command word 3 */
143  unsigned int last_render;
144  unsigned int last_flip;
145  unsigned int second_field; /* Flags a surface that is only half done */
146  drmAddress data;
147  drm_handle_t offset;
148  unsigned int offsets[3];
149  i810XvMCContext *privContext;
150} i810XvMCSurface;
151
152/***************************************************************************
153// i810XvMCSubpicture: Private data structure for each XvMCSubpicture. This
154//  structure is referenced by the privData pointer in the XvMCSubpicture
155//  structure.
156***************************************************************************/
157typedef struct _i810XvMCSubpicture {
158  unsigned int pitch;
159  unsigned int dbi1;  /* Destination buffer info command word 1 */
160  unsigned int dbv1;  /* Destination buffer variables command word */
161  unsigned int mi1;   /* Map Info command word 1 (Minus bit 28) */
162  unsigned int mi2;   /* Map info command word 2 */
163  unsigned int mi3;   /* Map info command word 3 */
164  unsigned int last_render;
165  unsigned int last_flip;
166  drmAddress data;
167  drm_handle_t offset;
168  unsigned int offsets[1];
169  unsigned char palette[3][16];
170  i810XvMCContext *privContext;
171} i810XvMCSubpicture;
172
173typedef struct _Box {
174  int x1,y1,x2,y2;
175} Box, *BoxPtr;
176
177/***************************************************************************
178// drm_i810_overlay_t: Structure returned by overlay info ioctl.
179// NOTE: If you change this structure you will have to change the equiv.
180//  structure in the kernel.
181***************************************************************************/
182typedef struct _drm_i810_overlay_t {
183  unsigned int offset;
184  unsigned int physical;
185} drm_i810_overlay_t;
186
187/***************************************************************************
188// drm_i810_dma_t: Structure used by dma allocation ioctl.
189// NOTE: If you change this structure you will have to change the equiv.
190//  structure in the kernel.
191***************************************************************************/
192typedef struct _drm_i810_dma {
193  void *virtual;
194  int request_idx;
195  int request_size;
196  int granted;
197} drm_i810_dma_t;
198
199/***************************************************************************
200// drm_i810_mc_t: Structure used by mc dispatch ioctl.
201// NOTE: If you change this structure you will have to change the equiv.
202//  structure in the kernel.
203***************************************************************************/
204typedef struct _drm_i810_mc {
205  int idx;		/* buffer index */
206  int used;		/* nr bytes in use */
207  int num_blocks;         /* number of GFXBlocks */
208  int *length;	        /* List of lengths for GFXBlocks */
209  unsigned int last_render; /* Last render request */
210} drm_i810_mc_t;
211
212/* Subpicture fourcc */
213#define FOURCC_IA44 0x34344149
214
215/* Static Parameters */
216#define I810_XVMC_MAXWIDTH  720
217#define I810_XVMC_MAXHEIGHT  576
218#define I810_DEFAULT16_COLORKEY 31
219#define I810_DMA_BUF_NR 256
220
221/* COMMANDS */
222#define CMD_FLUSH        ((4<<23) | 0x1)
223#define BOOLEAN_ENA_1    ((3<<29) | (3<<24) | (3<<2))
224#define BOOLEAN_ENA_2    ((3<<29) | (4<<24) | (3<<16) | (1<<3) | (1<<2))
225#define DEST_BUFFER_INFO (0x15<<23)
226#define DEST_BUFFER_VAR  ((0x3<<29) | (0x1d<<24) | (0x85<<16))
227#define DRAWING_RECT_INFO ((3<<29) | (0x1d<<24) | (0x80<<16) | 3)
228#define GFXBLOCK         ((0x3<<29) | (0x1e<<24))
229#define CMD_MAP_INFO     ((0x3<<29) | (0x1d<<24) | 0x2)
230#define MAP_PALETTE_LOAD ((3<<29) | (0x1d<<24) | (0x82<<16) | 0xff)
231#define VERTEX_FORMAT    ((3<<29) | (0x5<<24))
232#define SRC_DEST_BLEND_MONO ((3<<29) | (8<<24))
233
234/* Bit Patterns */
235
236/*
237 * OV0CMD - Overlay Command Register
238 */
239#define VERTICAL_CHROMINANCE_FILTER     0x70000000
240#define VC_SCALING_OFF          0x00000000
241#define VC_LINE_REPLICATION     0x10000000
242#define VC_UP_INTERPOLATION     0x20000000
243#define VC_PIXEL_DROPPING       0x50000000
244#define VC_DOWN_INTERPOLATION   0x60000000
245#define VERTICAL_LUMINANCE_FILTER       0x0E000000
246#define VL_SCALING_OFF          0x00000000
247#define VL_LINE_REPLICATION     0x02000000
248#define VL_UP_INTERPOLATION     0x04000000
249#define VL_PIXEL_DROPPING       0x0A000000
250#define VL_DOWN_INTERPOLATION   0x0C000000
251#define HORIZONTAL_CHROMINANCE_FILTER   0x01C00000
252#define HC_SCALING_OFF          0x00000000
253#define HC_LINE_REPLICATION     0x00400000
254#define HC_UP_INTERPOLATION     0x00800000
255#define HC_PIXEL_DROPPING       0x01400000
256#define HC_DOWN_INTERPOLATION   0x01800000
257#define HORIZONTAL_LUMINANCE_FILTER     0x00380000
258#define HL_SCALING_OFF          0x00000000
259#define HL_LINE_REPLICATION     0x00080000
260#define HL_UP_INTERPOLATION     0x00100000
261#define HL_PIXEL_DROPPING       0x00280000
262#define HL_DOWN_INTERPOLATION   0x00300000
263
264#define Y_ADJUST                0x00010000
265#define OV_BYTE_ORDER           0x0000C000
266#define UV_SWAP                 0x00004000
267#define Y_SWAP                  0x00008000
268#define Y_AND_UV_SWAP           0x0000C000
269#define SOURCE_FORMAT           0x00003C00
270#define RGB_555                 0x00000800
271#define RGB_565                 0x00000C00
272#define YUV_422                 0x00002000
273#define YUV_411                 0x00002400
274#define YUV_420                 0x00003000
275#define YUV_410                 0x00003800
276#define VERTICAL_PHASE_BOTH     0x00000020
277#define FLIP_TYPE_FIELD         0x00000020
278#define FLIP_TYPE_FRAME         0x00000000
279#define BUFFER_AND_FIELD        0x00000006
280#define BUFFER0_FIELD0          0x00000000
281#define BUFFER0_FIELD1          0x00000002
282#define BUFFER1_FIELD0          0x00000004
283#define BUFFER1_FIELD1          0x00000006
284#define OVERLAY_ENABLE          0x00000001
285
286/*
287 * DOV0STA - Display/Overlay 0 Status Register
288 */
289#define DOV0STA         0x30008
290#define OV0ADD         0x30000
291#define MINUV_SCALE     0x1
292
293#define RGB16ToColorKey(c) \
294        (((c & 0xF800) << 8) | ((c & 0x07E0) << 5) | ((c & 0x001F) << 3))
295
296/* Locking Macros lightweight lock used to prevent relocking */
297#define I810_LOCK(c,f)                     \
298  if(!c->lock) {                           \
299    drmGetLock(c->fd, c->drmcontext, f);   \
300  }                                        \
301  c->lock++;
302
303#define I810_UNLOCK(c)                     \
304  c->lock--;                               \
305  if(!c->lock) {                           \
306    drmUnlock(c->fd, c->drmcontext);       \
307  }
308
309/*
310  Block until the passed in value (n) is the active
311  buffer on the overlay.
312*/
313#define BLOCK_OVERLAY(c,n)                        \
314  do {                                            \
315   int temp,i=0;                                  \
316   while(i < 100000) {                            \
317     temp = GET_FSTATUS(c);                       \
318     if(((temp & (1<<20))>>20) == n) {            \
319       break;                                     \
320     }                                            \
321     usleep(10);                                  \
322   }                                              \
323   if(i == 100000) {                              \
324     printf("Overlay Lockup.\n");                 \
325     return BadAlloc;                             \
326   }                                              \
327 }while(0);
328
329#define OVERLAY_INFO(c,i) drmCommandRead(c->fd, DRM_I810_OV0INFO, &i, sizeof(i))
330#define OVERLAY_FLIP(c) drmCommandNone(c->fd, DRM_I810_OV0FLIP)
331#define GET_FSTATUS(c) drmCommandNone(c->fd, DRM_I810_FSTATUS)
332#define I810_MC(c,mc) drmCommandWrite(c->fd, DRM_I810_MC, &mc, sizeof(mc))
333#define GET_RSTATUS(c) drmCommandNone(c->fd, DRM_I810_RSTATUS)
334#define GET_BUFFER(c,dma) drmCommandWriteRead(c->fd, DRM_I810_GETBUF, &dma, sizeof(drmI810DMA))
335#define FLUSH(c) drmCommandNone(c->fd, DRM_I810_FLUSH)
336
337/*
338  Definitions for temporary wire protocol hooks to be replaced
339  when a HW independent libXvMC is created.
340*/
341extern Status _xvmc_create_context(Display *dpy, XvMCContext *context,
342				   int *priv_count, uint **priv_data);
343
344extern Status _xvmc_destroy_context(Display *dpy, XvMCContext *context);
345
346extern Status _xvmc_create_surface(Display *dpy, XvMCContext *context,
347				   XvMCSurface *surface, int *priv_count,
348				   uint **priv_data);
349
350extern Status _xvmc_destroy_surface(Display *dpy, XvMCSurface *surface);
351
352extern Status  _xvmc_create_subpicture(Display *dpy, XvMCContext *context,
353				       XvMCSubpicture *subpicture,
354				       int *priv_count, uint **priv_data);
355
356extern Status   _xvmc_destroy_subpicture(Display *dpy,
357					 XvMCSubpicture *subpicture);
358
359/*
360  Prototypes
361*/
362drmBufPtr i810_get_free_buffer(i810XvMCContext *pI810XvMC);
363void i810_free_privContext(i810XvMCContext *pI810XvMC);
364void dp(unsigned int *address, unsigned int i);
365
366#endif
367