Subversion Repositories Kolibri OS

Rev

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

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