Subversion Repositories Kolibri OS

Rev

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