Subversion Repositories Kolibri OS

Rev

Rev 4293 | Rev 4389 | 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.     const char *con_name;
  112.     const 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. struct drm_connector *get_active_connector(struct drm_device *dev)
  267. {
  268.     struct drm_connector *tmp = NULL;
  269.     struct drm_connector_helper_funcs *connector_funcs;
  270.     struct drm_encoder      *encoder;
  271.  
  272.     list_for_each_entry(tmp, &dev->mode_config.connector_list, head)
  273.     {
  274.         if( tmp->status != connector_status_connected)
  275.             continue;
  276.  
  277.         connector_funcs = tmp->helper_private;
  278.         encoder = connector_funcs->best_encoder(tmp);
  279.         if( encoder == NULL)
  280.         {
  281.             DRM_DEBUG_KMS("CONNECTOR %x ID: %d no active encoders\n",
  282.                       tmp, tmp->base.id);
  283.             continue;
  284.         };
  285.  
  286.         tmp->encoder = encoder;
  287.  
  288.         DRM_DEBUG_KMS("CONNECTOR %p ID:%d status:%d ENCODER %x CRTC %p ID:%d\n",
  289.                tmp, tmp->base.id, tmp->status, tmp->encoder,
  290.                tmp->encoder->crtc, tmp->encoder->crtc->base.id );
  291.  
  292.         return tmp;
  293.     };
  294.  
  295.     return NULL;
  296. }
  297.  
  298. struct drm_crtc *get_possible_crtc(struct drm_device *dev, struct drm_encoder *encoder)
  299.     {
  300.         struct drm_crtc *tmp_crtc;
  301.         int crtc_mask = 1;
  302.  
  303.         list_for_each_entry(tmp_crtc, &dev->mode_config.crtc_list, head)
  304.         {
  305.             if (encoder->possible_crtcs & crtc_mask)
  306.             {
  307.             encoder->crtc = tmp_crtc;
  308.             dbgprintf("CRTC %p\n", tmp_crtc);
  309.             return tmp_crtc;
  310.             };
  311.             crtc_mask <<= 1;
  312.         };
  313.     return NULL;
  314.     };
  315.  
  316. int get_boot_mode(struct drm_connector *connector, videomode_t *usermode)
  317. {
  318.     struct drm_display_mode *mode;
  319.  
  320.     list_for_each_entry(mode, &connector->modes, head)
  321.     {
  322.         dbgprintf("check mode w:%d h:%d %dHz\n",
  323.                 drm_mode_width(mode), drm_mode_height(mode),
  324.                 drm_mode_vrefresh(mode));
  325.  
  326.         if( os_display->width  == drm_mode_width(mode)  &&
  327.             os_display->height == drm_mode_height(mode) &&
  328.             drm_mode_vrefresh(mode) == 60)
  329.         {
  330.             usermode->width  = os_display->width;
  331.             usermode->height = os_display->height;
  332.             usermode->freq   = 60;
  333.             return 1;
  334.         }
  335.     }
  336.     return 0;
  337. }
  338.  
  339. int init_display_kms(struct drm_device *dev, videomode_t *usermode)
  340. {
  341.     struct drm_connector    *connector;
  342.     struct drm_connector_helper_funcs *connector_funcs;
  343.     struct drm_encoder      *encoder;
  344.     struct drm_crtc         *crtc;
  345.     struct drm_framebuffer  *fb;
  346.  
  347.     cursor_t  *cursor;
  348.     u32_t      ifl;
  349.     int        err;
  350.  
  351.     ENTER();
  352.  
  353.     mutex_lock(&dev->mode_config.mutex);
  354.  
  355.     connector = get_active_connector(dev) ;
  356.     if(connector == NULL)
  357.     {
  358.         DRM_DEBUG_KMS("No active connectors!\n");
  359.         mutex_unlock(&dev->mode_config.mutex);
  360.         return -1;
  361.     };
  362.  
  363.     encoder = connector->encoder;
  364.     crtc = encoder->crtc;
  365.  
  366.     if(crtc == NULL)
  367.         crtc = get_possible_crtc(dev, encoder);
  368.  
  369.     dbgprintf("CRTC %p\n", crtc);
  370.  
  371.     if(crtc == NULL)
  372.     {
  373.         DRM_DEBUG_KMS("No CRTC for encoder %d\n", encoder->base.id);
  374.         mutex_unlock(&dev->mode_config.mutex);
  375.         return -1;
  376.     };
  377.  
  378.     DRM_DEBUG_KMS("[Select CRTC: %p ID:%d]\n",crtc, crtc->base.id);
  379.  
  380.     os_display = GetDisplay();
  381.     os_display->ddev = dev;
  382.     os_display->connector = connector;
  383.     os_display->crtc = crtc;
  384.  
  385.     os_display->supported_modes = count_connector_modes(connector);
  386.  
  387.  
  388.     ifl = safe_cli();
  389.     {
  390.         struct intel_crtc *intel_crtc = to_intel_crtc(os_display->crtc);
  391.  
  392.         list_for_each_entry(cursor, &os_display->cursors, list)
  393.         {
  394.             init_cursor(cursor);
  395.         };
  396.  
  397.         os_display->restore_cursor(0,0);
  398.         os_display->init_cursor    = init_cursor;
  399.         os_display->select_cursor  = select_cursor_kms;
  400.         os_display->show_cursor    = NULL;
  401.         os_display->move_cursor    = move_cursor_kms;
  402.         os_display->restore_cursor = restore_cursor;
  403.         os_display->disable_mouse  = disable_mouse;
  404.  
  405.         intel_crtc->cursor_x = os_display->width/2;
  406.         intel_crtc->cursor_y = os_display->height/2;
  407.  
  408.         select_cursor_kms(os_display->cursor);
  409.     };
  410.     safe_sti(ifl);
  411.  
  412.     if( (usermode->width == 0) ||
  413.         (usermode->height == 0))
  414.     {
  415.         if( !get_boot_mode(connector, usermode))
  416.         {
  417.         struct drm_display_mode *mode;
  418.  
  419.             mode = list_entry(connector->modes.next, typeof(*mode), head);
  420.             usermode->width  = drm_mode_width(mode);
  421.             usermode->height = drm_mode_height(mode);
  422.             usermode->freq   = drm_mode_vrefresh(mode);
  423.         };
  424.     };
  425.  
  426.     mutex_unlock(&dev->mode_config.mutex);
  427.  
  428.     set_mode(dev, os_display->connector, usermode, false);
  429.  
  430. #ifdef __HWA__
  431.     err = init_bitmaps();
  432. #endif
  433.  
  434.     LEAVE();
  435.  
  436.     return 0;
  437. };
  438.  
  439.  
  440. int get_videomodes(videomode_t *mode, int *count)
  441. {
  442.     int err = -1;
  443.  
  444. //    dbgprintf("mode %x count %d\n", mode, *count);
  445.  
  446.     if( *count == 0 )
  447.     {
  448.         *count = os_display->supported_modes;
  449.         err = 0;
  450.     }
  451.     else if( mode != NULL )
  452.     {
  453.         struct drm_display_mode  *drmmode;
  454.         int i = 0;
  455.  
  456.         if( *count > os_display->supported_modes)
  457.             *count = os_display->supported_modes;
  458.  
  459.         list_for_each_entry(drmmode, &os_display->connector->modes, head)
  460.         {
  461.             if( i < *count)
  462.             {
  463.                 mode->width  = drm_mode_width(drmmode);
  464.                 mode->height = drm_mode_height(drmmode);
  465.                 mode->bpp    = 32;
  466.                 mode->freq   = drm_mode_vrefresh(drmmode);
  467.                 i++;
  468.                 mode++;
  469.             }
  470.             else break;
  471.         };
  472.         *count = i;
  473.         err = 0;
  474.     };
  475.     return err;
  476. };
  477.  
  478. int set_user_mode(videomode_t *mode)
  479. {
  480.     int err = -1;
  481.  
  482. //    dbgprintf("width %d height %d vrefresh %d\n",
  483. //               mode->width, mode->height, mode->freq);
  484.  
  485.     if( (mode->width  != 0)  &&
  486.         (mode->height != 0)  &&
  487.         (mode->freq   != 0 ) &&
  488.         ( (mode->width   != os_display->width)  ||
  489.           (mode->height  != os_display->height) ||
  490.           (mode->freq    != os_display->vrefresh) ) )
  491.     {
  492.         if( set_mode(os_display->ddev, os_display->connector, mode, true) )
  493.             err = 0;
  494.     };
  495.  
  496.     return err;
  497. };
  498.  
  499. void i915_dpms(struct drm_device *dev, int mode)
  500. {
  501.     const struct drm_connector_funcs *f = os_display->connector->funcs;
  502.  
  503.     f->dpms(os_display->connector, mode);
  504. };
  505.  
  506. void __attribute__((regparm(1))) destroy_cursor(cursor_t *cursor)
  507. {
  508.     list_del(&cursor->list);
  509.  
  510.     i915_gem_object_unpin(cursor->cobj);
  511.  
  512.     mutex_lock(&main_device->struct_mutex);
  513.     drm_gem_object_unreference(&cursor->cobj->base);
  514.     mutex_unlock(&main_device->struct_mutex);
  515.  
  516.     __DestroyObject(cursor);
  517. };
  518.  
  519. int init_cursor(cursor_t *cursor)
  520. {
  521.     struct drm_i915_private *dev_priv = os_display->ddev->dev_private;
  522.     struct drm_i915_gem_object *obj;
  523.     uint32_t *bits;
  524.     uint32_t *src;
  525.     void     *mapped;
  526.  
  527.     int       i,j;
  528.     int       ret;
  529.  
  530.     if (dev_priv->info->cursor_needs_physical)
  531.     {
  532.         bits = (uint32_t*)KernelAlloc(CURSOR_WIDTH*CURSOR_HEIGHT*4);
  533.         if (unlikely(bits == NULL))
  534.             return ENOMEM;
  535.         cursor->cobj = (struct drm_i915_gem_object *)GetPgAddr(bits);
  536.     }
  537.     else
  538.     {
  539.         obj = i915_gem_alloc_object(os_display->ddev, CURSOR_WIDTH*CURSOR_HEIGHT*4);
  540.         if (unlikely(obj == NULL))
  541.             return -ENOMEM;
  542.  
  543.         ret = i915_gem_obj_ggtt_pin(obj, CURSOR_WIDTH*CURSOR_HEIGHT*4, true, true);
  544.         if (ret) {
  545.             drm_gem_object_unreference(&obj->base);
  546.             return ret;
  547.         }
  548.  
  549.         ret = i915_gem_object_set_to_gtt_domain(obj, true);
  550.         if (ret)
  551.         {
  552.             i915_gem_object_unpin(obj);
  553.             drm_gem_object_unreference(&obj->base);
  554.             return ret;
  555.         }
  556. /* You don't need to worry about fragmentation issues.
  557.  * GTT space is continuous. I guarantee it.                           */
  558.  
  559.         mapped = bits = (u32*)MapIoMem(dev_priv->gtt.mappable_base + i915_gem_obj_ggtt_offset(obj),
  560.                     CURSOR_WIDTH*CURSOR_HEIGHT*4, PG_SW);
  561.  
  562.         if (unlikely(bits == NULL))
  563.         {
  564.             i915_gem_object_unpin(obj);
  565.             drm_gem_object_unreference(&obj->base);
  566.             return -ENOMEM;
  567.         };
  568.         cursor->cobj = obj;
  569.     };
  570.  
  571.     src = cursor->data;
  572.  
  573.     for(i = 0; i < 32; i++)
  574.     {
  575.         for(j = 0; j < 32; j++)
  576.             *bits++ = *src++;
  577.         for(j = 32; j < CURSOR_WIDTH; j++)
  578.             *bits++ = 0;
  579.     }
  580.     for(i = 0; i < CURSOR_WIDTH*(CURSOR_HEIGHT-32); i++)
  581.         *bits++ = 0;
  582.  
  583.     FreeKernelSpace(mapped);
  584.  
  585. // release old cursor
  586.  
  587.     KernelFree(cursor->data);
  588.  
  589.     cursor->data = bits;
  590.  
  591.     cursor->header.destroy = destroy_cursor;
  592.  
  593.     return 0;
  594. }
  595.  
  596.  
  597. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  598. {
  599.     struct drm_device *dev = crtc->dev;
  600.     struct drm_i915_private *dev_priv = dev->dev_private;
  601.     struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  602.     int pipe = intel_crtc->pipe;
  603.     bool visible = base != 0;
  604.  
  605.     if (intel_crtc->cursor_visible != visible) {
  606.         uint32_t cntl = I915_READ(CURCNTR(pipe));
  607.         if (base) {
  608.             cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
  609.             cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
  610.             cntl |= pipe << 28; /* Connect to correct pipe */
  611.         } else {
  612.             cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
  613.             cntl |= CURSOR_MODE_DISABLE;
  614.         }
  615.         I915_WRITE(CURCNTR(pipe), cntl);
  616.  
  617.         intel_crtc->cursor_visible = visible;
  618.     }
  619.     /* and commit changes on next vblank */
  620.     I915_WRITE(CURBASE(pipe), base);
  621. }
  622.  
  623. void __stdcall move_cursor_kms(cursor_t *cursor, int x, int y)
  624. {
  625.     struct drm_i915_private *dev_priv = os_display->ddev->dev_private;
  626.     struct intel_crtc *intel_crtc = to_intel_crtc(os_display->crtc);
  627.     u32 base, pos;
  628.     bool visible;
  629.  
  630.     int pipe = intel_crtc->pipe;
  631.  
  632.     intel_crtc->cursor_x = x;
  633.     intel_crtc->cursor_y = y;
  634.  
  635.     x = x - cursor->hot_x;
  636.     y = y - cursor->hot_y;
  637.  
  638.  
  639.     pos = 0;
  640.  
  641.     base = intel_crtc->cursor_addr;
  642.     if (x >= os_display->width)
  643.         base = 0;
  644.  
  645.     if (y >= os_display->height)
  646.         base = 0;
  647.  
  648.     if (x < 0)
  649.     {
  650.         if (x + intel_crtc->cursor_width < 0)
  651.             base = 0;
  652.  
  653.         pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  654.         x = -x;
  655.     }
  656.     pos |= x << CURSOR_X_SHIFT;
  657.  
  658.     if (y < 0)
  659.     {
  660.         if (y + intel_crtc->cursor_height < 0)
  661.             base = 0;
  662.  
  663.         pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  664.         y = -y;
  665.     }
  666.     pos |= y << CURSOR_Y_SHIFT;
  667.  
  668.     visible = base != 0;
  669.     if (!visible && !intel_crtc->cursor_visible)
  670.         return;
  671.  
  672.     I915_WRITE(CURPOS(pipe), pos);
  673. //    if (IS_845G(dev) || IS_I865G(dev))
  674. //        i845_update_cursor(crtc, base);
  675. //    else
  676.         i9xx_update_cursor(os_display->crtc, base);
  677.  
  678. };
  679.  
  680.  
  681. cursor_t* __stdcall select_cursor_kms(cursor_t *cursor)
  682. {
  683.     struct drm_i915_private *dev_priv = os_display->ddev->dev_private;
  684.     struct intel_crtc *intel_crtc = to_intel_crtc(os_display->crtc);
  685.     cursor_t *old;
  686.  
  687.     old = os_display->cursor;
  688.     os_display->cursor = cursor;
  689.  
  690.     if (!dev_priv->info->cursor_needs_physical)
  691.        intel_crtc->cursor_addr = i915_gem_obj_ggtt_offset(cursor->cobj);
  692.     else
  693.         intel_crtc->cursor_addr = (addr_t)cursor->cobj;
  694.  
  695.     intel_crtc->cursor_width = 32;
  696.     intel_crtc->cursor_height = 32;
  697.  
  698.     move_cursor_kms(cursor, intel_crtc->cursor_x, intel_crtc->cursor_y);
  699.     return old;
  700. };
  701.  
  702. int i915_fbinfo(struct drm_i915_fb_info *fb)
  703. {
  704.     struct drm_i915_gem_object *obj = get_fb_obj();
  705.  
  706.     fb->name   = obj->base.name;
  707.     fb->width  = os_display->width;
  708.     fb->height = os_display->height;
  709.     fb->pitch  = obj->stride;
  710.     fb->tiling = obj->tiling_mode;
  711.  
  712.     return 0;
  713. }
  714.  
  715.  
  716. typedef struct
  717. {
  718.     int left;
  719.     int top;
  720.     int right;
  721.     int bottom;
  722. }rect_t;
  723.  
  724.  
  725. #define CURRENT_TASK             (0x80003000)
  726.  
  727. static u32_t get_display_map()
  728. {
  729.     u32_t   addr;
  730.  
  731.     addr = (u32_t)os_display;
  732.     addr+= sizeof(display_t);            /*  shoot me  */
  733.     return *(u32_t*)addr;
  734. }
  735.  
  736. void  FASTCALL GetWindowRect(rect_t *rc)__asm__("GetWindowRect");
  737.  
  738. int i915_mask_update(struct drm_device *dev, void *data,
  739.             struct drm_file *file)
  740. {
  741.     struct drm_i915_mask *mask = data;
  742.     struct drm_gem_object *obj;
  743.     static unsigned int mask_seqno[256];
  744.     rect_t winrc;
  745.     u32    slot;
  746.     int    ret=0;
  747.  
  748.      if(mask->handle == -2)
  749.      {
  750.         printf("%s handle %d\n", __FUNCTION__, mask->handle);
  751.         return 0;
  752.      }
  753.  
  754.     obj = drm_gem_object_lookup(dev, file, mask->handle);
  755.     if (obj == NULL)
  756.         return -ENOENT;
  757.  
  758.     if (!obj->filp) {
  759.         drm_gem_object_unreference_unlocked(obj);
  760.         return -EINVAL;
  761.     }
  762.  
  763.     GetWindowRect(&winrc);
  764.     {
  765. //        static warn_count;
  766.  
  767.         mask->width    = winrc.right - winrc.left + 1;
  768.         mask->height   = winrc.bottom - winrc.top + 1;
  769.         mask->bo_pitch = (mask->width+15) & ~15;
  770.  
  771. #if 0
  772.         if(warn_count < 1)
  773.         {
  774.             printf("left %d top %d right %d bottom %d\n",
  775.                     winrc.left, winrc.top, winrc.right, winrc.bottom);
  776.             printf("mask pitch %d data %p\n", mask->bo_pitch, mask->bo_size);
  777.             warn_count++;
  778.         };
  779. #endif
  780.  
  781.      };
  782.  
  783.  
  784.     slot = *((u8*)CURRENT_TASK);
  785.  
  786.     if( mask_seqno[slot] != os_display->mask_seqno)
  787.     {
  788.         u8* src_offset;
  789.         u8* dst_offset;
  790.         u32 ifl;
  791.  
  792.         ret = i915_mutex_lock_interruptible(dev);
  793.         if (ret)
  794.             goto err1;
  795.  
  796.         ret = i915_gem_object_set_to_cpu_domain(to_intel_bo(obj), true);
  797.         if(ret !=0 )
  798.         {
  799.             dbgprintf("%s: i915_gem_object_set_to_cpu_domain failed\n", __FUNCTION__);
  800.             goto err2;
  801.         };
  802.  
  803. //        printf("width %d height %d\n", winrc.right, winrc.bottom);
  804.  
  805. //        slot = 0x01;
  806.  
  807.  
  808.         src_offset = (u8*)( winrc.top*os_display->width + winrc.left);
  809.         src_offset+= get_display_map();
  810.         dst_offset = (u8*)mask->bo_map;
  811.  
  812.         u32_t tmp_h = mask->height;
  813.  
  814.         ifl = safe_cli();
  815.         {
  816.             mask_seqno[slot] = os_display->mask_seqno;
  817.  
  818.             slot|= (slot<<8)|(slot<<16)|(slot<<24);
  819.  
  820.             __asm__ __volatile__ (
  821.                 "movd       %[slot],   %%xmm6    \n"
  822.             "punpckldq  %%xmm6, %%xmm6            \n"
  823.             "punpcklqdq %%xmm6, %%xmm6            \n"
  824.             :: [slot]  "m" (slot)
  825.             :"xmm6");
  826.  
  827.             while( tmp_h--)
  828.             {
  829.                 int tmp_w = mask->bo_pitch;
  830.  
  831.                 u8* tmp_src = src_offset;
  832.                 u8* tmp_dst = dst_offset;
  833.  
  834.                 src_offset+= os_display->width;
  835.                 dst_offset+= mask->bo_pitch;
  836.  
  837.                 while(tmp_w >= 64)
  838.                 {
  839.                     __asm__ __volatile__ (
  840.                     "movdqu     (%0),   %%xmm0            \n"
  841.                     "movdqu   16(%0),   %%xmm1            \n"
  842.                     "movdqu   32(%0),   %%xmm2            \n"
  843.                     "movdqu   48(%0),   %%xmm3            \n"
  844.                     "pcmpeqb    %%xmm6, %%xmm0            \n"
  845.                     "pcmpeqb    %%xmm6, %%xmm1            \n"
  846.                     "pcmpeqb    %%xmm6, %%xmm2            \n"
  847.                     "pcmpeqb    %%xmm6, %%xmm3            \n"
  848.                     "movdqa     %%xmm0,   (%%edi)         \n"
  849.                     "movdqa     %%xmm1, 16(%%edi)         \n"
  850.                     "movdqa     %%xmm2, 32(%%edi)         \n"
  851.                     "movdqa     %%xmm3, 48(%%edi)         \n"
  852.  
  853.                     :: "r" (tmp_src), "D" (tmp_dst)
  854.                     :"xmm0","xmm1","xmm2","xmm3");
  855.                     tmp_w -= 64;
  856.                     tmp_src += 64;
  857.                     tmp_dst += 64;
  858.                 }
  859.  
  860.                 if( tmp_w >= 32 )
  861.                 {
  862.                     __asm__ __volatile__ (
  863.                     "movdqu     (%0),   %%xmm0            \n"
  864.                     "movdqu   16(%0),   %%xmm1            \n"
  865.                     "pcmpeqb    %%xmm6, %%xmm0            \n"
  866.                     "pcmpeqb    %%xmm6, %%xmm1            \n"
  867.                     "movdqa     %%xmm0,   (%%edi)         \n"
  868.                     "movdqa     %%xmm1, 16(%%edi)         \n"
  869.  
  870.                     :: "r" (tmp_src), "D" (tmp_dst)
  871.                     :"xmm0","xmm1");
  872.                     tmp_w -= 32;
  873.                     tmp_src += 32;
  874.                     tmp_dst += 32;
  875.                 }
  876.  
  877.                 while( tmp_w > 0 )
  878.                 {
  879.                     __asm__ __volatile__ (
  880.                     "movdqu     (%0),   %%xmm0            \n"
  881.                     "pcmpeqb    %%xmm6, %%xmm0            \n"
  882.                     "movdqa     %%xmm0,   (%%edi)         \n"
  883.                     :: "r" (tmp_src), "D" (tmp_dst)
  884.                     :"xmm0");
  885.                     tmp_w -= 16;
  886.                     tmp_src += 16;
  887.                     tmp_dst += 16;
  888.                 }
  889.             };
  890.         };
  891.         safe_sti(ifl);
  892.  
  893.         ret = i915_gem_object_set_to_gtt_domain(to_intel_bo(obj), false);
  894.         if(ret != 0 )
  895.         {
  896.             dbgprintf("%s: i915_gem_object_set_to_gtt_domain failed\n", __FUNCTION__);
  897.         };
  898.     }
  899.  
  900. err2:
  901.     mutex_unlock(&dev->struct_mutex);
  902. err1:
  903.     drm_gem_object_unreference(obj);
  904.  
  905.     return ret;
  906. }
  907.  
  908.  
  909.  
  910.  
  911.  
  912.  
  913.  
  914.  
  915.  
  916.  
  917.  
  918.  
  919. #define NSEC_PER_SEC    1000000000L
  920.  
  921. void getrawmonotonic(struct timespec *ts)
  922. {
  923.     u32 tmp = GetTimerTicks();
  924.  
  925.     ts->tv_sec  = tmp/100;
  926.     ts->tv_nsec = (tmp - ts->tv_sec*100)*10000000;
  927. }
  928.  
  929. void set_normalized_timespec(struct timespec *ts, time_t sec, s64 nsec)
  930. {
  931.     while (nsec >= NSEC_PER_SEC) {
  932.         /*
  933.          * The following asm() prevents the compiler from
  934.          * optimising this loop into a modulo operation. See
  935.          * also __iter_div_u64_rem() in include/linux/time.h
  936.          */
  937.         asm("" : "+rm"(nsec));
  938.         nsec -= NSEC_PER_SEC;
  939.         ++sec;
  940.     }
  941.     while (nsec < 0) {
  942.         asm("" : "+rm"(nsec));
  943.         nsec += NSEC_PER_SEC;
  944.         --sec;
  945.     }
  946.     ts->tv_sec = sec;
  947.     ts->tv_nsec = nsec;
  948. }
  949.  
  950. void
  951. prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state)
  952. {
  953.     unsigned long flags;
  954.  
  955. //    wait->flags &= ~WQ_FLAG_EXCLUSIVE;
  956.     spin_lock_irqsave(&q->lock, flags);
  957.     if (list_empty(&wait->task_list))
  958.             __add_wait_queue(q, wait);
  959.     spin_unlock_irqrestore(&q->lock, flags);
  960. }
  961.  
  962. /**
  963.  * finish_wait - clean up after waiting in a queue
  964.  * @q: waitqueue waited on
  965.  * @wait: wait descriptor
  966.  *
  967.  * Sets current thread back to running state and removes
  968.  * the wait descriptor from the given waitqueue if still
  969.  * queued.
  970.  */
  971. void finish_wait(wait_queue_head_t *q, wait_queue_t *wait)
  972. {
  973.     unsigned long flags;
  974.  
  975. //    __set_current_state(TASK_RUNNING);
  976.     /*
  977.      * We can check for list emptiness outside the lock
  978.      * IFF:
  979.      *  - we use the "careful" check that verifies both
  980.      *    the next and prev pointers, so that there cannot
  981.      *    be any half-pending updates in progress on other
  982.      *    CPU's that we haven't seen yet (and that might
  983.      *    still change the stack area.
  984.      * and
  985.      *  - all other users take the lock (ie we can only
  986.      *    have _one_ other CPU that looks at or modifies
  987.      *    the list).
  988.      */
  989.     if (!list_empty_careful(&wait->task_list)) {
  990.             spin_lock_irqsave(&q->lock, flags);
  991.             list_del_init(&wait->task_list);
  992.             spin_unlock_irqrestore(&q->lock, flags);
  993.     }
  994.  
  995.     DestroyEvent(wait->evnt);
  996. }
  997.  
  998. int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key)
  999. {
  1000.     list_del_init(&wait->task_list);
  1001.     return 1;
  1002. }
  1003.  
  1004. unsigned int hweight16(unsigned int w)
  1005. {
  1006.     unsigned int res = w - ((w >> 1) & 0x5555);
  1007.     res = (res & 0x3333) + ((res >> 2) & 0x3333);
  1008.     res = (res + (res >> 4)) & 0x0F0F;
  1009.     return (res + (res >> 8)) & 0x00FF;
  1010. }
  1011.  
  1012.  
  1013. unsigned long round_jiffies_up_relative(unsigned long j)
  1014. {
  1015.     unsigned long j0 = GetTimerTicks();
  1016.  
  1017.         /* Use j0 because jiffies might change while we run */
  1018.     return round_jiffies_common(j + j0, true) - j0;
  1019. }
  1020.  
  1021.  
  1022.