Subversion Repositories Kolibri OS

Rev

Rev 1986 | Rev 5078 | Go to most recent revision | 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.  
  39. int radeonfb_create_pinned_object(struct radeon_fbdev *rfbdev,
  40.                      struct drm_mode_fb_cmd2 *mode_cmd,
  41.                      struct drm_gem_object **gobj_p);
  42.  
  43. /* object hierarchy -
  44.    this contains a helper + a radeon fb
  45.    the helper contains a pointer to radeon framebuffer baseclass.
  46. */
  47. struct radeon_fbdev {
  48.     struct drm_fb_helper        helper;
  49.         struct radeon_framebuffer rfb;
  50.         struct list_head fbdev_list;
  51.         struct radeon_device            *rdev;
  52. };
  53.  
  54. static struct fb_ops radeonfb_ops = {
  55.         .owner = THIS_MODULE,
  56.         .fb_check_var = drm_fb_helper_check_var,
  57.         .fb_set_par = drm_fb_helper_set_par,
  58. //      .fb_fillrect = cfb_fillrect,
  59. //      .fb_copyarea = cfb_copyarea,
  60. //      .fb_imageblit = cfb_imageblit,
  61. //      .fb_pan_display = drm_fb_helper_pan_display,
  62.         .fb_blank = drm_fb_helper_blank,
  63.         .fb_setcmap = drm_fb_helper_setcmap,
  64. };
  65.  
  66.  
  67. int radeon_align_pitch(struct radeon_device *rdev, int width, int bpp, bool tiled)
  68. {
  69.         int aligned = width;
  70.         int align_large = (ASIC_IS_AVIVO(rdev)) || tiled;
  71.         int pitch_mask = 0;
  72.  
  73.         switch (bpp / 8) {
  74.         case 1:
  75.                 pitch_mask = align_large ? 255 : 127;
  76.                 break;
  77.         case 2:
  78.                 pitch_mask = align_large ? 127 : 31;
  79.                 break;
  80.         case 3:
  81.         case 4:
  82.                 pitch_mask = align_large ? 63 : 15;
  83.                 break;
  84.         }
  85.  
  86.         aligned += pitch_mask;
  87.         aligned &= ~pitch_mask;
  88.         return aligned;
  89. }
  90.  
  91.  
  92. static int radeonfb_create(struct radeon_fbdev *rfbdev,
  93.                            struct drm_fb_helper_surface_size *sizes)
  94. {
  95.         struct radeon_device *rdev = rfbdev->rdev;
  96.         struct fb_info *info;
  97.         struct drm_framebuffer *fb = NULL;
  98.         struct drm_mode_fb_cmd2 mode_cmd;
  99.         struct drm_gem_object *gobj = NULL;
  100.         struct radeon_bo *rbo = NULL;
  101.         struct device *device = &rdev->pdev->dev;
  102.         int ret;
  103.         unsigned long tmp;
  104.  
  105.     ENTER();
  106.  
  107.         mode_cmd.width = sizes->surface_width;
  108.         mode_cmd.height = sizes->surface_height;
  109.  
  110.         /* avivo can't scanout real 24bpp */
  111.         if ((sizes->surface_bpp == 24) && ASIC_IS_AVIVO(rdev))
  112.                 sizes->surface_bpp = 32;
  113.  
  114.         mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
  115.                                                           sizes->surface_depth);
  116.  
  117.         ret = radeonfb_create_pinned_object(rfbdev, &mode_cmd, &gobj);
  118.         if (ret) {
  119.                 DRM_ERROR("failed to create fbcon object %d\n", ret);
  120.                 return ret;
  121.         }
  122.  
  123.         rbo = gem_to_radeon_bo(gobj);
  124.  
  125.         /* okay we have an object now allocate the framebuffer */
  126.         info = framebuffer_alloc(0, device);
  127.         if (info == NULL) {
  128.         dbgprintf("framebuffer_alloc\n");
  129.                 ret = -ENOMEM;
  130.                 goto out_unref;
  131.         }
  132.  
  133.         info->par = rfbdev;
  134.  
  135.         ret = radeon_framebuffer_init(rdev->ddev, &rfbdev->rfb, &mode_cmd, gobj);
  136.         if (ret) {
  137.                 DRM_ERROR("failed to initalise framebuffer %d\n", ret);
  138.                 goto out_unref;
  139.         }
  140.  
  141.         fb = &rfbdev->rfb.base;
  142.  
  143.         /* setup helper */
  144.         rfbdev->helper.fb = fb;
  145.         rfbdev->helper.fbdev = info;
  146.  
  147. //   memset_io(rbo->kptr, 0x0, radeon_bo_size(rbo));
  148.  
  149.         strcpy(info->fix.id, "radeondrmfb");
  150.  
  151.         drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
  152.  
  153.         info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT;
  154.         info->fbops = &radeonfb_ops;
  155.  
  156.         tmp = radeon_bo_gpu_offset(rbo) - rdev->mc.vram_start;
  157.         info->fix.smem_start = rdev->mc.aper_base + tmp;
  158.         info->fix.smem_len = radeon_bo_size(rbo);
  159.         info->screen_base = rbo->kptr;
  160.         info->screen_size = radeon_bo_size(rbo);
  161.  
  162.         drm_fb_helper_fill_var(info, &rfbdev->helper, sizes->fb_width, sizes->fb_height);
  163.  
  164.         /* setup aperture base/size for vesafb takeover */
  165.         info->apertures = alloc_apertures(1);
  166.         if (!info->apertures) {
  167.                 ret = -ENOMEM;
  168.                 goto out_unref;
  169.         }
  170.         info->apertures->ranges[0].base = rdev->ddev->mode_config.fb_base;
  171.         info->apertures->ranges[0].size = rdev->mc.aper_size;
  172.  
  173.         /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
  174.  
  175. //   if (info->screen_base == NULL) {
  176. //       ret = -ENOSPC;
  177. //       goto out_unref;
  178. //   }
  179.         DRM_INFO("fb mappable at 0x%lX\n",  info->fix.smem_start);
  180.         DRM_INFO("vram apper at 0x%lX\n",  (unsigned long)rdev->mc.aper_base);
  181.         DRM_INFO("size %lu\n", (unsigned long)radeon_bo_size(rbo));
  182.         DRM_INFO("fb depth is %d\n", fb->depth);
  183.         DRM_INFO("   pitch is %d\n", fb->pitches[0]);
  184.  
  185.  
  186.     LEAVE();
  187.  
  188.     return 0;
  189.  
  190. out_unref:
  191.         if (rbo) {
  192.  
  193.         }
  194.         if (fb && ret) {
  195.                 kfree(fb);
  196.         }
  197.         return ret;
  198. }
  199.  
  200. static int radeon_fb_find_or_create_single(struct drm_fb_helper *helper,
  201.                                            struct drm_fb_helper_surface_size *sizes)
  202. {
  203.         struct radeon_fbdev *rfbdev = (struct radeon_fbdev *)helper;
  204.         int new_fb = 0;
  205.         int ret;
  206.  
  207.         if (!helper->fb) {
  208.                 ret = radeonfb_create(rfbdev, sizes);
  209.                 if (ret)
  210.                         return ret;
  211.                 new_fb = 1;
  212.         }
  213.         return new_fb;
  214. }
  215. static int radeon_fbdev_destroy(struct drm_device *dev, struct radeon_fbdev *rfbdev)
  216. {
  217.         struct fb_info *info;
  218.         struct radeon_framebuffer *rfb = &rfbdev->rfb;
  219.  
  220.         if (rfbdev->helper.fbdev) {
  221.                 info = rfbdev->helper.fbdev;
  222.  
  223. //       unregister_framebuffer(info);
  224. //       framebuffer_release(info);
  225.         }
  226.  
  227.         if (rfb->obj) {
  228.                 rfb->obj = NULL;
  229.         }
  230. //   drm_fb_helper_fini(&rfbdev->helper);
  231.         drm_framebuffer_cleanup(&rfb->base);
  232.  
  233.         return 0;
  234. }
  235.  
  236. static struct drm_fb_helper_funcs radeon_fb_helper_funcs = {
  237.         .gamma_set = radeon_crtc_fb_gamma_set,
  238.         .gamma_get = radeon_crtc_fb_gamma_get,
  239.         .fb_probe = radeon_fb_find_or_create_single,
  240. };
  241.  
  242. extern struct radeon_fbdev *kos_rfbdev;
  243.  
  244. int radeon_fbdev_init(struct radeon_device *rdev)
  245. {
  246.         struct radeon_fbdev *rfbdev;
  247.         int bpp_sel = 32;
  248.         int ret;
  249.  
  250.     ENTER();
  251.  
  252.         /* select 8 bpp console on RN50 or 16MB cards */
  253.         if (ASIC_IS_RN50(rdev) || rdev->mc.real_vram_size <= (32*1024*1024))
  254.                 bpp_sel = 8;
  255.  
  256.         rfbdev = kzalloc(sizeof(struct radeon_fbdev), GFP_KERNEL);
  257.         if (!rfbdev)
  258.                 return -ENOMEM;
  259.  
  260.         rfbdev->rdev = rdev;
  261.         rdev->mode_info.rfbdev = rfbdev;
  262.         rfbdev->helper.funcs = &radeon_fb_helper_funcs;
  263.  
  264.         ret = drm_fb_helper_init(rdev->ddev, &rfbdev->helper,
  265.                                  rdev->num_crtc,
  266.                                  RADEONFB_CONN_LIMIT);
  267.         if (ret) {
  268.                 kfree(rfbdev);
  269.                 return ret;
  270.         }
  271.  
  272.         drm_fb_helper_single_add_all_connectors(&rfbdev->helper);
  273.         drm_fb_helper_initial_config(&rfbdev->helper, bpp_sel);
  274.  
  275.     kos_rfbdev = rfbdev;
  276.  
  277.     LEAVE();
  278.  
  279.         return 0;
  280. }
  281.  
  282. void radeon_fbdev_fini(struct radeon_device *rdev)
  283. {
  284.         if (!rdev->mode_info.rfbdev)
  285.                 return;
  286.  
  287.         radeon_fbdev_destroy(rdev->ddev, rdev->mode_info.rfbdev);
  288.         kfree(rdev->mode_info.rfbdev);
  289.         rdev->mode_info.rfbdev = NULL;
  290. }
  291.  
  292.  
  293. int radeon_fbdev_total_size(struct radeon_device *rdev)
  294. {
  295.         struct radeon_bo *robj;
  296.         int size = 0;
  297.  
  298.         robj = gem_to_radeon_bo(rdev->mode_info.rfbdev->rfb.obj);
  299.         size += radeon_bo_size(robj);
  300.         return size;
  301. }
  302.  
  303. bool radeon_fbdev_robj_is_fb(struct radeon_device *rdev, struct radeon_bo *robj)
  304. {
  305.         if (robj == gem_to_radeon_bo(rdev->mode_info.rfbdev->rfb.obj))
  306.                 return true;
  307.         return false;
  308. }
  309.