Subversion Repositories Kolibri OS

Rev

Rev 2344 | Blame | 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.  
  27. #include <linux/module.h>
  28. #include <linux/kernel.h>
  29. #include <linux/errno.h>
  30. #include <linux/string.h>
  31. //#include <linux/mm.h>
  32. //#include <linux/tty.h>
  33. #include <linux/sysrq.h>
  34. //#include <linux/delay.h>
  35. #include <linux/fb.h>
  36. //#include <linux/init.h>
  37. //#include <linux/vga_switcheroo.h>
  38.  
  39. #include <drm/drmP.h>
  40. #include <drm/drm_crtc.h>
  41. #include <drm/drm_fb_helper.h>
  42. #include "intel_drv.h"
  43. #include <drm/i915_drm.h>
  44. #include "i915_drv.h"
  45.  
  46.  
  47. struct fb_info *framebuffer_alloc(size_t size, struct device *dev)
  48. {
  49. #define BYTES_PER_LONG (BITS_PER_LONG/8)
  50. #define PADDING (BYTES_PER_LONG - (sizeof(struct fb_info) % BYTES_PER_LONG))
  51.     int fb_info_size = sizeof(struct fb_info);
  52.     struct fb_info *info;
  53.     char *p;
  54.  
  55.     if (size)
  56.         fb_info_size += PADDING;
  57.  
  58.     p = kzalloc(fb_info_size + size, GFP_KERNEL);
  59.  
  60.     if (!p)
  61.         return NULL;
  62.  
  63.     info = (struct fb_info *) p;
  64.  
  65.     if (size)
  66.         info->par = p + fb_info_size;
  67.  
  68.     return info;
  69. #undef PADDING
  70. #undef BYTES_PER_LONG
  71. }
  72.  
  73.  
  74. static struct fb_ops intelfb_ops = {
  75.         .owner = THIS_MODULE,
  76.         .fb_check_var = drm_fb_helper_check_var,
  77.         .fb_set_par = drm_fb_helper_set_par,
  78. //   .fb_fillrect = cfb_fillrect,
  79. //   .fb_copyarea = cfb_copyarea,
  80. //   .fb_imageblit = cfb_imageblit,
  81. //   .fb_pan_display = drm_fb_helper_pan_display,
  82.         .fb_blank = drm_fb_helper_blank,
  83. //   .fb_setcmap = drm_fb_helper_setcmap,
  84. //   .fb_debug_enter = drm_fb_helper_debug_enter,
  85. //   .fb_debug_leave = drm_fb_helper_debug_leave,
  86. };
  87.  
  88. static int intelfb_create(struct intel_fbdev *ifbdev,
  89.                           struct drm_fb_helper_surface_size *sizes)
  90. {
  91.         struct drm_device *dev = ifbdev->helper.dev;
  92.         struct drm_i915_private *dev_priv = dev->dev_private;
  93.         struct fb_info *info;
  94.         struct drm_framebuffer *fb;
  95.         struct drm_mode_fb_cmd2 mode_cmd = {};
  96.         struct drm_i915_gem_object *obj;
  97.         struct device *device = &dev->pdev->dev;
  98.         int size, ret;
  99.  
  100.         /* we don't do packed 24bpp */
  101.         if (sizes->surface_bpp == 24)
  102.                 sizes->surface_bpp = 32;
  103.  
  104.         mode_cmd.width = sizes->surface_width;
  105.         mode_cmd.height = sizes->surface_height;
  106.  
  107.         mode_cmd.pitches[0] = ALIGN(mode_cmd.width * ((sizes->surface_bpp + 7) /
  108.                                                       8), 64);
  109.         mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
  110.                                                           sizes->surface_depth);
  111.  
  112.         size = mode_cmd.pitches[0] * mode_cmd.height;
  113.         size = ALIGN(size, PAGE_SIZE);
  114.         obj = i915_gem_alloc_object(dev, size);
  115.         if (!obj) {
  116.                 DRM_ERROR("failed to allocate framebuffer\n");
  117.                 ret = -ENOMEM;
  118.                 goto out;
  119.         }
  120.  
  121.         mutex_lock(&dev->struct_mutex);
  122.  
  123. #if 0
  124. // skip this part and use existing framebiffer
  125.  
  126.         /* Flush everything out, we'll be doing GTT only from now on */
  127.         ret = intel_pin_and_fence_fb_obj(dev, obj, false);
  128.         if (ret) {
  129.                 DRM_ERROR("failed to pin fb: %d\n", ret);
  130.                 goto out_unref;
  131.         }
  132. #endif
  133.  
  134. /***********************************************************************/
  135.     {
  136. #define LFB_SIZE 0xC00000
  137.  
  138.         static struct drm_mm_node lfb_vm_node;
  139.  
  140.         lfb_vm_node.size = LFB_SIZE;
  141.         lfb_vm_node.start = 0;
  142.         lfb_vm_node.mm = NULL;
  143.  
  144.         obj->gtt_space = &lfb_vm_node;
  145.         obj->gtt_offset = 0;
  146.         obj->pin_count = 2;
  147.     }
  148. /***********************************************************************/
  149.  
  150.         info = framebuffer_alloc(0, device);
  151.         if (!info) {
  152.                 ret = -ENOMEM;
  153.                 goto out_unpin;
  154.         }
  155.  
  156.         info->par = ifbdev;
  157.  
  158.         ret = intel_framebuffer_init(dev, &ifbdev->ifb, &mode_cmd, obj);
  159.         if (ret)
  160.                 goto out_unpin;
  161.  
  162.         fb = &ifbdev->ifb.base;
  163.  
  164.         ifbdev->helper.fb = fb;
  165.         ifbdev->helper.fbdev = info;
  166.  
  167.     strcpy(info->fix.id, "inteldrmfb");
  168.  
  169.     info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT;
  170.     info->fbops = &intelfb_ops;
  171.  
  172.     /* setup aperture base/size for vesafb takeover */
  173.         info->apertures = alloc_apertures(1);
  174.         if (!info->apertures) {
  175.                 ret = -ENOMEM;
  176.                 goto out_unpin;
  177.         }
  178.         info->apertures->ranges[0].base = dev->mode_config.fb_base;
  179.         info->apertures->ranges[0].size =
  180.                 dev_priv->mm.gtt->gtt_mappable_entries << PAGE_SHIFT;
  181.  
  182.         info->fix.smem_start = dev->mode_config.fb_base + obj->gtt_offset;
  183.         info->fix.smem_len = size;
  184.  
  185.         info->screen_base = 0xFE000000;
  186.         info->screen_size = size;
  187.  
  188. //      memset(info->screen_base, 0, size);
  189.  
  190.         drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
  191.         drm_fb_helper_fill_var(info, &ifbdev->helper, sizes->fb_width, sizes->fb_height);
  192.  
  193.         /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
  194.  
  195.         DRM_DEBUG_KMS("allocated %dx%d fb: 0x%08x, bo %p\n",
  196.                       fb->width, fb->height,
  197.                       obj->gtt_offset, obj);
  198.  
  199.  
  200.         mutex_unlock(&dev->struct_mutex);
  201. //   vga_switcheroo_client_fb_set(dev->pdev, info);
  202.  
  203.         return 0;
  204.  
  205. out_unpin:
  206.         i915_gem_object_unpin(obj);
  207. out_unref:
  208.         drm_gem_object_unreference(&obj->base);
  209.         mutex_unlock(&dev->struct_mutex);
  210. out:
  211.         return ret;
  212. }
  213.  
  214. static int intel_fb_find_or_create_single(struct drm_fb_helper *helper,
  215.                                           struct drm_fb_helper_surface_size *sizes)
  216. {
  217.         struct intel_fbdev *ifbdev = (struct intel_fbdev *)helper;
  218.         int new_fb = 0;
  219.         int ret;
  220.  
  221.         if (!helper->fb) {
  222.                 ret = intelfb_create(ifbdev, sizes);
  223.                 if (ret)
  224.                         return ret;
  225.                 new_fb = 1;
  226.         }
  227.         return new_fb;
  228. }
  229.  
  230. static struct drm_fb_helper_funcs intel_fb_helper_funcs = {
  231.         .gamma_set = intel_crtc_fb_gamma_set,
  232.         .gamma_get = intel_crtc_fb_gamma_get,
  233.     .fb_probe = intel_fb_find_or_create_single,
  234. };
  235.  
  236.  
  237. int intel_fbdev_init(struct drm_device *dev)
  238. {
  239.         struct intel_fbdev *ifbdev;
  240.         drm_i915_private_t *dev_priv = dev->dev_private;
  241.         int ret;
  242.  
  243.     ENTER();
  244.  
  245.         ifbdev = kzalloc(sizeof(struct intel_fbdev), GFP_KERNEL);
  246.         if (!ifbdev)
  247.                 return -ENOMEM;
  248.  
  249.         dev_priv->fbdev = ifbdev;
  250.         ifbdev->helper.funcs = &intel_fb_helper_funcs;
  251.  
  252.         ret = drm_fb_helper_init(dev, &ifbdev->helper,
  253.                                  dev_priv->num_pipe,
  254.                                  INTELFB_CONN_LIMIT);
  255.         if (ret) {
  256.                 kfree(ifbdev);
  257.                 return ret;
  258.         }
  259.  
  260.         drm_fb_helper_single_add_all_connectors(&ifbdev->helper);
  261.         drm_fb_helper_initial_config(&ifbdev->helper, 32);
  262.  
  263.     LEAVE();
  264.         return 0;
  265. }
  266.  
  267.  
  268.