Subversion Repositories Kolibri OS

Rev

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

  1.  
  2. #include <drm/drmP.h>
  3. #include <drm.h>
  4. #include <drm_mm.h>
  5. #include "radeon_drm.h"
  6. #include "radeon.h"
  7. #include "radeon_object.h"
  8. #include "display.h"
  9.  
  10.  
  11. static cursor_t*  __stdcall select_cursor_kms(cursor_t *cursor);
  12. static void       __stdcall move_cursor_kms(cursor_t *cursor, int x, int y);
  13.  
  14. int radeon_align_pitch(struct radeon_device *rdev, int width, int bpp, bool tiled);
  15.  
  16. void disable_mouse(void);
  17.  
  18. static void radeon_show_cursor_kms(struct drm_crtc *crtc)
  19. {
  20.     struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  21.     struct radeon_device *rdev = crtc->dev->dev_private;
  22.  
  23.     if (ASIC_IS_AVIVO(rdev)) {
  24.         WREG32(RADEON_MM_INDEX, AVIVO_D1CUR_CONTROL + radeon_crtc->crtc_offset);
  25.         WREG32(RADEON_MM_DATA, AVIVO_D1CURSOR_EN |
  26.                  (AVIVO_D1CURSOR_MODE_24BPP << AVIVO_D1CURSOR_MODE_SHIFT));
  27.     } else {
  28.         switch (radeon_crtc->crtc_id) {
  29.         case 0:
  30.             WREG32(RADEON_MM_INDEX, RADEON_CRTC_GEN_CNTL);
  31.             break;
  32.         case 1:
  33.             WREG32(RADEON_MM_INDEX, RADEON_CRTC2_GEN_CNTL);
  34.             break;
  35.         default:
  36.             return;
  37.         }
  38.  
  39.         WREG32_P(RADEON_MM_DATA, (RADEON_CRTC_CUR_EN |
  40.                       (RADEON_CRTC_CUR_MODE_24BPP << RADEON_CRTC_CUR_MODE_SHIFT)),
  41.              ~(RADEON_CRTC_CUR_EN | RADEON_CRTC_CUR_MODE_MASK));
  42.     }
  43. }
  44.  
  45. static void radeon_lock_cursor_kms(struct drm_crtc *crtc, bool lock)
  46. {
  47.     struct radeon_device *rdev = crtc->dev->dev_private;
  48.     struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  49.     uint32_t cur_lock;
  50.  
  51.     if (ASIC_IS_AVIVO(rdev)) {
  52.         cur_lock = RREG32(AVIVO_D1CUR_UPDATE + radeon_crtc->crtc_offset);
  53.         if (lock)
  54.             cur_lock |= AVIVO_D1CURSOR_UPDATE_LOCK;
  55.         else
  56.             cur_lock &= ~AVIVO_D1CURSOR_UPDATE_LOCK;
  57.         WREG32(AVIVO_D1CUR_UPDATE + radeon_crtc->crtc_offset, cur_lock);
  58.     } else {
  59.         cur_lock = RREG32(RADEON_CUR_OFFSET + radeon_crtc->crtc_offset);
  60.         if (lock)
  61.             cur_lock |= RADEON_CUR_LOCK;
  62.         else
  63.             cur_lock &= ~RADEON_CUR_LOCK;
  64.         WREG32(RADEON_CUR_OFFSET + radeon_crtc->crtc_offset, cur_lock);
  65.     }
  66. }
  67.  
  68. cursor_t* __stdcall select_cursor_kms(cursor_t *cursor)
  69. {
  70.     struct radeon_device *rdev;
  71.     struct radeon_crtc   *radeon_crtc;
  72.     cursor_t *old;
  73.     uint32_t  gpu_addr;
  74.  
  75.     rdev = (struct radeon_device *)rdisplay->ddev->dev_private;
  76.     radeon_crtc = to_radeon_crtc(rdisplay->crtc);
  77.  
  78.     old = rdisplay->cursor;
  79.  
  80.     rdisplay->cursor = cursor;
  81. //    gpu_addr = cursor->robj->gpu_addr;
  82.  
  83.     if (ASIC_IS_AVIVO(rdev))
  84.         WREG32(AVIVO_D1CUR_SURFACE_ADDRESS + radeon_crtc->crtc_offset, gpu_addr);
  85.     else {
  86.         radeon_crtc->legacy_cursor_offset = gpu_addr - rdev->mc.vram_location;
  87.         /* offset is from DISP(2)_BASE_ADDRESS */
  88.         WREG32(RADEON_CUR_OFFSET + radeon_crtc->crtc_offset, radeon_crtc->legacy_cursor_offset);
  89.     }
  90.  
  91.     return old;
  92. };
  93.  
  94. void __stdcall move_cursor_kms(cursor_t *cursor, int x, int y)
  95. {
  96.     struct radeon_device *rdev;
  97.     rdev = (struct radeon_device *)rdisplay->ddev->dev_private;
  98.     struct drm_crtc *crtc = rdisplay->crtc;
  99.     struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  100.  
  101.     int hot_x = cursor->hot_x;
  102.     int hot_y = cursor->hot_y;
  103.  
  104.     radeon_lock_cursor_kms(crtc, true);
  105.     if (ASIC_IS_AVIVO(rdev))
  106.     {
  107.         int w = 32;
  108.         int i = 0;
  109.         struct drm_crtc *crtc_p;
  110.  
  111.         /* avivo cursor are offset into the total surface */
  112. //        x += crtc->x;
  113. //        y += crtc->y;
  114.  
  115. //        DRM_DEBUG("x %d y %d c->x %d c->y %d\n", x, y, crtc->x, crtc->y);
  116. #if 0
  117.         /* avivo cursor image can't end on 128 pixel boundry or
  118.          * go past the end of the frame if both crtcs are enabled
  119.          */
  120.         list_for_each_entry(crtc_p, &crtc->dev->mode_config.crtc_list, head) {
  121.             if (crtc_p->enabled)
  122.                 i++;
  123.         }
  124.         if (i > 1) {
  125.             int cursor_end, frame_end;
  126.  
  127.             cursor_end = x + w;
  128.             frame_end = crtc->x + crtc->mode.crtc_hdisplay;
  129.             if (cursor_end >= frame_end) {
  130.                 w = w - (cursor_end - frame_end);
  131.                 if (!(frame_end & 0x7f))
  132.                     w--;
  133.             } else {
  134.                 if (!(cursor_end & 0x7f))
  135.                     w--;
  136.             }
  137.             if (w <= 0)
  138.                 w = 1;
  139.         }
  140. #endif
  141.         WREG32(AVIVO_D1CUR_POSITION + radeon_crtc->crtc_offset,
  142.                (x << 16) | y);
  143.         WREG32(AVIVO_D1CUR_HOT_SPOT + radeon_crtc->crtc_offset,
  144.                (hot_x << 16) | hot_y);
  145.         WREG32(AVIVO_D1CUR_SIZE + radeon_crtc->crtc_offset,
  146.                ((w - 1) << 16) | 31);
  147.     } else {
  148.         if (crtc->mode.flags & DRM_MODE_FLAG_DBLSCAN)
  149.             y *= 2;
  150.  
  151.         WREG32(RADEON_CUR_HORZ_VERT_OFF + radeon_crtc->crtc_offset,
  152.                (RADEON_CUR_LOCK | (hot_x << 16) | hot_y ));
  153.         WREG32(RADEON_CUR_HORZ_VERT_POSN + radeon_crtc->crtc_offset,
  154.                (RADEON_CUR_LOCK | (x << 16) | y));
  155.  
  156.         /* offset is from DISP(2)_BASE_ADDRESS */
  157.         WREG32(RADEON_CUR_OFFSET + radeon_crtc->crtc_offset,
  158.          (radeon_crtc->legacy_cursor_offset + (hot_y * 256)));
  159.     }
  160.     radeon_lock_cursor_kms(crtc, false);
  161. }
  162.  
  163. static char *manufacturer_name(unsigned char *x)
  164. {
  165.     static char name[4];
  166.  
  167.     name[0] = ((x[0] & 0x7C) >> 2) + '@';
  168.     name[1] = ((x[0] & 0x03) << 3) + ((x[1] & 0xE0) >> 5) + '@';
  169.     name[2] = (x[1] & 0x1F) + '@';
  170.     name[3] = 0;
  171.  
  172.     return name;
  173. }
  174.  
  175. bool set_mode(struct drm_device *dev, struct drm_connector *connector,
  176.               videomode_t *reqmode, bool strict)
  177. {
  178.     struct drm_display_mode  *mode = NULL, *tmpmode;
  179.  
  180.     bool ret = false;
  181.  
  182.     ENTER();
  183.  
  184.     dbgprintf("width %d height %d vrefresh %d\n",
  185.                reqmode->width, reqmode->height, reqmode->freq);
  186.  
  187.     list_for_each_entry(tmpmode, &connector->modes, head)
  188.     {
  189.         if( (drm_mode_width(tmpmode)    == reqmode->width)  &&
  190.             (drm_mode_height(tmpmode)   == reqmode->height) &&
  191.             (drm_mode_vrefresh(tmpmode) == reqmode->freq) )
  192.         {
  193.             mode = tmpmode;
  194.             goto do_set;
  195.         }
  196.     };
  197.  
  198.     if( (mode == NULL) && (strict == false) )
  199.     {
  200.         list_for_each_entry(tmpmode, &connector->modes, head)
  201.         {
  202.             if( (drm_mode_width(tmpmode)  == reqmode->width)  &&
  203.                 (drm_mode_height(tmpmode) == reqmode->height) )
  204.             {
  205.                 mode = tmpmode;
  206.                 goto do_set;
  207.             }
  208.         };
  209.     };
  210.  
  211. do_set:
  212.  
  213.     if( mode != NULL )
  214.     {
  215.         struct drm_framebuffer   *fb;
  216.         struct drm_encoder       *encoder;
  217.         struct drm_crtc          *crtc;
  218.  
  219. //        char  con_edid[128];
  220.         char *con_name;
  221.         char *enc_name;
  222.  
  223.         encoder = connector->encoder;
  224.         crtc = encoder->crtc;
  225.  
  226.         fb = list_first_entry(&dev->mode_config.fb_kernel_list,
  227.                               struct drm_framebuffer, filp_head);
  228.  
  229. //        memcpy(con_edid, connector->edid_blob_ptr->data, 128);
  230.  
  231. //        dbgprintf("Manufacturer: %s Model %x Serial Number %u\n",
  232. //        manufacturer_name(con_edid + 0x08),
  233. //        (unsigned short)(con_edid[0x0A] + (con_edid[0x0B] << 8)),
  234. //        (unsigned int)(con_edid[0x0C] + (con_edid[0x0D] << 8)
  235. //            + (con_edid[0x0E] << 16) + (con_edid[0x0F] << 24)));
  236.  
  237.         con_name = drm_get_connector_name(connector);
  238.         enc_name = drm_get_encoder_name(encoder);
  239.  
  240.         dbgprintf("set mode %d %d connector %s encoder %s\n",
  241.                    reqmode->width, reqmode->height, con_name, enc_name);
  242.  
  243.         fb->width  = reqmode->width;
  244.         fb->height = reqmode->height;
  245.         fb->pitch  = radeon_align_pitch(dev->dev_private, reqmode->width, 32, false) * ((32 + 1) / 8);
  246.  
  247.         crtc->fb = fb;
  248.         crtc->enabled = true;
  249.         rdisplay->crtc = crtc;
  250.  
  251.         ret = drm_crtc_helper_set_mode(crtc, mode, 0, 0, fb);
  252.  
  253.         select_cursor_kms(rdisplay->cursor);
  254.         radeon_show_cursor_kms(crtc);
  255.  
  256.         if (ret == true)
  257.         {
  258.             rdisplay->width    = fb->width;
  259.             rdisplay->height   = fb->height;
  260.             rdisplay->pitch    = fb->pitch;
  261.             rdisplay->vrefresh = drm_mode_vrefresh(mode);
  262.  
  263.             sysSetScreen(fb->width, fb->height, fb->pitch);
  264.  
  265.             dbgprintf("new mode %d x %d pitch %d\n",
  266.                        fb->width, fb->height, fb->pitch);
  267.         }
  268.         else
  269.             DRM_ERROR("failed to set mode %d_%d on crtc %p\n",
  270.                        fb->width, fb->height, crtc);
  271.     }
  272.  
  273.     LEAVE();
  274.     return ret;
  275. };
  276.  
  277. static int count_connector_modes(struct drm_connector* connector)
  278. {
  279.     struct drm_display_mode  *mode;
  280.     int count = 0;
  281.  
  282.     list_for_each_entry(mode, &connector->modes, head)
  283.     {
  284.         count++;
  285.     };
  286.     return count;
  287. };
  288.  
  289. static struct drm_connector* get_def_connector(struct drm_device *dev)
  290. {
  291.     struct drm_connector  *connector;
  292.     struct drm_connector  *def_connector = NULL;
  293.  
  294.     list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  295.     {
  296.         struct drm_encoder  *encoder;
  297.         struct drm_crtc     *crtc;
  298.  
  299.         if( connector->status != connector_status_connected)
  300.             continue;
  301.  
  302.         encoder = connector->encoder;
  303.         if( encoder == NULL)
  304.             continue;
  305.  
  306.         crtc = encoder->crtc;
  307.         if(crtc == NULL)
  308.             continue;
  309.  
  310.         def_connector = connector;
  311.         break;
  312.     };
  313.  
  314.     return def_connector;
  315. };
  316.  
  317. bool init_display_kms(struct radeon_device *rdev, videomode_t *usermode)
  318. {
  319.     struct drm_device   *dev;
  320.  
  321.     cursor_t            *cursor;
  322.     bool                 retval = false;
  323.     u32_t                ifl;
  324.  
  325.     ENTER();
  326.  
  327.     rdisplay = GetDisplay();
  328.  
  329.     dev = rdisplay->ddev = rdev->ddev;
  330.  
  331.     ifl = safe_cli();
  332.     {
  333.         list_for_each_entry(cursor, &rdisplay->cursors, list)
  334.         {
  335.             init_cursor(cursor);
  336.         };
  337.     };
  338.     safe_sti(ifl);
  339.  
  340.     rdisplay->connector = get_def_connector(dev);
  341.     if( rdisplay->connector == 0 )
  342.     {
  343.         dbgprintf("no active connectors\n");
  344.         return false;
  345.     };
  346.  
  347.     rdisplay->crtc = rdisplay->connector->encoder->crtc;
  348.     rdisplay->supported_modes = count_connector_modes(rdisplay->connector);
  349.  
  350.     dbgprintf("current mode %d x %d x %d\n",
  351.               rdisplay->width, rdisplay->height, rdisplay->vrefresh);
  352.     dbgprintf("user mode mode %d x %d x %d\n",
  353.               usermode->width, usermode->height, usermode->freq);
  354.  
  355.     if( (usermode->width  != 0) &&
  356.         (usermode->height != 0) &&
  357.         ( (usermode->width  != rdisplay->width)  ||
  358.           (usermode->height != rdisplay->height) ||
  359.           (usermode->freq   != rdisplay->vrefresh) ) )
  360.     {
  361.  
  362.         retval = set_mode(dev, rdisplay->connector, usermode, false);
  363.     }
  364.  
  365.     ifl = safe_cli();
  366.     {
  367.         rdisplay->restore_cursor(0,0);
  368.         rdisplay->init_cursor    = init_cursor;
  369.         rdisplay->select_cursor  = select_cursor_kms;
  370.         rdisplay->show_cursor    = NULL;
  371.         rdisplay->move_cursor    = move_cursor_kms;
  372.         rdisplay->restore_cursor = restore_cursor;
  373.         rdisplay->disable_mouse  = disable_mouse;
  374.  
  375.         select_cursor_kms(rdisplay->cursor);
  376.         radeon_show_cursor_kms(rdisplay->crtc);
  377.     };
  378.     safe_sti(ifl);
  379.  
  380.     LEAVE();
  381.  
  382.     return retval;
  383. };
  384.  
  385. int get_modes(videomode_t *mode, int *count)
  386. {
  387.     int err = -1;
  388.  
  389.     ENTER();
  390.  
  391.     dbgprintf("mode %x count %d\n", mode, *count);
  392.  
  393.     if( *count == 0 )
  394.     {
  395.         *count = rdisplay->supported_modes;
  396.         err = 0;
  397.     }
  398.     else if( mode != NULL )
  399.     {
  400.         struct drm_display_mode  *drmmode;
  401.         int i = 0;
  402.  
  403.         if( *count > rdisplay->supported_modes)
  404.             *count = rdisplay->supported_modes;
  405.  
  406.         list_for_each_entry(drmmode, &rdisplay->connector->modes, head)
  407.         {
  408.             if( i < *count)
  409.             {
  410.                 mode->width  = drm_mode_width(drmmode);
  411.                 mode->height = drm_mode_height(drmmode);
  412.                 mode->bpp    = 32;
  413.                 mode->freq   = drm_mode_vrefresh(drmmode);
  414.                 i++;
  415.                 mode++;
  416.             }
  417.             else break;
  418.         };
  419.         *count = i;
  420.         err = 0;
  421.     };
  422.     LEAVE();
  423.     return err;
  424. }
  425.  
  426. int set_user_mode(videomode_t *mode)
  427. {
  428.     int err = -1;
  429.  
  430.     ENTER();
  431.  
  432.     dbgprintf("width %d height %d vrefresh %d\n",
  433.                mode->width, mode->height, mode->freq);
  434.  
  435.     if( (mode->width  != 0)  &&
  436.         (mode->height != 0)  &&
  437.         (mode->freq   != 0 ) &&
  438.         ( (mode->width   != rdisplay->width)  ||
  439.           (mode->height  != rdisplay->height) ||
  440.           (mode->freq    != rdisplay->vrefresh) ) )
  441.     {
  442.         if( set_mode(rdisplay->ddev, rdisplay->connector, mode, true) )
  443.             err = 0;
  444.     };
  445.  
  446.     LEAVE();
  447.     return err;
  448. };
  449.  
  450.