Subversion Repositories Kolibri OS

Rev

Rev 6938 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright © 2007 David Airlie
  3.  *
  4.  * Permission is hereby granted, free of charge, to any person obtaining a
  5.  * copy of this software and associated documentation files (the "Software"),
  6.  * to deal in the Software without restriction, including without limitation
  7.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8.  * and/or sell copies of the Software, and to permit persons to whom the
  9.  * Software is furnished to do so, subject to the following conditions:
  10.  *
  11.  * The above copyright notice and this permission notice (including the next
  12.  * paragraph) shall be included in all copies or substantial portions of the
  13.  * Software.
  14.  *
  15.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  18.  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20.  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21.  * DEALINGS IN THE SOFTWARE.
  22.  *
  23.  * Authors:
  24.  *     David Airlie
  25.  */
  26. #include <linux/module.h>
  27. #include <linux/slab.h>
  28. #include <linux/fb.h>
  29.  
  30. #include <drm/drmP.h>
  31. #include <drm/drm_crtc.h>
  32. #include <drm/drm_crtc_helper.h>
  33. #include <drm/radeon_drm.h>
  34. #include "radeon.h"
  35.  
  36. #include <drm/drm_fb_helper.h>
  37.  
  38. struct drm_framebuffer *main_fb;
  39. struct drm_gem_object  *main_fb_obj;
  40.  
  41. /* object hierarchy -
  42.  * this contains a helper + a radeon fb
  43.  * the helper contains a pointer to radeon framebuffer baseclass.
  44.  */
  45. struct radeon_fbdev {
  46.         struct drm_fb_helper helper;
  47.         struct radeon_framebuffer rfb;
  48.         struct radeon_device *rdev;
  49. };
  50.  
  51. static struct fb_ops radeonfb_ops = {
  52.         .owner = THIS_MODULE,
  53.         .fb_check_var = drm_fb_helper_check_var,
  54.         .fb_set_par = drm_fb_helper_set_par,
  55. //      .fb_fillrect = cfb_fillrect,
  56. //      .fb_copyarea = cfb_copyarea,
  57. //      .fb_imageblit = cfb_imageblit,
  58. //      .fb_pan_display = drm_fb_helper_pan_display,
  59.         .fb_blank = drm_fb_helper_blank,
  60.         .fb_setcmap = drm_fb_helper_setcmap,
  61. };
  62.  
  63.  
  64. int radeon_align_pitch(struct radeon_device *rdev, int width, int bpp, bool tiled)
  65. {
  66.         int aligned = width;
  67.         int align_large = (ASIC_IS_AVIVO(rdev)) || tiled;
  68.         int pitch_mask = 0;
  69.  
  70.         switch (bpp / 8) {
  71.         case 1:
  72.                 pitch_mask = align_large ? 255 : 127;
  73.                 break;
  74.         case 2:
  75.                 pitch_mask = align_large ? 127 : 31;
  76.                 break;
  77.         case 3:
  78.         case 4:
  79.                 pitch_mask = align_large ? 63 : 15;
  80.                 break;
  81.         }
  82.  
  83.         aligned += pitch_mask;
  84.         aligned &= ~pitch_mask;
  85.         return aligned;
  86. }
  87.  
  88. static void radeonfb_destroy_pinned_object(struct drm_gem_object *gobj)
  89. {
  90.         struct radeon_bo *rbo = gem_to_radeon_bo(gobj);
  91.         int ret;
  92.  
  93.         ret = radeon_bo_reserve(rbo, false);
  94.         if (likely(ret == 0)) {
  95.                 radeon_bo_kunmap(rbo);
  96.                 radeon_bo_unpin(rbo);
  97.                 radeon_bo_unreserve(rbo);
  98.         }
  99.         drm_gem_object_unreference_unlocked(gobj);
  100. }
  101.  
  102. static int radeonfb_create_pinned_object(struct radeon_fbdev *rfbdev,
  103.                                          struct drm_mode_fb_cmd2 *mode_cmd,
  104.                                          struct drm_gem_object **gobj_p)
  105. {
  106.         struct radeon_device *rdev = rfbdev->rdev;
  107.         struct drm_gem_object *gobj = NULL;
  108.         struct radeon_bo *rbo = NULL;
  109.         bool fb_tiled = false; /* useful for testing */
  110.         u32 tiling_flags = 0;
  111.         int ret;
  112.         int aligned_size, size;
  113.         int height = mode_cmd->height;
  114.         u32 bpp, depth;
  115.  
  116.         drm_fb_get_bpp_depth(mode_cmd->pixel_format, &depth, &bpp);
  117.  
  118.         /* need to align pitch with crtc limits */
  119.         mode_cmd->pitches[0] = radeon_align_pitch(rdev, mode_cmd->width, bpp,
  120.                                                   fb_tiled) * ((bpp + 1) / 8);
  121.  
  122.         if (rdev->family >= CHIP_R600)
  123.                 height = ALIGN(mode_cmd->height, 8);
  124.         size = mode_cmd->pitches[0] * height;
  125.         aligned_size = ALIGN(size, PAGE_SIZE);
  126.  
  127.         rbo = rdev->stollen_vga_memory;
  128.         gobj = &rbo->gem_base;
  129.         mutex_lock(&rdev->gem.mutex);
  130.         list_add_tail(&rbo->list, &rdev->gem.objects);
  131.         mutex_unlock(&rdev->gem.mutex);
  132.  
  133.         rbo = gem_to_radeon_bo(gobj);
  134.  
  135.         if (fb_tiled)
  136.                 tiling_flags = RADEON_TILING_MACRO;
  137.  
  138. #ifdef __BIG_ENDIAN
  139.         switch (bpp) {
  140.         case 32:
  141.                 tiling_flags |= RADEON_TILING_SWAP_32BIT;
  142.                 break;
  143.         case 16:
  144.                 tiling_flags |= RADEON_TILING_SWAP_16BIT;
  145.         default:
  146.                 break;
  147.         }
  148. #endif
  149.  
  150. //    if (tiling_flags) {
  151. //        ret = radeon_bo_set_tiling_flags(rbo,
  152. //                         tiling_flags | RADEON_TILING_SURFACE,
  153. //                         mode_cmd->pitches[0]);
  154. //        if (ret)
  155. //            dev_err(rdev->dev, "FB failed to set tiling flags\n");
  156. //    }
  157.  
  158.  
  159.         ret = radeon_bo_reserve(rbo, false);
  160.         if (unlikely(ret != 0))
  161.                 goto out_unref;
  162.         /* Only 27 bit offset for legacy CRTC */
  163.         ret = radeon_bo_pin_restricted(rbo, RADEON_GEM_DOMAIN_VRAM,
  164.                                        ASIC_IS_AVIVO(rdev) ? 0 : 1 << 27,
  165.                                        NULL);
  166.         if (ret) {
  167.                 radeon_bo_unreserve(rbo);
  168.                 goto out_unref;
  169.         }
  170.         radeon_bo_unreserve(rbo);
  171.         if (ret) {
  172.                 goto out_unref;
  173.         }
  174.  
  175.         *gobj_p = gobj;
  176.         return 0;
  177. out_unref:
  178.         radeonfb_destroy_pinned_object(gobj);
  179.         *gobj_p = NULL;
  180.         return ret;
  181. }
  182.  
  183. static int radeonfb_create(struct drm_fb_helper *helper,
  184.                            struct drm_fb_helper_surface_size *sizes)
  185. {
  186.         struct radeon_fbdev *rfbdev =
  187.                 container_of(helper, struct radeon_fbdev, helper);
  188.         struct radeon_device *rdev = rfbdev->rdev;
  189.         struct fb_info *info;
  190.         struct drm_framebuffer *fb = NULL;
  191.         struct drm_mode_fb_cmd2 mode_cmd;
  192.         struct drm_gem_object *gobj = NULL;
  193.         struct radeon_bo *rbo = NULL;
  194.         int ret;
  195.         unsigned long tmp;
  196.  
  197.         mode_cmd.width = sizes->surface_width;
  198.         mode_cmd.height = sizes->surface_height;
  199.  
  200.         /* avivo can't scanout real 24bpp */
  201.         if ((sizes->surface_bpp == 24) && ASIC_IS_AVIVO(rdev))
  202.                 sizes->surface_bpp = 32;
  203.  
  204.         mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
  205.                                                           sizes->surface_depth);
  206.  
  207.         ret = radeonfb_create_pinned_object(rfbdev, &mode_cmd, &gobj);
  208.         if (ret) {
  209.                 DRM_ERROR("failed to create fbcon object %d\n", ret);
  210.                 return ret;
  211.         }
  212.  
  213.         rbo = gem_to_radeon_bo(gobj);
  214.  
  215.         /* okay we have an object now allocate the framebuffer */
  216.         info = drm_fb_helper_alloc_fbi(helper);
  217.         if (IS_ERR(info)) {
  218.                 ret = PTR_ERR(info);
  219.                 goto out_unref;
  220.         }
  221.  
  222.         info->par = rfbdev;
  223.         info->skip_vt_switch = true;
  224.  
  225.         ret = radeon_framebuffer_init(rdev->ddev, &rfbdev->rfb, &mode_cmd, gobj);
  226.         if (ret) {
  227.                 DRM_ERROR("failed to initialize framebuffer %d\n", ret);
  228.                 goto out_destroy_fbi;
  229.         }
  230.  
  231.         fb = &rfbdev->rfb.base;
  232.  
  233.         /* setup helper */
  234.         rfbdev->helper.fb = fb;
  235.  
  236. //   memset_io(rbo->kptr, 0x0, radeon_bo_size(rbo));
  237.  
  238.         strcpy(info->fix.id, "radeondrmfb");
  239.  
  240.         drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
  241.  
  242.         info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT;
  243.         info->fbops = &radeonfb_ops;
  244.  
  245.         tmp = radeon_bo_gpu_offset(rbo) - rdev->mc.vram_start;
  246.         info->fix.smem_start = rdev->mc.aper_base + tmp;
  247.         info->fix.smem_len = radeon_bo_size(rbo);
  248.         info->screen_base = rbo->kptr;
  249.         info->screen_size = radeon_bo_size(rbo);
  250.  
  251.         drm_fb_helper_fill_var(info, &rfbdev->helper, sizes->fb_width, sizes->fb_height);
  252.  
  253.         /* setup aperture base/size for vesafb takeover */
  254.         info->apertures->ranges[0].base = rdev->ddev->mode_config.fb_base;
  255.         info->apertures->ranges[0].size = rdev->mc.aper_size;
  256.  
  257.         /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
  258.  
  259.  
  260.         DRM_INFO("fb mappable at 0x%lX\n",  info->fix.smem_start);
  261.         DRM_INFO("vram apper at 0x%lX\n",  (unsigned long)rdev->mc.aper_base);
  262.         DRM_INFO("size %lu\n", (unsigned long)radeon_bo_size(rbo));
  263.         DRM_INFO("fb depth is %d\n", fb->depth);
  264.         DRM_INFO("   pitch is %d\n", fb->pitches[0]);
  265.  
  266.     main_fb = fb;
  267.     main_fb_obj = gobj;
  268.  
  269.         return 0;
  270.  
  271. out_destroy_fbi:
  272. //   drm_fb_helper_release_fbi(helper);
  273. out_unref:
  274.         if (rbo) {
  275.  
  276.         }
  277.         if (fb && ret) {
  278.                 kfree(fb);
  279.         }
  280.         return ret;
  281. }
  282.  
  283.  
  284. static int radeon_fbdev_destroy(struct drm_device *dev, struct radeon_fbdev *rfbdev)
  285. {
  286.         struct radeon_framebuffer *rfb = &rfbdev->rfb;
  287.  
  288. //      drm_fb_helper_unregister_fbi(&rfbdev->helper);
  289. //      drm_fb_helper_release_fbi(&rfbdev->helper);
  290.  
  291.         if (rfb->obj) {
  292.                 rfb->obj = NULL;
  293.         }
  294. //   drm_fb_helper_fini(&rfbdev->helper);
  295.         drm_framebuffer_cleanup(&rfb->base);
  296.  
  297.         return 0;
  298. }
  299.  
  300. static const struct drm_fb_helper_funcs radeon_fb_helper_funcs = {
  301.         .gamma_set = radeon_crtc_fb_gamma_set,
  302.         .gamma_get = radeon_crtc_fb_gamma_get,
  303.         .fb_probe = radeonfb_create,
  304. };
  305.  
  306. int radeon_fbdev_init(struct radeon_device *rdev)
  307. {
  308.         struct radeon_fbdev *rfbdev;
  309.         int bpp_sel = 32;
  310.         int ret;
  311.  
  312.         /* don't enable fbdev if no connectors */
  313.         if (list_empty(&rdev->ddev->mode_config.connector_list))
  314.                 return 0;
  315.  
  316.         /* select 8 bpp console on RN50 or 16MB cards */
  317.         if (ASIC_IS_RN50(rdev) || rdev->mc.real_vram_size <= (32*1024*1024))
  318.                 bpp_sel = 8;
  319.  
  320.         rfbdev = kzalloc(sizeof(struct radeon_fbdev), GFP_KERNEL);
  321.         if (!rfbdev)
  322.                 return -ENOMEM;
  323.  
  324.         rfbdev->rdev = rdev;
  325.         rdev->mode_info.rfbdev = rfbdev;
  326.  
  327.         drm_fb_helper_prepare(rdev->ddev, &rfbdev->helper,
  328.                               &radeon_fb_helper_funcs);
  329.  
  330.         ret = drm_fb_helper_init(rdev->ddev, &rfbdev->helper,
  331.                                  rdev->num_crtc,
  332.                                  RADEONFB_CONN_LIMIT);
  333.         if (ret)
  334.                 goto free;
  335.  
  336.         ret = drm_fb_helper_single_add_all_connectors(&rfbdev->helper);
  337.         if (ret)
  338.                 goto fini;
  339.  
  340.         /* disable all the possible outputs/crtcs before entering KMS mode */
  341.         drm_helper_disable_unused_functions(rdev->ddev);
  342.  
  343.         ret = drm_fb_helper_initial_config(&rfbdev->helper, bpp_sel);
  344.         if (ret)
  345.                 goto fini;
  346.  
  347.         return 0;
  348.  
  349. fini:
  350. //   drm_fb_helper_fini(&rfbdev->helper);
  351. free:
  352.         kfree(rfbdev);
  353.         return ret;
  354. }
  355.  
  356. void radeon_fbdev_fini(struct radeon_device *rdev)
  357. {
  358.         if (!rdev->mode_info.rfbdev)
  359.                 return;
  360.  
  361.         radeon_fbdev_destroy(rdev->ddev, rdev->mode_info.rfbdev);
  362.         kfree(rdev->mode_info.rfbdev);
  363.         rdev->mode_info.rfbdev = NULL;
  364. }
  365.  
  366. bool radeon_fbdev_robj_is_fb(struct radeon_device *rdev, struct radeon_bo *robj)
  367. {
  368.         if (robj == gem_to_radeon_bo(rdev->mode_info.rfbdev->rfb.obj))
  369.                 return true;
  370.         return false;
  371. }
  372.  
  373. void radeon_fb_add_connector(struct radeon_device *rdev, struct drm_connector *connector)
  374. {
  375.         drm_fb_helper_add_one_connector(&rdev->mode_info.rfbdev->helper, connector);
  376. }
  377.  
  378. void radeon_fb_remove_connector(struct radeon_device *rdev, struct drm_connector *connector)
  379. {
  380.         drm_fb_helper_remove_one_connector(&rdev->mode_info.rfbdev->helper, connector);
  381. }
  382.