Subversion Repositories Kolibri OS

Rev

Rev 4557 | Rev 5060 | 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. static display_t *os_display;
  68. struct drm_i915_gem_object *main_fb_obj;
  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.  
  87. static char *manufacturer_name(unsigned char *x)
  88. {
  89.     static char name[4];
  90.  
  91.     name[0] = ((x[0] & 0x7C) >> 2) + '@';
  92.     name[1] = ((x[0] & 0x03) << 3) + ((x[1] & 0xE0) >> 5) + '@';
  93.     name[2] = (x[1] & 0x1F) + '@';
  94.     name[3] = 0;
  95.  
  96.     return name;
  97. }
  98.  
  99. bool set_mode(struct drm_device *dev, struct drm_connector *connector,
  100.               videomode_t *reqmode, bool strict)
  101. {
  102.     drm_i915_private_t      *dev_priv   = dev->dev_private;
  103.     struct drm_fb_helper    *fb_helper  = &dev_priv->fbdev->helper;
  104.  
  105.     struct drm_mode_config  *config     = &dev->mode_config;
  106.     struct drm_display_mode *mode       = NULL, *tmpmode;
  107.     struct drm_framebuffer  *fb         = NULL;
  108.     struct drm_crtc         *crtc;
  109.     struct drm_encoder      *encoder;
  110.     struct drm_mode_set     set;
  111.     const char *con_name;
  112.     const char *enc_name;
  113.     unsigned hdisplay, vdisplay;
  114.     int stride;
  115.     int ret;
  116.  
  117.     mutex_lock(&dev->mode_config.mutex);
  118.  
  119.     list_for_each_entry(tmpmode, &connector->modes, head)
  120.     {
  121.         if( (drm_mode_width(tmpmode)    == reqmode->width)  &&
  122.             (drm_mode_height(tmpmode)   == reqmode->height) &&
  123.             (drm_mode_vrefresh(tmpmode) == reqmode->freq) )
  124.         {
  125.             mode = tmpmode;
  126.             goto do_set;
  127.         }
  128.     };
  129.  
  130.     if( (mode == NULL) && (strict == false) )
  131.     {
  132.         list_for_each_entry(tmpmode, &connector->modes, head)
  133.         {
  134.             if( (drm_mode_width(tmpmode)  == reqmode->width)  &&
  135.                 (drm_mode_height(tmpmode) == reqmode->height) )
  136.             {
  137.                 mode = tmpmode;
  138.                 goto do_set;
  139.             }
  140.         };
  141.     };
  142.  
  143.     DRM_ERROR("%s failed\n", __FUNCTION__);
  144.  
  145.     return -1;
  146.  
  147. do_set:
  148.  
  149.     encoder = connector->encoder;
  150.     crtc = encoder->crtc;
  151.  
  152.     con_name = drm_get_connector_name(connector);
  153.     enc_name = drm_get_encoder_name(encoder);
  154.  
  155.     DRM_DEBUG_KMS("set mode %d %d: crtc %d connector %s encoder %s\n",
  156.               reqmode->width, reqmode->height, crtc->base.id,
  157.               con_name, enc_name);
  158.  
  159.     drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
  160.  
  161.     hdisplay = mode->hdisplay;
  162.     vdisplay = mode->vdisplay;
  163.  
  164.     if (crtc->invert_dimensions)
  165.         swap(hdisplay, vdisplay);
  166.  
  167.     fb = fb_helper->fb;
  168.  
  169.     fb->width  = reqmode->width;
  170.     fb->height = reqmode->height;
  171.  
  172.     if(dev_priv->mm.bit_6_swizzle_x != I915_BIT_6_SWIZZLE_NONE)
  173.     {
  174.         main_fb_obj->tiling_mode = I915_TILING_X;
  175.  
  176.         if(IS_GEN3(dev))
  177.             for (stride = 512; stride < reqmode->width * 4; stride <<= 1);
  178.         else
  179.             stride = ALIGN(reqmode->width * 4, 512);
  180.     }
  181.     else
  182.     {
  183.         main_fb_obj->tiling_mode = I915_TILING_NONE;
  184.         stride = ALIGN(reqmode->width * 4, 64);
  185.     }
  186.  
  187.     fb->pitches[0]  =
  188.     fb->pitches[1]  =
  189.     fb->pitches[2]  =
  190.     fb->pitches[3]  = stride;
  191.  
  192.     main_fb_obj->stride  = stride;
  193.  
  194.     fb->bits_per_pixel = 32;
  195.     fb->depth = 24;
  196.  
  197.     crtc->fb = fb;
  198.     crtc->enabled = true;
  199.     os_display->crtc = crtc;
  200.  
  201.     i915_gem_object_put_fence(main_fb_obj);
  202.  
  203.     set.crtc = crtc;
  204.     set.x = 0;
  205.     set.y = 0;
  206.     set.mode = mode;
  207.     set.connectors = &connector;
  208.     set.num_connectors = 1;
  209.     set.fb = fb;
  210.     ret = crtc->funcs->set_config(&set);
  211.     mutex_unlock(&dev->mode_config.mutex);
  212.  
  213.     if ( !ret )
  214.     {
  215.         os_display->width    = fb->width;
  216.         os_display->height   = fb->height;
  217.         os_display->vrefresh = drm_mode_vrefresh(mode);
  218.  
  219.         sysSetScreen(fb->width, fb->height, fb->pitches[0]);
  220.  
  221.         DRM_DEBUG_KMS("new mode %d x %d pitch %d\n",
  222.                        fb->width, fb->height, fb->pitches[0]);
  223.     }
  224.     else
  225.         DRM_ERROR("failed to set mode %d_%d on crtc %p\n",
  226.                    fb->width, fb->height, crtc);
  227.  
  228.     return ret;
  229. }
  230.  
  231. static int count_connector_modes(struct drm_connector* connector)
  232. {
  233.     struct drm_display_mode  *mode;
  234.     int count = 0;
  235.  
  236.     list_for_each_entry(mode, &connector->modes, head)
  237.     {
  238.         count++;
  239.     };
  240.     return count;
  241. };
  242.  
  243. static struct drm_connector* get_def_connector(struct drm_device *dev)
  244. {
  245.     struct drm_connector  *connector;
  246.     struct drm_connector_helper_funcs *connector_funcs;
  247.  
  248.     struct drm_connector  *def_connector = NULL;
  249.  
  250.     list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  251.     {
  252.         struct drm_encoder  *encoder;
  253.         struct drm_crtc     *crtc;
  254.  
  255.         if( connector->status != connector_status_connected)
  256.             continue;
  257.  
  258.         connector_funcs = connector->helper_private;
  259.         encoder = connector_funcs->best_encoder(connector);
  260.         if( encoder == NULL)
  261.             continue;
  262.  
  263.         connector->encoder = encoder;
  264.  
  265.         crtc = encoder->crtc;
  266.  
  267.         DRM_DEBUG_KMS("CONNECTOR %x ID:  %d status %d encoder %x\n crtc %x",
  268.                    connector, connector->base.id,
  269.                    connector->status, connector->encoder,
  270.                    crtc);
  271.  
  272. //        if (crtc == NULL)
  273. //            continue;
  274.  
  275.         def_connector = connector;
  276.  
  277.         break;
  278.     };
  279.  
  280.     return def_connector;
  281. };
  282.  
  283. struct drm_connector *get_active_connector(struct drm_device *dev)
  284. {
  285.     struct drm_connector *tmp = NULL;
  286.     struct drm_connector_helper_funcs *connector_funcs;
  287.     struct drm_encoder   *encoder;
  288.  
  289.     list_for_each_entry(tmp, &dev->mode_config.connector_list, head)
  290.     {
  291.         if( tmp->status != connector_status_connected)
  292.             continue;
  293.  
  294.         connector_funcs = tmp->helper_private;
  295.         encoder = connector_funcs->best_encoder(tmp);
  296.         if( encoder == NULL)
  297.         {
  298.             DRM_DEBUG_KMS("CONNECTOR %x ID: %d no active encoders\n",
  299.                       tmp, tmp->base.id);
  300.             continue;
  301.         };
  302.  
  303.         tmp->encoder = encoder;
  304.  
  305.         DRM_DEBUG_KMS("CONNECTOR %p ID:%d status:%d ENCODER %x CRTC %p ID:%d\n",
  306.                tmp, tmp->base.id, tmp->status, tmp->encoder,
  307.                tmp->encoder->crtc, tmp->encoder->crtc->base.id );
  308.  
  309.         return tmp;
  310.     };
  311.  
  312.     return NULL;
  313. }
  314.  
  315. struct drm_crtc *get_possible_crtc(struct drm_device *dev, struct drm_encoder *encoder)
  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.         if (encoder->possible_crtcs & crtc_mask)
  323.         {
  324.             encoder->crtc = tmp_crtc;
  325.             dbgprintf("CRTC %p\n", tmp_crtc);
  326.             return tmp_crtc;
  327.         };
  328.         crtc_mask <<= 1;
  329.     };
  330.     return NULL;
  331. };
  332.  
  333. int get_boot_mode(struct drm_connector *connector, videomode_t *usermode)
  334. {
  335.     struct drm_display_mode *mode;
  336.  
  337.     list_for_each_entry(mode, &connector->modes, head)
  338.     {
  339.         DRM_DEBUG_KMS("check mode w:%d h:%d %dHz\n",
  340.                 drm_mode_width(mode), drm_mode_height(mode),
  341.                 drm_mode_vrefresh(mode));
  342.  
  343.         if( os_display->width  == drm_mode_width(mode)  &&
  344.             os_display->height == drm_mode_height(mode) &&
  345.             drm_mode_vrefresh(mode) == 60)
  346.         {
  347.             usermode->width  = os_display->width;
  348.             usermode->height = os_display->height;
  349.             usermode->freq   = 60;
  350.             return 1;
  351.         }
  352.     }
  353.     return 0;
  354. }
  355.  
  356. int init_display_kms(struct drm_device *dev, videomode_t *usermode)
  357. {
  358.     struct drm_connector    *connector;
  359.     struct drm_connector_helper_funcs *connector_funcs;
  360.     struct drm_encoder      *encoder;
  361.     struct drm_crtc         *crtc;
  362.     struct drm_framebuffer  *fb;
  363.  
  364.     cursor_t  *cursor;
  365.     u32_t      ifl;
  366.     int        err;
  367.  
  368.     mutex_lock(&dev->mode_config.mutex);
  369.  
  370.     connector = get_active_connector(dev) ;
  371.     if(connector == NULL)
  372.     {
  373.         DRM_DEBUG_KMS("No active connectors!\n");
  374.         mutex_unlock(&dev->mode_config.mutex);
  375.         return -1;
  376.     };
  377.  
  378.     encoder = connector->encoder;
  379.     crtc = encoder->crtc;
  380.  
  381.     if(crtc == NULL)
  382.         crtc = get_possible_crtc(dev, encoder);
  383.  
  384.     if(crtc == NULL)
  385.     {
  386.         DRM_DEBUG_KMS("No CRTC for encoder %d\n", encoder->base.id);
  387.         mutex_unlock(&dev->mode_config.mutex);
  388.         return -1;
  389.     };
  390.  
  391.     DRM_DEBUG_KMS("[Select CRTC: %p ID:%d]\n",crtc, crtc->base.id);
  392.  
  393.     os_display = GetDisplay();
  394.     os_display->ddev = dev;
  395.     os_display->connector = connector;
  396.     os_display->crtc = crtc;
  397.  
  398.     os_display->supported_modes = count_connector_modes(connector);
  399.  
  400.  
  401.     ifl = safe_cli();
  402.     {
  403.         struct intel_crtc *intel_crtc = to_intel_crtc(os_display->crtc);
  404.  
  405.         list_for_each_entry(cursor, &os_display->cursors, list)
  406.         {
  407.             init_cursor(cursor);
  408.         };
  409.  
  410.         os_display->restore_cursor(0,0);
  411.         os_display->init_cursor    = init_cursor;
  412.         os_display->select_cursor  = select_cursor_kms;
  413.         os_display->show_cursor    = NULL;
  414.         os_display->move_cursor    = move_cursor_kms;
  415.         os_display->restore_cursor = restore_cursor;
  416.         os_display->disable_mouse  = disable_mouse;
  417.  
  418.         intel_crtc->cursor_x = os_display->width/2;
  419.         intel_crtc->cursor_y = os_display->height/2;
  420.  
  421.         select_cursor_kms(os_display->cursor);
  422.     };
  423.     safe_sti(ifl);
  424.  
  425.     if( (usermode->width == 0) ||
  426.         (usermode->height == 0))
  427.     {
  428.         if( !get_boot_mode(connector, usermode))
  429.         {
  430.             struct drm_display_mode *mode;
  431.  
  432.             mode = list_entry(connector->modes.next, typeof(*mode), head);
  433.             usermode->width  = drm_mode_width(mode);
  434.             usermode->height = drm_mode_height(mode);
  435.             usermode->freq   = drm_mode_vrefresh(mode);
  436.         };
  437.     };
  438.  
  439.     mutex_unlock(&dev->mode_config.mutex);
  440.  
  441.     set_mode(dev, os_display->connector, usermode, false);
  442.  
  443. #ifdef __HWA__
  444.     err = init_bitmaps();
  445. #endif
  446.  
  447.     LEAVE();
  448.  
  449.     return 0;
  450. };
  451.  
  452.  
  453. int get_videomodes(videomode_t *mode, int *count)
  454. {
  455.     int err = -1;
  456.  
  457. //    dbgprintf("mode %x count %d\n", mode, *count);
  458.  
  459.     if( *count == 0 )
  460.     {
  461.         *count = os_display->supported_modes;
  462.         err = 0;
  463.     }
  464.     else if( mode != NULL )
  465.     {
  466.         struct drm_display_mode  *drmmode;
  467.         int i = 0;
  468.  
  469.         if( *count > os_display->supported_modes)
  470.             *count = os_display->supported_modes;
  471.  
  472.         list_for_each_entry(drmmode, &os_display->connector->modes, head)
  473.         {
  474.             if( i < *count)
  475.             {
  476.                 mode->width  = drm_mode_width(drmmode);
  477.                 mode->height = drm_mode_height(drmmode);
  478.                 mode->bpp    = 32;
  479.                 mode->freq   = drm_mode_vrefresh(drmmode);
  480.                 i++;
  481.                 mode++;
  482.             }
  483.             else break;
  484.         };
  485.         *count = i;
  486.         err = 0;
  487.     };
  488.     return err;
  489. };
  490.  
  491. int set_user_mode(videomode_t *mode)
  492. {
  493.     int err = -1;
  494.  
  495. //    dbgprintf("width %d height %d vrefresh %d\n",
  496. //               mode->width, mode->height, mode->freq);
  497.  
  498.     if( (mode->width  != 0)  &&
  499.         (mode->height != 0)  &&
  500.         (mode->freq   != 0 ) &&
  501.         ( (mode->width   != os_display->width)  ||
  502.           (mode->height  != os_display->height) ||
  503.           (mode->freq    != os_display->vrefresh) ) )
  504.     {
  505.         if( set_mode(os_display->ddev, os_display->connector, mode, true) )
  506.             err = 0;
  507.     };
  508.  
  509.     return err;
  510. };
  511.  
  512. void i915_dpms(struct drm_device *dev, int mode)
  513. {
  514.     const struct drm_connector_funcs *f = os_display->connector->funcs;
  515.  
  516.     f->dpms(os_display->connector, mode);
  517. };
  518.  
  519. void __attribute__((regparm(1))) destroy_cursor(cursor_t *cursor)
  520. {
  521.     list_del(&cursor->list);
  522.  
  523.     i915_gem_object_unpin(cursor->cobj);
  524.  
  525.     mutex_lock(&main_device->struct_mutex);
  526.     drm_gem_object_unreference(&cursor->cobj->base);
  527.     mutex_unlock(&main_device->struct_mutex);
  528.  
  529.     __DestroyObject(cursor);
  530. };
  531.  
  532. int init_cursor(cursor_t *cursor)
  533. {
  534.     struct drm_i915_private *dev_priv = os_display->ddev->dev_private;
  535.     struct drm_i915_gem_object *obj;
  536.     uint32_t *bits;
  537.     uint32_t *src;
  538.     void     *mapped;
  539.  
  540.     int       i,j;
  541.     int       ret;
  542.  
  543.     if (dev_priv->info->cursor_needs_physical)
  544.     {
  545.         bits = (uint32_t*)KernelAlloc(CURSOR_WIDTH*CURSOR_HEIGHT*4);
  546.         if (unlikely(bits == NULL))
  547.             return ENOMEM;
  548.         cursor->cobj = (struct drm_i915_gem_object *)GetPgAddr(bits);
  549.     }
  550.     else
  551.     {
  552.         obj = i915_gem_alloc_object(os_display->ddev, CURSOR_WIDTH*CURSOR_HEIGHT*4);
  553.         if (unlikely(obj == NULL))
  554.             return -ENOMEM;
  555.  
  556.         ret = i915_gem_obj_ggtt_pin(obj, CURSOR_WIDTH*CURSOR_HEIGHT*4, true, true);
  557.         if (ret) {
  558.             drm_gem_object_unreference(&obj->base);
  559.             return ret;
  560.         }
  561.  
  562.         ret = i915_gem_object_set_to_gtt_domain(obj, true);
  563.         if (ret)
  564.         {
  565.             i915_gem_object_unpin(obj);
  566.             drm_gem_object_unreference(&obj->base);
  567.             return ret;
  568.         }
  569. /* You don't need to worry about fragmentation issues.
  570.  * GTT space is continuous. I guarantee it.                           */
  571.  
  572.         mapped = bits = (u32*)MapIoMem(dev_priv->gtt.mappable_base + i915_gem_obj_ggtt_offset(obj),
  573.                     CURSOR_WIDTH*CURSOR_HEIGHT*4, PG_SW);
  574.  
  575.         if (unlikely(bits == NULL))
  576.         {
  577.             i915_gem_object_unpin(obj);
  578.             drm_gem_object_unreference(&obj->base);
  579.             return -ENOMEM;
  580.         };
  581.         cursor->cobj = obj;
  582.     };
  583.  
  584.     src = cursor->data;
  585.  
  586.     for(i = 0; i < 32; i++)
  587.     {
  588.         for(j = 0; j < 32; j++)
  589.             *bits++ = *src++;
  590.         for(j = 32; j < CURSOR_WIDTH; j++)
  591.             *bits++ = 0;
  592.     }
  593.     for(i = 0; i < CURSOR_WIDTH*(CURSOR_HEIGHT-32); i++)
  594.         *bits++ = 0;
  595.  
  596.     FreeKernelSpace(mapped);
  597.  
  598. // release old cursor
  599.  
  600.     KernelFree(cursor->data);
  601.  
  602.     cursor->data = bits;
  603.  
  604.     cursor->header.destroy = destroy_cursor;
  605.  
  606.     return 0;
  607. }
  608.  
  609.  
  610. void __stdcall move_cursor_kms(cursor_t *cursor, int x, int y)
  611. {
  612.     struct drm_crtc *crtc = os_display->crtc;
  613.     x-= cursor->hot_x;
  614.     y-= cursor->hot_y;
  615.  
  616.     if (crtc->funcs->cursor_move)
  617.         crtc->funcs->cursor_move(crtc, x, y);
  618.  
  619. };
  620.  
  621.  
  622. cursor_t* __stdcall select_cursor_kms(cursor_t *cursor)
  623. {
  624.     struct drm_i915_private *dev_priv = os_display->ddev->dev_private;
  625.     struct intel_crtc *intel_crtc = to_intel_crtc(os_display->crtc);
  626.     cursor_t *old;
  627.  
  628.     old = os_display->cursor;
  629.     os_display->cursor = cursor;
  630.  
  631.     intel_crtc->cursor_bo = cursor->cobj;
  632.  
  633.     if (!dev_priv->info->cursor_needs_physical)
  634.        intel_crtc->cursor_addr = i915_gem_obj_ggtt_offset(cursor->cobj);
  635.     else
  636.         intel_crtc->cursor_addr = (addr_t)cursor->cobj;
  637.  
  638.     intel_crtc->cursor_width = 32;
  639.     intel_crtc->cursor_height = 32;
  640.  
  641.     move_cursor_kms(cursor, intel_crtc->cursor_x, intel_crtc->cursor_y);
  642.     return old;
  643. };
  644.  
  645. int i915_fbinfo(struct drm_i915_fb_info *fb)
  646. {
  647.     struct drm_i915_private *dev_priv = os_display->ddev->dev_private;
  648.     struct intel_crtc *crtc = to_intel_crtc(os_display->crtc);
  649.  
  650.     struct drm_i915_gem_object *obj = get_fb_obj();
  651.  
  652.     fb->name   = obj->base.name;
  653.     fb->width  = os_display->width;
  654.     fb->height = os_display->height;
  655.     fb->pitch  = obj->stride;
  656.     fb->tiling = obj->tiling_mode;
  657.     fb->crtc   = crtc->base.base.id;
  658.     fb->pipe   = crtc->pipe;
  659.  
  660.     return 0;
  661. }
  662.  
  663.  
  664. typedef struct
  665. {
  666.     int left;
  667.     int top;
  668.     int right;
  669.     int bottom;
  670. }rect_t;
  671.  
  672.  
  673. #define CURRENT_TASK             (0x80003000)
  674.  
  675. static u32_t get_display_map()
  676. {
  677.     u32_t   addr;
  678.  
  679.     addr = (u32_t)os_display;
  680.     addr+= sizeof(display_t);            /*  shoot me  */
  681.     return *(u32_t*)addr;
  682. }
  683.  
  684. void  FASTCALL GetWindowRect(rect_t *rc)__asm__("GetWindowRect");
  685.  
  686. int i915_mask_update(struct drm_device *dev, void *data,
  687.             struct drm_file *file)
  688. {
  689.     struct drm_i915_mask *mask = data;
  690.     struct drm_gem_object *obj;
  691.     static unsigned int mask_seqno[256];
  692.     rect_t winrc;
  693.     u32    slot;
  694.     int    ret=0;
  695.  
  696.     obj = drm_gem_object_lookup(dev, file, mask->handle);
  697.     if (obj == NULL)
  698.         return -ENOENT;
  699.  
  700.     if (!obj->filp) {
  701.         drm_gem_object_unreference_unlocked(obj);
  702.         return -EINVAL;
  703.     }
  704.  
  705.     GetWindowRect(&winrc);
  706.     {
  707. //        static warn_count;
  708.  
  709.         mask->width    = winrc.right - winrc.left + 1;
  710.         mask->height   = winrc.bottom - winrc.top + 1;
  711.         mask->bo_pitch = (mask->width+15) & ~15;
  712.  
  713. #if 0
  714.         if(warn_count < 1)
  715.         {
  716.             printf("left %d top %d right %d bottom %d\n",
  717.                     winrc.left, winrc.top, winrc.right, winrc.bottom);
  718.             printf("mask pitch %d data %p\n", mask->bo_pitch, mask->bo_size);
  719.             warn_count++;
  720.         };
  721. #endif
  722.  
  723.      };
  724.  
  725.  
  726.     slot = *((u8*)CURRENT_TASK);
  727.  
  728.     if( mask_seqno[slot] != os_display->mask_seqno)
  729.     {
  730.         u8* src_offset;
  731.         u8* dst_offset;
  732.         u32 ifl;
  733.  
  734.         ret = i915_mutex_lock_interruptible(dev);
  735.         if (ret)
  736.             goto err1;
  737.  
  738.         ret = i915_gem_object_set_to_cpu_domain(to_intel_bo(obj), true);
  739.         if(ret != 0 )
  740.         {
  741.             dbgprintf("%s: i915_gem_object_set_to_cpu_domain failed\n", __FUNCTION__);
  742.             goto err2;
  743.         };
  744.  
  745. //        printf("width %d height %d\n", winrc.right, winrc.bottom);
  746.  
  747. //        slot = 0x01;
  748.  
  749.  
  750.         src_offset = (u8*)( winrc.top*os_display->width + winrc.left);
  751.         src_offset+= get_display_map();
  752.         dst_offset = (u8*)mask->bo_map;
  753.  
  754.         u32_t tmp_h = mask->height;
  755.  
  756.         ifl = safe_cli();
  757.         {
  758.             mask_seqno[slot] = os_display->mask_seqno;
  759.  
  760.             slot|= (slot<<8)|(slot<<16)|(slot<<24);
  761.  
  762.             __asm__ __volatile__ (
  763.             "movd       %[slot],   %%xmm6         \n"
  764.             "punpckldq  %%xmm6, %%xmm6            \n"
  765.             "punpcklqdq %%xmm6, %%xmm6            \n"
  766.             :: [slot]  "m" (slot)
  767.             :"xmm6");
  768.  
  769.             while( tmp_h--)
  770.             {
  771.                 int tmp_w = mask->width;
  772.  
  773.                 u8* tmp_src = src_offset;
  774.                 u8* tmp_dst = dst_offset;
  775.  
  776.                 src_offset+= os_display->width;
  777.                 dst_offset+= mask->bo_pitch;
  778.  
  779.                 while(tmp_w >= 64)
  780.                 {
  781.                     __asm__ __volatile__ (
  782.                     "movdqu     (%0),   %%xmm0            \n"
  783.                     "movdqu   16(%0),   %%xmm1            \n"
  784.                     "movdqu   32(%0),   %%xmm2            \n"
  785.                     "movdqu   48(%0),   %%xmm3            \n"
  786.                     "pcmpeqb    %%xmm6, %%xmm0            \n"
  787.                     "pcmpeqb    %%xmm6, %%xmm1            \n"
  788.                     "pcmpeqb    %%xmm6, %%xmm2            \n"
  789.                     "pcmpeqb    %%xmm6, %%xmm3            \n"
  790.                     "movdqa     %%xmm0,   (%%edi)         \n"
  791.                     "movdqa     %%xmm1, 16(%%edi)         \n"
  792.                     "movdqa     %%xmm2, 32(%%edi)         \n"
  793.                     "movdqa     %%xmm3, 48(%%edi)         \n"
  794.  
  795.                     :: "r" (tmp_src), "D" (tmp_dst)
  796.                     :"xmm0","xmm1","xmm2","xmm3");
  797.                     tmp_w -= 64;
  798.                     tmp_src += 64;
  799.                     tmp_dst += 64;
  800.                 }
  801.  
  802.                 if( tmp_w >= 32 )
  803.                 {
  804.                     __asm__ __volatile__ (
  805.                     "movdqu     (%0),   %%xmm0            \n"
  806.                     "movdqu   16(%0),   %%xmm1            \n"
  807.                     "pcmpeqb    %%xmm6, %%xmm0            \n"
  808.                     "pcmpeqb    %%xmm6, %%xmm1            \n"
  809.                     "movdqa     %%xmm0,   (%%edi)         \n"
  810.                     "movdqa     %%xmm1, 16(%%edi)         \n"
  811.  
  812.                     :: "r" (tmp_src), "D" (tmp_dst)
  813.                     :"xmm0","xmm1");
  814.                     tmp_w -= 32;
  815.                     tmp_src += 32;
  816.                     tmp_dst += 32;
  817.                 }
  818.  
  819.                 if( tmp_w >= 16 )
  820.                 {
  821.                     __asm__ __volatile__ (
  822.                     "movdqu     (%0),   %%xmm0            \n"
  823.                     "pcmpeqb    %%xmm6, %%xmm0            \n"
  824.                     "movdqa     %%xmm0,   (%%edi)         \n"
  825.                     :: "r" (tmp_src), "D" (tmp_dst)
  826.                     :"xmm0");
  827.                     tmp_w -= 16;
  828.                     tmp_src += 16;
  829.                     tmp_dst += 16;
  830.                 }
  831.  
  832.                 if( tmp_w >= 8 )
  833.                 {
  834.                     __asm__ __volatile__ (
  835.                     "movq       (%0),   %%xmm0            \n"
  836.                     "pcmpeqb    %%xmm6, %%xmm0            \n"
  837.                     "movq       %%xmm0,   (%%edi)         \n"
  838.                     :: "r" (tmp_src), "D" (tmp_dst)
  839.                     :"xmm0");
  840.                     tmp_w -= 8;
  841.                     tmp_src += 8;
  842.                     tmp_dst += 8;
  843.                 }
  844.                 if( tmp_w >= 4 )
  845.                 {
  846.                     __asm__ __volatile__ (
  847.                     "movd       (%0),   %%xmm0            \n"
  848.                     "pcmpeqb    %%xmm6, %%xmm0            \n"
  849.                     "movd       %%xmm0,   (%%edi)         \n"
  850.                     :: "r" (tmp_src), "D" (tmp_dst)
  851.                     :"xmm0");
  852.                     tmp_w -= 4;
  853.                     tmp_src += 4;
  854.                     tmp_dst += 4;
  855.                 }
  856.                 while(tmp_w--)
  857.                     *tmp_dst++ = (*tmp_src++ == (u8)slot) ? 0xFF:0x00;
  858.             };
  859.         };
  860.         safe_sti(ifl);
  861.  
  862.         ret = i915_gem_object_set_to_gtt_domain(to_intel_bo(obj), false);
  863.     }
  864.  
  865. err2:
  866.     mutex_unlock(&dev->struct_mutex);
  867. err1:
  868.     drm_gem_object_unreference(obj);
  869.  
  870.     return ret;
  871. }
  872.  
  873. int i915_mask_update_ex(struct drm_device *dev, void *data,
  874.             struct drm_file *file)
  875. {
  876.     struct drm_i915_mask_update *mask = data;
  877.     struct drm_gem_object *obj;
  878.     static unsigned int mask_seqno[256];
  879.     static warn_count;
  880.  
  881.     rect_t win;
  882.     u32    winw,winh;
  883.     u32    ml,mt,mr,mb;
  884.     u32    slot;
  885.     int    ret = 0;
  886.     slot = *((u8*)CURRENT_TASK);
  887.  
  888.     if( mask_seqno[slot] == os_display->mask_seqno)
  889.         return 0;
  890.  
  891.     GetWindowRect(&win);
  892.     win.right+= 1;
  893.     win.bottom+=  1;
  894.  
  895.     winw = win.right - win.left;
  896.     winh = win.bottom - win.top;
  897.  
  898.     if(mask->dx >= winw ||
  899.        mask->dy >= winh)
  900.        return 1;
  901.  
  902.     ml = win.left + mask->dx;
  903.     mt = win.top  + mask->dy;
  904.     mr = ml + mask->width;
  905.     mb = mt + mask->height;
  906.  
  907.     if( ml >= win.right || mt >= win.bottom ||
  908.         mr < win.left   || mb < win.top )
  909.         return 1;
  910.  
  911.     if( mr > win.right )
  912.         mr = win.right;
  913.  
  914.     if( mb > win.bottom )
  915.         mb = win.bottom;
  916.  
  917.     mask->width  = mr - ml;
  918.     mask->height = mb - mt;
  919.  
  920.     if( mask->width == 0 ||
  921.         mask->height== 0 )
  922.         return 1;
  923.  
  924.     obj = drm_gem_object_lookup(dev, file, mask->handle);
  925.     if (obj == NULL)
  926.         return -ENOENT;
  927.  
  928.     if (!obj->filp) {
  929.         drm_gem_object_unreference_unlocked(obj);
  930.         return -EINVAL;
  931.     }
  932.  
  933. #if 0
  934.     if(warn_count < 1000)
  935.     {
  936.         printf("left %d top %d right %d bottom %d\n",
  937.                 ml, mt, mr, mb);
  938.         warn_count++;
  939.     };
  940. #endif
  941.  
  942.  
  943. #if 1
  944.  
  945.     {
  946.         u8* src_offset;
  947.         u8* dst_offset;
  948.         u32 ifl;
  949.  
  950.         ret = i915_mutex_lock_interruptible(dev);
  951.         if (ret)
  952.             goto err1;
  953.  
  954.         i915_gem_object_set_to_cpu_domain(to_intel_bo(obj), true);
  955.  
  956.         src_offset = (u8*)( mt*os_display->width + ml);
  957.         src_offset+= get_display_map();
  958.         dst_offset = (u8*)mask->bo_map;
  959.  
  960.         u32_t tmp_h = mask->height;
  961.  
  962.         ifl = safe_cli();
  963.         {
  964.             mask_seqno[slot] = os_display->mask_seqno;
  965.  
  966.             slot|= (slot<<8)|(slot<<16)|(slot<<24);
  967.  
  968.             __asm__ __volatile__ (
  969.             "movd       %[slot],   %%xmm6         \n"
  970.             "punpckldq  %%xmm6, %%xmm6            \n"
  971.             "punpcklqdq %%xmm6, %%xmm6            \n"
  972.             :: [slot]  "m" (slot)
  973.             :"xmm6");
  974.  
  975.             while( tmp_h--)
  976.             {
  977.                 int tmp_w = mask->width;
  978.  
  979.                 u8* tmp_src = src_offset;
  980.                 u8* tmp_dst = dst_offset;
  981.  
  982.                 src_offset+= os_display->width;
  983.                 dst_offset+= mask->bo_pitch;
  984.  
  985.                 while(tmp_w >= 64)
  986.                 {
  987.                     __asm__ __volatile__ (
  988.                     "movdqu     (%0),   %%xmm0            \n"
  989.                     "movdqu   16(%0),   %%xmm1            \n"
  990.                     "movdqu   32(%0),   %%xmm2            \n"
  991.                     "movdqu   48(%0),   %%xmm3            \n"
  992.                     "pcmpeqb    %%xmm6, %%xmm0            \n"
  993.                     "pcmpeqb    %%xmm6, %%xmm1            \n"
  994.                     "pcmpeqb    %%xmm6, %%xmm2            \n"
  995.                     "pcmpeqb    %%xmm6, %%xmm3            \n"
  996.                     "movdqa     %%xmm0,   (%%edi)         \n"
  997.                     "movdqa     %%xmm1, 16(%%edi)         \n"
  998.                     "movdqa     %%xmm2, 32(%%edi)         \n"
  999.                     "movdqa     %%xmm3, 48(%%edi)         \n"
  1000.  
  1001.                     :: "r" (tmp_src), "D" (tmp_dst)
  1002.                     :"xmm0","xmm1","xmm2","xmm3");
  1003.                     tmp_w -= 64;
  1004.                     tmp_src += 64;
  1005.                     tmp_dst += 64;
  1006.                 }
  1007.  
  1008.                 if( tmp_w >= 32 )
  1009.                 {
  1010.                     __asm__ __volatile__ (
  1011.                     "movdqu     (%0),   %%xmm0            \n"
  1012.                     "movdqu   16(%0),   %%xmm1            \n"
  1013.                     "pcmpeqb    %%xmm6, %%xmm0            \n"
  1014.                     "pcmpeqb    %%xmm6, %%xmm1            \n"
  1015.                     "movdqa     %%xmm0,   (%%edi)         \n"
  1016.                     "movdqa     %%xmm1, 16(%%edi)         \n"
  1017.  
  1018.                     :: "r" (tmp_src), "D" (tmp_dst)
  1019.                     :"xmm0","xmm1");
  1020.                     tmp_w -= 32;
  1021.                     tmp_src += 32;
  1022.                     tmp_dst += 32;
  1023.                 }
  1024.  
  1025.                 if( tmp_w >= 16 )
  1026.                 {
  1027.                     __asm__ __volatile__ (
  1028.                     "movdqu     (%0),   %%xmm0            \n"
  1029.                     "pcmpeqb    %%xmm6, %%xmm0            \n"
  1030.                     "movdqa     %%xmm0,   (%%edi)         \n"
  1031.                     :: "r" (tmp_src), "D" (tmp_dst)
  1032.                     :"xmm0");
  1033.                     tmp_w -= 16;
  1034.                     tmp_src += 16;
  1035.                     tmp_dst += 16;
  1036.                 }
  1037.  
  1038.                 if( tmp_w >= 8 )
  1039.                 {
  1040.                     __asm__ __volatile__ (
  1041.                     "movq       (%0),   %%xmm0            \n"
  1042.                     "pcmpeqb    %%xmm6, %%xmm0            \n"
  1043.                     "movq       %%xmm0,   (%%edi)         \n"
  1044.                     :: "r" (tmp_src), "D" (tmp_dst)
  1045.                     :"xmm0");
  1046.                     tmp_w -= 8;
  1047.                     tmp_src += 8;
  1048.                     tmp_dst += 8;
  1049.                 }
  1050.                 if( tmp_w >= 4 )
  1051.                 {
  1052.                     __asm__ __volatile__ (
  1053.                     "movd       (%0),   %%xmm0            \n"
  1054.                     "pcmpeqb    %%xmm6, %%xmm0            \n"
  1055.                     "movd       %%xmm0,   (%%edi)         \n"
  1056.                     :: "r" (tmp_src), "D" (tmp_dst)
  1057.                     :"xmm0");
  1058.                     tmp_w -= 4;
  1059.                     tmp_src += 4;
  1060.                     tmp_dst += 4;
  1061.                 }
  1062.                 while(tmp_w--)
  1063.                     *tmp_dst++ = (*tmp_src++ == (u8)slot) ? 0xFF:0x00;
  1064.             };
  1065.         };
  1066.         safe_sti(ifl);
  1067.  
  1068.         i915_gem_object_set_to_gtt_domain(to_intel_bo(obj), false);
  1069.     }
  1070. #endif
  1071.  
  1072. err2:
  1073.     mutex_unlock(&dev->struct_mutex);
  1074. err1:
  1075.     drm_gem_object_unreference(obj);
  1076.  
  1077.     return ret;
  1078. }
  1079.  
  1080.  
  1081.  
  1082.  
  1083.  
  1084.  
  1085.  
  1086.  
  1087.  
  1088.  
  1089.  
  1090. #define NSEC_PER_SEC    1000000000L
  1091.  
  1092. void getrawmonotonic(struct timespec *ts)
  1093. {
  1094.     u32 tmp = GetTimerTicks();
  1095.  
  1096.     ts->tv_sec  = tmp/100;
  1097.     ts->tv_nsec = (tmp - ts->tv_sec*100)*10000000;
  1098. }
  1099.  
  1100. void set_normalized_timespec(struct timespec *ts, time_t sec, s64 nsec)
  1101. {
  1102.     while (nsec >= NSEC_PER_SEC) {
  1103.         /*
  1104.          * The following asm() prevents the compiler from
  1105.          * optimising this loop into a modulo operation. See
  1106.          * also __iter_div_u64_rem() in include/linux/time.h
  1107.          */
  1108.         asm("" : "+rm"(nsec));
  1109.         nsec -= NSEC_PER_SEC;
  1110.         ++sec;
  1111.     }
  1112.     while (nsec < 0) {
  1113.         asm("" : "+rm"(nsec));
  1114.         nsec += NSEC_PER_SEC;
  1115.         --sec;
  1116.     }
  1117.     ts->tv_sec = sec;
  1118.     ts->tv_nsec = nsec;
  1119. }
  1120.  
  1121. void
  1122. prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state)
  1123. {
  1124.     unsigned long flags;
  1125.  
  1126. //    wait->flags &= ~WQ_FLAG_EXCLUSIVE;
  1127.     spin_lock_irqsave(&q->lock, flags);
  1128.     if (list_empty(&wait->task_list))
  1129.             __add_wait_queue(q, wait);
  1130.     spin_unlock_irqrestore(&q->lock, flags);
  1131. }
  1132.  
  1133. /**
  1134.  * finish_wait - clean up after waiting in a queue
  1135.  * @q: waitqueue waited on
  1136.  * @wait: wait descriptor
  1137.  *
  1138.  * Sets current thread back to running state and removes
  1139.  * the wait descriptor from the given waitqueue if still
  1140.  * queued.
  1141.  */
  1142. void finish_wait(wait_queue_head_t *q, wait_queue_t *wait)
  1143. {
  1144.     unsigned long flags;
  1145.  
  1146. //    __set_current_state(TASK_RUNNING);
  1147.     /*
  1148.      * We can check for list emptiness outside the lock
  1149.      * IFF:
  1150.      *  - we use the "careful" check that verifies both
  1151.      *    the next and prev pointers, so that there cannot
  1152.      *    be any half-pending updates in progress on other
  1153.      *    CPU's that we haven't seen yet (and that might
  1154.      *    still change the stack area.
  1155.      * and
  1156.      *  - all other users take the lock (ie we can only
  1157.      *    have _one_ other CPU that looks at or modifies
  1158.      *    the list).
  1159.      */
  1160.     if (!list_empty_careful(&wait->task_list)) {
  1161.             spin_lock_irqsave(&q->lock, flags);
  1162.             list_del_init(&wait->task_list);
  1163.             spin_unlock_irqrestore(&q->lock, flags);
  1164.     }
  1165.  
  1166.     DestroyEvent(wait->evnt);
  1167. }
  1168.  
  1169. int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key)
  1170. {
  1171.     list_del_init(&wait->task_list);
  1172.     return 1;
  1173. }
  1174.  
  1175. unsigned int hweight16(unsigned int w)
  1176. {
  1177.     unsigned int res = w - ((w >> 1) & 0x5555);
  1178.     res = (res & 0x3333) + ((res >> 2) & 0x3333);
  1179.     res = (res + (res >> 4)) & 0x0F0F;
  1180.     return (res + (res >> 8)) & 0x00FF;
  1181. }
  1182.  
  1183.  
  1184. unsigned long round_jiffies_up_relative(unsigned long j)
  1185. {
  1186.     unsigned long j0 = GetTimerTicks();
  1187.  
  1188.         /* Use j0 because jiffies might change while we run */
  1189.     return round_jiffies_common(j + j0, true) - j0;
  1190. }
  1191.  
  1192.  
  1193.