Subversion Repositories Kolibri OS

Rev

Rev 4280 | Rev 4371 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1.  
  2. #define iowrite32(v, addr)      writel((v), (addr))
  3.  
  4. #include "drmP.h"
  5. #include "drm.h"
  6. #include "i915_drm.h"
  7. #include "i915_drv.h"
  8. #include "intel_drv.h"
  9.  
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/mod_devicetable.h>
  13. #include <errno-base.h>
  14. #include <linux/pci.h>
  15.  
  16. #include <syscall.h>
  17.  
  18. #include "bitmap.h"
  19.  
  20. typedef struct
  21. {
  22.     kobj_t     header;
  23.  
  24.     uint32_t  *data;
  25.     uint32_t   hot_x;
  26.     uint32_t   hot_y;
  27.  
  28.     struct list_head   list;
  29.     struct drm_i915_gem_object  *cobj;
  30. }cursor_t;
  31.  
  32. #define CURSOR_WIDTH 64
  33. #define CURSOR_HEIGHT 64
  34.  
  35.  
  36. struct tag_display
  37. {
  38.     int  x;
  39.     int  y;
  40.     int  width;
  41.     int  height;
  42.     int  bpp;
  43.     int  vrefresh;
  44.     int  pitch;
  45.     int  lfb;
  46.  
  47.     int  supported_modes;
  48.     struct drm_device    *ddev;
  49.     struct drm_connector *connector;
  50.     struct drm_crtc      *crtc;
  51.  
  52.     struct list_head   cursors;
  53.  
  54.     cursor_t   *cursor;
  55.     int       (*init_cursor)(cursor_t*);
  56.     cursor_t* (__stdcall *select_cursor)(cursor_t*);
  57.     void      (*show_cursor)(int show);
  58.     void      (__stdcall *move_cursor)(cursor_t *cursor, int x, int y);
  59.     void      (__stdcall *restore_cursor)(int x, int y);
  60.     void      (*disable_mouse)(void);
  61.     u32  mask_seqno;
  62.     u32  check_mouse;
  63.     u32  check_m_pixel;
  64.  
  65. };
  66.  
  67.  
  68. static display_t *os_display;
  69.  
  70. u32_t cmd_buffer;
  71. u32_t cmd_offset;
  72.  
  73. void init_render();
  74. int  sna_init();
  75.  
  76. int init_cursor(cursor_t *cursor);
  77. static cursor_t*  __stdcall select_cursor_kms(cursor_t *cursor);
  78. static void       __stdcall move_cursor_kms(cursor_t *cursor, int x, int y);
  79.  
  80. void __stdcall restore_cursor(int x, int y)
  81. {};
  82.  
  83. void disable_mouse(void)
  84. {};
  85.  
  86. static char *manufacturer_name(unsigned char *x)
  87. {
  88.     static char name[4];
  89.  
  90.     name[0] = ((x[0] & 0x7C) >> 2) + '@';
  91.     name[1] = ((x[0] & 0x03) << 3) + ((x[1] & 0xE0) >> 5) + '@';
  92.     name[2] = (x[1] & 0x1F) + '@';
  93.     name[3] = 0;
  94.  
  95.     return name;
  96. }
  97.  
  98. bool set_mode(struct drm_device *dev, struct drm_connector *connector,
  99.               videomode_t *reqmode, bool strict)
  100. {
  101.     drm_i915_private_t      *dev_priv   = dev->dev_private;
  102.     struct drm_fb_helper    *fb_helper  = &dev_priv->fbdev->helper;
  103.  
  104.     struct drm_mode_config  *config     = &dev->mode_config;
  105.     struct drm_display_mode *mode       = NULL, *tmpmode;
  106.     struct drm_framebuffer  *fb         = NULL;
  107.     struct drm_crtc         *crtc;
  108.     struct drm_encoder      *encoder;
  109.     struct drm_i915_gem_object *fb_obj;
  110.     struct drm_mode_set     set;
  111.     char *con_name;
  112.     char *enc_name;
  113.     unsigned hdisplay, vdisplay;
  114.     int ret;
  115.  
  116.     mutex_lock(&dev->mode_config.mutex);
  117.  
  118.     list_for_each_entry(tmpmode, &connector->modes, head)
  119.     {
  120.         if( (drm_mode_width(tmpmode)    == reqmode->width)  &&
  121.             (drm_mode_height(tmpmode)   == reqmode->height) &&
  122.             (drm_mode_vrefresh(tmpmode) == reqmode->freq) )
  123.         {
  124.             mode = tmpmode;
  125.             goto do_set;
  126.         }
  127.     };
  128.  
  129.     if( (mode == NULL) && (strict == false) )
  130.     {
  131.         list_for_each_entry(tmpmode, &connector->modes, head)
  132.         {
  133.             if( (drm_mode_width(tmpmode)  == reqmode->width)  &&
  134.                 (drm_mode_height(tmpmode) == reqmode->height) )
  135.             {
  136.                 mode = tmpmode;
  137.                 goto do_set;
  138.             }
  139.         };
  140.     };
  141.  
  142.     DRM_ERROR("%s failed\n", __FUNCTION__);
  143.  
  144.     return -1;
  145.  
  146. do_set:
  147.  
  148.     encoder = connector->encoder;
  149.     crtc = encoder->crtc;
  150.  
  151.     con_name = drm_get_connector_name(connector);
  152.     enc_name = drm_get_encoder_name(encoder);
  153.  
  154.     DRM_DEBUG_KMS("set mode %d %d: crtc %d connector %s encoder %s\n",
  155.               reqmode->width, reqmode->height, crtc->base.id,
  156.               con_name, enc_name);
  157.  
  158.     drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
  159.  
  160.     hdisplay = mode->hdisplay;
  161.     vdisplay = mode->vdisplay;
  162.  
  163.     if (crtc->invert_dimensions)
  164.         swap(hdisplay, vdisplay);
  165.  
  166.     fb_obj = get_fb_obj();
  167.     fb = fb_helper->fb;
  168.  
  169.     fb->width  = reqmode->width;
  170.     fb->height = reqmode->height;
  171.  
  172.     fb->pitches[0]  = fb->pitches[1]  = fb->pitches[2]  =
  173.                       fb->pitches[3]  = ALIGN(reqmode->width * 4, 512);
  174.  
  175.     fb_obj->stride = fb->pitches[0];
  176.  
  177.     fb->bits_per_pixel = 32;
  178.     fb->depth = 24;
  179.  
  180.     crtc->fb = fb;
  181.     crtc->enabled = true;
  182.     os_display->crtc = crtc;
  183.  
  184.     i915_gem_object_put_fence(fb_obj);
  185.  
  186.     set.crtc = crtc;
  187.     set.x = 0;
  188.     set.y = 0;
  189.     set.mode = mode;
  190.     set.connectors = &connector;
  191.     set.num_connectors = 1;
  192.     set.fb = fb;
  193.     ret = crtc->funcs->set_config(&set);
  194.     mutex_unlock(&dev->mode_config.mutex);
  195.  
  196.     if ( !ret )
  197.     {
  198.         os_display->width    = fb->width;
  199.         os_display->height   = fb->height;
  200.         os_display->vrefresh = drm_mode_vrefresh(mode);
  201.  
  202.         sysSetScreen(fb->width, fb->height, fb->pitches[0]);
  203.  
  204.         DRM_DEBUG_KMS("new mode %d x %d pitch %d\n",
  205.                        fb->width, fb->height, fb->pitches[0]);
  206.     }
  207.     else
  208.         DRM_ERROR("failed to set mode %d_%d on crtc %p\n",
  209.                    fb->width, fb->height, crtc);
  210.  
  211.     return ret;
  212. }
  213.  
  214. static int count_connector_modes(struct drm_connector* connector)
  215. {
  216.     struct drm_display_mode  *mode;
  217.     int count = 0;
  218.  
  219.     list_for_each_entry(mode, &connector->modes, head)
  220.     {
  221.         count++;
  222.     };
  223.     return count;
  224. };
  225.  
  226. static struct drm_connector* get_def_connector(struct drm_device *dev)
  227. {
  228.     struct drm_connector  *connector;
  229.     struct drm_connector_helper_funcs *connector_funcs;
  230.  
  231.     struct drm_connector  *def_connector = NULL;
  232.  
  233.     list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  234.     {
  235.         struct drm_encoder  *encoder;
  236.         struct drm_crtc     *crtc;
  237.  
  238.         if( connector->status != connector_status_connected)
  239.             continue;
  240.  
  241.         connector_funcs = connector->helper_private;
  242.         encoder = connector_funcs->best_encoder(connector);
  243.         if( encoder == NULL)
  244.             continue;
  245.  
  246.         connector->encoder = encoder;
  247.  
  248.         crtc = encoder->crtc;
  249.  
  250.         DRM_DEBUG_KMS("CONNECTOR %x ID:  %d status %d encoder %x\n crtc %x",
  251.                    connector, connector->base.id,
  252.                    connector->status, connector->encoder,
  253.                    crtc);
  254.  
  255. //        if (crtc == NULL)
  256. //            continue;
  257.  
  258.         def_connector = connector;
  259.  
  260.         break;
  261.     };
  262.  
  263.     return def_connector;
  264. };
  265.  
  266.  
  267. int init_display_kms(struct drm_device *dev, videomode_t *usermode)
  268. {
  269.     struct drm_connector    *connector;
  270.     struct drm_connector_helper_funcs *connector_funcs;
  271.     struct drm_encoder      *encoder;
  272.     struct drm_crtc         *crtc = NULL;
  273.     struct drm_framebuffer  *fb;
  274.  
  275.     cursor_t  *cursor;
  276.     u32_t      ifl;
  277.     int        err;
  278.  
  279.     ENTER();
  280.  
  281.     mutex_lock(&dev->mode_config.mutex);
  282.  
  283.     list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  284.     {
  285.         if( connector->status != connector_status_connected)
  286.             continue;
  287.  
  288.         connector_funcs = connector->helper_private;
  289.         encoder = connector_funcs->best_encoder(connector);
  290.         if( encoder == NULL)
  291.         {
  292.             DRM_DEBUG_KMS("CONNECTOR %x ID: %d no active encoders\n",
  293.                       connector, connector->base.id);
  294.             continue;
  295.         }
  296.         connector->encoder = encoder;
  297.         crtc = encoder->crtc;
  298.  
  299.         DRM_DEBUG_KMS("CONNECTOR %p ID:%d status:%d ENCODER %x CRTC %p ID:%d\n",
  300.                connector, connector->base.id,
  301.                connector->status, connector->encoder,
  302.                crtc, crtc->base.id );
  303.         break;
  304.     };
  305.  
  306.     if(connector == NULL)
  307.     {
  308.         DRM_DEBUG_KMS("No active connectors!\n");
  309.         mutex_unlock(&dev->mode_config.mutex);
  310.         return -1;
  311.     };
  312.  
  313.     dbgprintf("CRTC %p\n", crtc);
  314.  
  315.     if(crtc == NULL)
  316.     {
  317.         struct drm_crtc *tmp_crtc;
  318.         int crtc_mask = 1;
  319.  
  320.         list_for_each_entry(tmp_crtc, &dev->mode_config.crtc_list, head)
  321.         {
  322.             dbgprintf("tmp_crtc %p\n", tmp_crtc);
  323.             if (encoder->possible_crtcs & crtc_mask)
  324.             {
  325.                 crtc = tmp_crtc;
  326.                 encoder->crtc = crtc;
  327.                 dbgprintf("CRTC %p\n", crtc);
  328.                 break;
  329.             };
  330.             crtc_mask <<= 1;
  331.         };
  332.     };
  333.  
  334.     dbgprintf("CRTC %p\n", crtc);
  335.  
  336.     if(crtc == NULL)
  337.     {
  338.         DRM_DEBUG_KMS("No CRTC for encoder %d\n", encoder->base.id);
  339.         mutex_unlock(&dev->mode_config.mutex);
  340.         return -1;
  341.     };
  342.  
  343.     DRM_DEBUG_KMS("[Select CRTC: %p ID:%d]\n",crtc, crtc->base.id);
  344.  
  345.     os_display = GetDisplay();
  346.     os_display->ddev = dev;
  347.     os_display->connector = connector;
  348.     os_display->crtc = crtc;
  349.  
  350.     os_display->supported_modes = count_connector_modes(connector);
  351.  
  352.  
  353.     ifl = safe_cli();
  354.     {
  355.         struct intel_crtc *intel_crtc = to_intel_crtc(os_display->crtc);
  356.  
  357.         list_for_each_entry(cursor, &os_display->cursors, list)
  358.         {
  359.             init_cursor(cursor);
  360.         };
  361.  
  362.         os_display->restore_cursor(0,0);
  363.         os_display->init_cursor    = init_cursor;
  364.         os_display->select_cursor  = select_cursor_kms;
  365.         os_display->show_cursor    = NULL;
  366.         os_display->move_cursor    = move_cursor_kms;
  367.         os_display->restore_cursor = restore_cursor;
  368.         os_display->disable_mouse  = disable_mouse;
  369.  
  370.         intel_crtc->cursor_x = os_display->width/2;
  371.         intel_crtc->cursor_y = os_display->height/2;
  372.  
  373.         select_cursor_kms(os_display->cursor);
  374.     };
  375.     safe_sti(ifl);
  376.  
  377.     if( (usermode->width == 0) ||
  378.         (usermode->height == 0))
  379.     {
  380.         struct drm_display_mode *mode;
  381.  
  382.         list_for_each_entry(mode, &connector->modes, head)
  383.         {
  384.             dbgprintf("check mode w:%d h:%d %dHz\n",
  385.                     drm_mode_width(mode), drm_mode_height(mode),
  386.                     drm_mode_vrefresh(mode));
  387.  
  388.             if( os_display->width  == drm_mode_width(mode)  &&
  389.                 os_display->height == drm_mode_height(mode) &&
  390.                 drm_mode_vrefresh(mode) == 60)
  391.             {
  392.                 usermode->width  = os_display->width;
  393.                 usermode->height = os_display->height;
  394.                 usermode->freq   = 60;
  395.                 break;
  396.  
  397.             }
  398.         }
  399.  
  400.         if( usermode->width  == 0 ||
  401.             usermode->height == 0)
  402.         {
  403.             mode = list_entry(connector->modes.next, typeof(*mode), head);
  404.  
  405.             usermode->width  = drm_mode_width(mode);
  406.             usermode->height = drm_mode_height(mode);
  407.             usermode->freq   = drm_mode_vrefresh(mode);
  408.         };
  409.     };
  410.  
  411.     mutex_unlock(&dev->mode_config.mutex);
  412.  
  413.     set_mode(dev, os_display->connector, usermode, false);
  414.  
  415. #ifdef __HWA__
  416.     err = init_bitmaps();
  417. #endif
  418.  
  419.     LEAVE();
  420.  
  421.     return 0;
  422. };
  423.  
  424.  
  425. int get_videomodes(videomode_t *mode, int *count)
  426. {
  427.     int err = -1;
  428.  
  429. //    dbgprintf("mode %x count %d\n", mode, *count);
  430.  
  431.     if( *count == 0 )
  432.     {
  433.         *count = os_display->supported_modes;
  434.         err = 0;
  435.     }
  436.     else if( mode != NULL )
  437.     {
  438.         struct drm_display_mode  *drmmode;
  439.         int i = 0;
  440.  
  441.         if( *count > os_display->supported_modes)
  442.             *count = os_display->supported_modes;
  443.  
  444.         list_for_each_entry(drmmode, &os_display->connector->modes, head)
  445.         {
  446.             if( i < *count)
  447.             {
  448.                 mode->width  = drm_mode_width(drmmode);
  449.                 mode->height = drm_mode_height(drmmode);
  450.                 mode->bpp    = 32;
  451.                 mode->freq   = drm_mode_vrefresh(drmmode);
  452.                 i++;
  453.                 mode++;
  454.             }
  455.             else break;
  456.         };
  457.         *count = i;
  458.         err = 0;
  459.     };
  460.     return err;
  461. };
  462.  
  463. int set_user_mode(videomode_t *mode)
  464. {
  465.     int err = -1;
  466.  
  467. //    dbgprintf("width %d height %d vrefresh %d\n",
  468. //               mode->width, mode->height, mode->freq);
  469.  
  470.     if( (mode->width  != 0)  &&
  471.         (mode->height != 0)  &&
  472.         (mode->freq   != 0 ) &&
  473.         ( (mode->width   != os_display->width)  ||
  474.           (mode->height  != os_display->height) ||
  475.           (mode->freq    != os_display->vrefresh) ) )
  476.     {
  477.         if( set_mode(os_display->ddev, os_display->connector, mode, true) )
  478.             err = 0;
  479.     };
  480.  
  481.     return err;
  482. };
  483.  
  484. void i915_dpms(struct drm_device *dev, int mode)
  485. {
  486.     struct drm_connector_funcs *f = os_display->connector->funcs;
  487.  
  488.     f->dpms(os_display->connector, mode);
  489. };
  490.  
  491. void __attribute__((regparm(1))) destroy_cursor(cursor_t *cursor)
  492. {
  493.     list_del(&cursor->list);
  494.  
  495.     i915_gem_object_unpin(cursor->cobj);
  496.  
  497.     mutex_lock(&main_device->struct_mutex);
  498.     drm_gem_object_unreference(&cursor->cobj->base);
  499.     mutex_unlock(&main_device->struct_mutex);
  500.  
  501.     __DestroyObject(cursor);
  502. };
  503.  
  504. int init_cursor(cursor_t *cursor)
  505. {
  506.     struct drm_i915_private *dev_priv = os_display->ddev->dev_private;
  507.     struct drm_i915_gem_object *obj;
  508.     uint32_t *bits;
  509.     uint32_t *src;
  510.     void     *mapped;
  511.  
  512.     int       i,j;
  513.     int       ret;
  514.  
  515.     if (dev_priv->info->cursor_needs_physical)
  516.     {
  517.         bits = (uint32_t*)KernelAlloc(CURSOR_WIDTH*CURSOR_HEIGHT*4);
  518.         if (unlikely(bits == NULL))
  519.             return ENOMEM;
  520.         cursor->cobj = (struct drm_i915_gem_object *)GetPgAddr(bits);
  521.     }
  522.     else
  523.     {
  524.         obj = i915_gem_alloc_object(os_display->ddev, CURSOR_WIDTH*CURSOR_HEIGHT*4);
  525.         if (unlikely(obj == NULL))
  526.             return -ENOMEM;
  527.  
  528.         ret = i915_gem_obj_ggtt_pin(obj, CURSOR_WIDTH*CURSOR_HEIGHT*4, true, true);
  529.         if (ret) {
  530.             drm_gem_object_unreference(&obj->base);
  531.             return ret;
  532.         }
  533.  
  534.         ret = i915_gem_object_set_to_gtt_domain(obj, true);
  535.         if (ret)
  536.         {
  537.             i915_gem_object_unpin(obj);
  538.             drm_gem_object_unreference(&obj->base);
  539.             return ret;
  540.         }
  541. /* You don't need to worry about fragmentation issues.
  542.  * GTT space is continuous. I guarantee it.                           */
  543.  
  544.         mapped = bits = (u32*)MapIoMem(dev_priv->gtt.mappable_base + i915_gem_obj_ggtt_offset(obj),
  545.                     CURSOR_WIDTH*CURSOR_HEIGHT*4, PG_SW);
  546.  
  547.         if (unlikely(bits == NULL))
  548.         {
  549.             i915_gem_object_unpin(obj);
  550.             drm_gem_object_unreference(&obj->base);
  551.             return -ENOMEM;
  552.         };
  553.         cursor->cobj = obj;
  554.     };
  555.  
  556.     src = cursor->data;
  557.  
  558.     for(i = 0; i < 32; i++)
  559.     {
  560.         for(j = 0; j < 32; j++)
  561.             *bits++ = *src++;
  562.         for(j = 32; j < CURSOR_WIDTH; j++)
  563.             *bits++ = 0;
  564.     }
  565.     for(i = 0; i < CURSOR_WIDTH*(CURSOR_HEIGHT-32); i++)
  566.         *bits++ = 0;
  567.  
  568.     FreeKernelSpace(mapped);
  569.  
  570. // release old cursor
  571.  
  572.     KernelFree(cursor->data);
  573.  
  574.     cursor->data = bits;
  575.  
  576.     cursor->header.destroy = destroy_cursor;
  577.  
  578.     return 0;
  579. }
  580.  
  581.  
  582. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  583. {
  584.     struct drm_device *dev = crtc->dev;
  585.     struct drm_i915_private *dev_priv = dev->dev_private;
  586.     struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  587.     int pipe = intel_crtc->pipe;
  588.     bool visible = base != 0;
  589.  
  590.     if (intel_crtc->cursor_visible != visible) {
  591.         uint32_t cntl = I915_READ(CURCNTR(pipe));
  592.         if (base) {
  593.             cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
  594.             cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
  595.             cntl |= pipe << 28; /* Connect to correct pipe */
  596.         } else {
  597.             cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
  598.             cntl |= CURSOR_MODE_DISABLE;
  599.         }
  600.         I915_WRITE(CURCNTR(pipe), cntl);
  601.  
  602.         intel_crtc->cursor_visible = visible;
  603.     }
  604.     /* and commit changes on next vblank */
  605.     I915_WRITE(CURBASE(pipe), base);
  606. }
  607.  
  608. void __stdcall move_cursor_kms(cursor_t *cursor, int x, int y)
  609. {
  610.     struct drm_i915_private *dev_priv = os_display->ddev->dev_private;
  611.     struct intel_crtc *intel_crtc = to_intel_crtc(os_display->crtc);
  612.     u32 base, pos;
  613.     bool visible;
  614.  
  615.     int pipe = intel_crtc->pipe;
  616.  
  617.     intel_crtc->cursor_x = x;
  618.     intel_crtc->cursor_y = y;
  619.  
  620.     x = x - cursor->hot_x;
  621.     y = y - cursor->hot_y;
  622.  
  623.  
  624.     pos = 0;
  625.  
  626.     base = intel_crtc->cursor_addr;
  627.     if (x >= os_display->width)
  628.         base = 0;
  629.  
  630.     if (y >= os_display->height)
  631.         base = 0;
  632.  
  633.     if (x < 0)
  634.     {
  635.         if (x + intel_crtc->cursor_width < 0)
  636.             base = 0;
  637.  
  638.         pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  639.         x = -x;
  640.     }
  641.     pos |= x << CURSOR_X_SHIFT;
  642.  
  643.     if (y < 0)
  644.     {
  645.         if (y + intel_crtc->cursor_height < 0)
  646.             base = 0;
  647.  
  648.         pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  649.         y = -y;
  650.     }
  651.     pos |= y << CURSOR_Y_SHIFT;
  652.  
  653.     visible = base != 0;
  654.     if (!visible && !intel_crtc->cursor_visible)
  655.         return;
  656.  
  657.     I915_WRITE(CURPOS(pipe), pos);
  658. //    if (IS_845G(dev) || IS_I865G(dev))
  659. //        i845_update_cursor(crtc, base);
  660. //    else
  661.         i9xx_update_cursor(os_display->crtc, base);
  662.  
  663. };
  664.  
  665.  
  666. cursor_t* __stdcall select_cursor_kms(cursor_t *cursor)
  667. {
  668.     struct drm_i915_private *dev_priv = os_display->ddev->dev_private;
  669.     struct intel_crtc *intel_crtc = to_intel_crtc(os_display->crtc);
  670.     cursor_t *old;
  671.  
  672.     old = os_display->cursor;
  673.     os_display->cursor = cursor;
  674.  
  675.     if (!dev_priv->info->cursor_needs_physical)
  676.        intel_crtc->cursor_addr = i915_gem_obj_ggtt_offset(cursor->cobj);
  677.     else
  678.         intel_crtc->cursor_addr = (addr_t)cursor->cobj;
  679.  
  680.     intel_crtc->cursor_width = 32;
  681.     intel_crtc->cursor_height = 32;
  682.  
  683.     move_cursor_kms(cursor, intel_crtc->cursor_x, intel_crtc->cursor_y);
  684.     return old;
  685. };
  686.  
  687. struct sna_fb
  688. {
  689.     uint32_t  width;
  690.     uint32_t  height;
  691.     uint32_t  pitch;
  692.     uint32_t  tiling;
  693. };
  694.  
  695. int i915_fbinfo(struct sna_fb *fb)
  696. {
  697.     fb->width  = os_display->width;
  698.     fb->height = os_display->height;
  699.     fb->pitch  = os_display->pitch;
  700.     fb->tiling = 0;
  701.  
  702.     return 0;
  703. };
  704.  
  705. typedef struct
  706. {
  707.     int left;
  708.     int top;
  709.     int right;
  710.     int bottom;
  711. }rect_t;
  712.  
  713. struct drm_i915_mask {
  714.     __u32 handle;
  715.     __u32 width;
  716.     __u32 height;
  717.     __u32 bo_size;
  718.     __u32 bo_pitch;
  719.     __u32 bo_map;
  720. };
  721.  
  722. #define CURRENT_TASK             (0x80003000)
  723.  
  724. static u32_t get_display_map()
  725. {
  726.     u32_t   addr;
  727.  
  728.     addr = (u32_t)os_display;
  729.     addr+= sizeof(display_t);            /*  shoot me  */
  730.     return *(u32_t*)addr;
  731. }
  732.  
  733. void  FASTCALL GetWindowRect(rect_t *rc)__asm__("GetWindowRect");
  734.  
  735. int i915_mask_update(struct drm_device *dev, void *data,
  736.             struct drm_file *file)
  737. {
  738.     struct drm_i915_mask *mask = data;
  739.     struct drm_gem_object *obj;
  740.     static unsigned int mask_seqno[256];
  741.     rect_t winrc;
  742.     u32    slot;
  743.     int    ret;
  744.  
  745.      if(mask->handle == -2)
  746.      {
  747.         printf("%s handle %d\n", __FUNCTION__, mask->handle);
  748.         return 0;
  749.      }
  750.  
  751.     obj = drm_gem_object_lookup(dev, file, mask->handle);
  752.     if (obj == NULL)
  753.         return -ENOENT;
  754.  
  755.     if (!obj->filp) {
  756.         drm_gem_object_unreference_unlocked(obj);
  757.         return -EINVAL;
  758.     }
  759.  
  760.     GetWindowRect(&winrc);
  761.     {
  762. //        static warn_count;
  763.  
  764.         mask->width    = winrc.right - winrc.left + 1;
  765.         mask->height   = winrc.bottom - winrc.top + 1;
  766.         mask->bo_pitch = (mask->width+15) & ~15;
  767.  
  768. #if 0
  769.         if(warn_count < 1)
  770.         {
  771.             printf("left %d top %d right %d bottom %d\n",
  772.                     winrc.left, winrc.top, winrc.right, winrc.bottom);
  773.             printf("mask pitch %d data %p\n", mask->bo_pitch, mask->bo_size);
  774.             warn_count++;
  775.         };
  776. #endif
  777.  
  778.      };
  779.  
  780.  
  781.     slot = *((u8*)CURRENT_TASK);
  782.  
  783.     if( mask_seqno[slot] != os_display->mask_seqno)
  784.     {
  785.         u8* src_offset;
  786.         u8* dst_offset;
  787.         u32 ifl;
  788.  
  789.         ret = i915_mutex_lock_interruptible(dev);
  790.         if (ret)
  791.             return ret;
  792.  
  793.         ret = i915_gem_object_set_to_cpu_domain(to_intel_bo(obj), true);
  794.         if(ret !=0 )
  795.         {
  796.             dbgprintf("%s fail\n", __FUNCTION__);
  797.             return ret;
  798.         };
  799.  
  800. //        printf("width %d height %d\n", winrc.right, winrc.bottom);
  801.  
  802. //        slot = 0x01;
  803.  
  804.  
  805.         src_offset = (u8*)( winrc.top*os_display->width + winrc.left);
  806.         src_offset+= get_display_map();
  807.         dst_offset = (u8*)mask->bo_map;
  808.  
  809.         u32_t tmp_h = mask->height;
  810.  
  811.         ifl = safe_cli();
  812.         {
  813.             mask_seqno[slot] = os_display->mask_seqno;
  814.  
  815.             slot|= (slot<<8)|(slot<<16)|(slot<<24);
  816.  
  817.             __asm__ __volatile__ (
  818.                 "movd       %[slot],   %%xmm6    \n"
  819.             "punpckldq  %%xmm6, %%xmm6            \n"
  820.             "punpcklqdq %%xmm6, %%xmm6            \n"
  821.             :: [slot]  "m" (slot)
  822.             :"xmm6");
  823.  
  824.             while( tmp_h--)
  825.             {
  826.                 int tmp_w = mask->bo_pitch;
  827.  
  828.                 u8* tmp_src = src_offset;
  829.                 u8* tmp_dst = dst_offset;
  830.  
  831.                 src_offset+= os_display->width;
  832.                 dst_offset+= mask->bo_pitch;
  833.  
  834.                 while(tmp_w >= 64)
  835.                 {
  836.                     __asm__ __volatile__ (
  837.                     "movdqu     (%0),   %%xmm0            \n"
  838.                     "movdqu   16(%0),   %%xmm1            \n"
  839.                     "movdqu   32(%0),   %%xmm2            \n"
  840.                     "movdqu   48(%0),   %%xmm3            \n"
  841.                     "pcmpeqb    %%xmm6, %%xmm0            \n"
  842.                     "pcmpeqb    %%xmm6, %%xmm1            \n"
  843.                     "pcmpeqb    %%xmm6, %%xmm2            \n"
  844.                     "pcmpeqb    %%xmm6, %%xmm3            \n"
  845.                     "movdqa     %%xmm0,   (%%edi)         \n"
  846.                     "movdqa     %%xmm1, 16(%%edi)         \n"
  847.                     "movdqa     %%xmm2, 32(%%edi)         \n"
  848.                     "movdqa     %%xmm3, 48(%%edi)         \n"
  849.  
  850.                     :: "r" (tmp_src), "D" (tmp_dst)
  851.                     :"xmm0","xmm1","xmm2","xmm3");
  852.                     tmp_w -= 64;
  853.                     tmp_src += 64;
  854.                     tmp_dst += 64;
  855.                 }
  856.  
  857.                 if( tmp_w >= 32 )
  858.                 {
  859.                     __asm__ __volatile__ (
  860.                     "movdqu     (%0),   %%xmm0            \n"
  861.                     "movdqu   16(%0),   %%xmm1            \n"
  862.                     "pcmpeqb    %%xmm6, %%xmm0            \n"
  863.                     "pcmpeqb    %%xmm6, %%xmm1            \n"
  864.                     "movdqa     %%xmm0,   (%%edi)         \n"
  865.                     "movdqa     %%xmm1, 16(%%edi)         \n"
  866.  
  867.                     :: "r" (tmp_src), "D" (tmp_dst)
  868.                     :"xmm0","xmm1");
  869.                     tmp_w -= 32;
  870.                     tmp_src += 32;
  871.                     tmp_dst += 32;
  872.                 }
  873.  
  874.                 while( tmp_w > 0 )
  875.                 {
  876.                     __asm__ __volatile__ (
  877.                     "movdqu     (%0),   %%xmm0            \n"
  878.                     "pcmpeqb    %%xmm6, %%xmm0            \n"
  879.                     "movdqa     %%xmm0,   (%%edi)         \n"
  880.                     :: "r" (tmp_src), "D" (tmp_dst)
  881.                     :"xmm0");
  882.                     tmp_w -= 16;
  883.                     tmp_src += 16;
  884.                     tmp_dst += 16;
  885.                 }
  886.             };
  887.         };
  888.         safe_sti(ifl);
  889.     }
  890.  
  891.     drm_gem_object_unreference(obj);
  892.  
  893.     mutex_unlock(&dev->struct_mutex);
  894.  
  895.     return 0;
  896. }
  897.  
  898.  
  899.  
  900.  
  901.  
  902.  
  903.  
  904.  
  905.  
  906.  
  907.  
  908.  
  909. #define NSEC_PER_SEC    1000000000L
  910.  
  911. void getrawmonotonic(struct timespec *ts)
  912. {
  913.     u32 tmp = GetTimerTicks();
  914.  
  915.     ts->tv_sec  = tmp/100;
  916.     ts->tv_nsec = (tmp - ts->tv_sec*100)*10000000;
  917. }
  918.  
  919. void set_normalized_timespec(struct timespec *ts, time_t sec, s64 nsec)
  920. {
  921.     while (nsec >= NSEC_PER_SEC) {
  922.         /*
  923.          * The following asm() prevents the compiler from
  924.          * optimising this loop into a modulo operation. See
  925.          * also __iter_div_u64_rem() in include/linux/time.h
  926.          */
  927.         asm("" : "+rm"(nsec));
  928.         nsec -= NSEC_PER_SEC;
  929.         ++sec;
  930.     }
  931.     while (nsec < 0) {
  932.         asm("" : "+rm"(nsec));
  933.         nsec += NSEC_PER_SEC;
  934.         --sec;
  935.     }
  936.     ts->tv_sec = sec;
  937.     ts->tv_nsec = nsec;
  938. }
  939.  
  940. void
  941. prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state)
  942. {
  943.     unsigned long flags;
  944.  
  945. //    wait->flags &= ~WQ_FLAG_EXCLUSIVE;
  946.     spin_lock_irqsave(&q->lock, flags);
  947.     if (list_empty(&wait->task_list))
  948.             __add_wait_queue(q, wait);
  949.     spin_unlock_irqrestore(&q->lock, flags);
  950. }
  951.  
  952. /**
  953.  * finish_wait - clean up after waiting in a queue
  954.  * @q: waitqueue waited on
  955.  * @wait: wait descriptor
  956.  *
  957.  * Sets current thread back to running state and removes
  958.  * the wait descriptor from the given waitqueue if still
  959.  * queued.
  960.  */
  961. void finish_wait(wait_queue_head_t *q, wait_queue_t *wait)
  962. {
  963.     unsigned long flags;
  964.  
  965. //    __set_current_state(TASK_RUNNING);
  966.     /*
  967.      * We can check for list emptiness outside the lock
  968.      * IFF:
  969.      *  - we use the "careful" check that verifies both
  970.      *    the next and prev pointers, so that there cannot
  971.      *    be any half-pending updates in progress on other
  972.      *    CPU's that we haven't seen yet (and that might
  973.      *    still change the stack area.
  974.      * and
  975.      *  - all other users take the lock (ie we can only
  976.      *    have _one_ other CPU that looks at or modifies
  977.      *    the list).
  978.      */
  979.     if (!list_empty_careful(&wait->task_list)) {
  980.             spin_lock_irqsave(&q->lock, flags);
  981.             list_del_init(&wait->task_list);
  982.             spin_unlock_irqrestore(&q->lock, flags);
  983.     }
  984.  
  985.     DestroyEvent(wait->evnt);
  986. }
  987.  
  988. int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key)
  989. {
  990.     list_del_init(&wait->task_list);
  991.     return 1;
  992. }
  993.  
  994. unsigned int hweight16(unsigned int w)
  995. {
  996.     unsigned int res = w - ((w >> 1) & 0x5555);
  997.     res = (res & 0x3333) + ((res >> 2) & 0x3333);
  998.     res = (res + (res >> 4)) & 0x0F0F;
  999.     return (res + (res >> 8)) & 0x00FF;
  1000. }
  1001.  
  1002.  
  1003. unsigned long round_jiffies_up_relative(unsigned long j)
  1004. {
  1005.     unsigned long j0 = GetTimerTicks();
  1006.  
  1007.         /* Use j0 because jiffies might change while we run */
  1008.     return round_jiffies_common(j + j0, true) - j0;
  1009. }
  1010.  
  1011.  
  1012.