Subversion Repositories Kolibri OS

Rev

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