intel_fb.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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/kernel.h>
  28. #include <linux/errno.h>
  29. #include <linux/string.h>
  30. #include <linux/mm.h>
  31. #include <linux/tty.h>
  32. #include <linux/slab.h>
  33. #include <linux/sysrq.h>
  34. #include <linux/delay.h>
  35. #include <linux/fb.h>
  36. #include <linux/init.h>
  37. #include "drmP.h"
  38. #include "drm.h"
  39. #include "drm_crtc.h"
  40. #include "drm_fb_helper.h"
  41. #include "intel_drv.h"
  42. #include "i915_drm.h"
  43. #include "i915_drv.h"
  44. struct intelfb_par {
  45. struct drm_fb_helper helper;
  46. struct intel_framebuffer *intel_fb;
  47. struct drm_display_mode *our_mode;
  48. };
  49. static struct fb_ops intelfb_ops = {
  50. .owner = THIS_MODULE,
  51. .fb_check_var = drm_fb_helper_check_var,
  52. .fb_set_par = drm_fb_helper_set_par,
  53. .fb_setcolreg = drm_fb_helper_setcolreg,
  54. .fb_fillrect = cfb_fillrect,
  55. .fb_copyarea = cfb_copyarea,
  56. .fb_imageblit = cfb_imageblit,
  57. .fb_pan_display = drm_fb_helper_pan_display,
  58. .fb_blank = drm_fb_helper_blank,
  59. };
  60. static struct drm_fb_helper_funcs intel_fb_helper_funcs = {
  61. .gamma_set = intel_crtc_fb_gamma_set,
  62. };
  63. /**
  64. * Curretly it is assumed that the old framebuffer is reused.
  65. *
  66. * LOCKING
  67. * caller should hold the mode config lock.
  68. *
  69. */
  70. int intelfb_resize(struct drm_device *dev, struct drm_crtc *crtc)
  71. {
  72. struct fb_info *info;
  73. struct drm_framebuffer *fb;
  74. struct drm_display_mode *mode = crtc->desired_mode;
  75. fb = crtc->fb;
  76. if (!fb)
  77. return 1;
  78. info = fb->fbdev;
  79. if (!info)
  80. return 1;
  81. if (!mode)
  82. return 1;
  83. info->var.xres = mode->hdisplay;
  84. info->var.right_margin = mode->hsync_start - mode->hdisplay;
  85. info->var.hsync_len = mode->hsync_end - mode->hsync_start;
  86. info->var.left_margin = mode->htotal - mode->hsync_end;
  87. info->var.yres = mode->vdisplay;
  88. info->var.lower_margin = mode->vsync_start - mode->vdisplay;
  89. info->var.vsync_len = mode->vsync_end - mode->vsync_start;
  90. info->var.upper_margin = mode->vtotal - mode->vsync_end;
  91. info->var.pixclock = 10000000 / mode->htotal * 1000 / mode->vtotal * 100;
  92. /* avoid overflow */
  93. info->var.pixclock = info->var.pixclock * 1000 / mode->vrefresh;
  94. return 0;
  95. }
  96. EXPORT_SYMBOL(intelfb_resize);
  97. static int intelfb_create(struct drm_device *dev, uint32_t fb_width,
  98. uint32_t fb_height, uint32_t surface_width,
  99. uint32_t surface_height,
  100. struct drm_framebuffer **fb_p)
  101. {
  102. struct fb_info *info;
  103. struct intelfb_par *par;
  104. struct drm_framebuffer *fb;
  105. struct intel_framebuffer *intel_fb;
  106. struct drm_mode_fb_cmd mode_cmd;
  107. struct drm_gem_object *fbo = NULL;
  108. struct drm_i915_gem_object *obj_priv;
  109. struct device *device = &dev->pdev->dev;
  110. int size, ret, mmio_bar = IS_I9XX(dev) ? 0 : 1;
  111. mode_cmd.width = surface_width;
  112. mode_cmd.height = surface_height;
  113. mode_cmd.bpp = 32;
  114. mode_cmd.pitch = ALIGN(mode_cmd.width * ((mode_cmd.bpp + 1) / 8), 64);
  115. mode_cmd.depth = 24;
  116. size = mode_cmd.pitch * mode_cmd.height;
  117. size = ALIGN(size, PAGE_SIZE);
  118. fbo = drm_gem_object_alloc(dev, size);
  119. if (!fbo) {
  120. DRM_ERROR("failed to allocate framebuffer\n");
  121. ret = -ENOMEM;
  122. goto out;
  123. }
  124. obj_priv = fbo->driver_private;
  125. mutex_lock(&dev->struct_mutex);
  126. ret = i915_gem_object_pin(fbo, PAGE_SIZE);
  127. if (ret) {
  128. DRM_ERROR("failed to pin fb: %d\n", ret);
  129. goto out_unref;
  130. }
  131. /* Flush everything out, we'll be doing GTT only from now on */
  132. i915_gem_object_set_to_gtt_domain(fbo, 1);
  133. ret = intel_framebuffer_create(dev, &mode_cmd, &fb, fbo);
  134. if (ret) {
  135. DRM_ERROR("failed to allocate fb.\n");
  136. goto out_unpin;
  137. }
  138. list_add(&fb->filp_head, &dev->mode_config.fb_kernel_list);
  139. intel_fb = to_intel_framebuffer(fb);
  140. *fb_p = fb;
  141. info = framebuffer_alloc(sizeof(struct intelfb_par), device);
  142. if (!info) {
  143. ret = -ENOMEM;
  144. goto out_unpin;
  145. }
  146. par = info->par;
  147. par->helper.funcs = &intel_fb_helper_funcs;
  148. par->helper.dev = dev;
  149. ret = drm_fb_helper_init_crtc_count(&par->helper, 2,
  150. INTELFB_CONN_LIMIT);
  151. if (ret)
  152. goto out_unref;
  153. strcpy(info->fix.id, "inteldrmfb");
  154. info->flags = FBINFO_DEFAULT;
  155. info->fbops = &intelfb_ops;
  156. /* setup aperture base/size for vesafb takeover */
  157. info->aperture_base = dev->mode_config.fb_base;
  158. if (IS_I9XX(dev))
  159. info->aperture_size = pci_resource_len(dev->pdev, 2);
  160. else
  161. info->aperture_size = pci_resource_len(dev->pdev, 0);
  162. info->fix.smem_start = dev->mode_config.fb_base + obj_priv->gtt_offset;
  163. info->fix.smem_len = size;
  164. info->flags = FBINFO_DEFAULT;
  165. info->screen_base = ioremap_wc(dev->agp->base + obj_priv->gtt_offset,
  166. size);
  167. if (!info->screen_base) {
  168. ret = -ENOSPC;
  169. goto out_unpin;
  170. }
  171. info->screen_size = size;
  172. // memset(info->screen_base, 0, size);
  173. drm_fb_helper_fill_fix(info, fb->pitch);
  174. drm_fb_helper_fill_var(info, fb, fb_width, fb_height);
  175. /* FIXME: we really shouldn't expose mmio space at all */
  176. info->fix.mmio_start = pci_resource_start(dev->pdev, mmio_bar);
  177. info->fix.mmio_len = pci_resource_len(dev->pdev, mmio_bar);
  178. info->pixmap.size = 64*1024;
  179. info->pixmap.buf_align = 8;
  180. info->pixmap.access_align = 32;
  181. info->pixmap.flags = FB_PIXMAP_SYSTEM;
  182. info->pixmap.scan_align = 1;
  183. fb->fbdev = info;
  184. par->intel_fb = intel_fb;
  185. /* To allow resizeing without swapping buffers */
  186. DRM_DEBUG("allocated %dx%d fb: 0x%08x, bo %p\n", intel_fb->base.width,
  187. intel_fb->base.height, obj_priv->gtt_offset, fbo);
  188. mutex_unlock(&dev->struct_mutex);
  189. return 0;
  190. out_unpin:
  191. i915_gem_object_unpin(fbo);
  192. out_unref:
  193. drm_gem_object_unreference(fbo);
  194. mutex_unlock(&dev->struct_mutex);
  195. out:
  196. return ret;
  197. }
  198. int intelfb_probe(struct drm_device *dev)
  199. {
  200. int ret;
  201. DRM_DEBUG("\n");
  202. ret = drm_fb_helper_single_fb_probe(dev, intelfb_create);
  203. return ret;
  204. }
  205. EXPORT_SYMBOL(intelfb_probe);
  206. int intelfb_remove(struct drm_device *dev, struct drm_framebuffer *fb)
  207. {
  208. struct fb_info *info;
  209. if (!fb)
  210. return -EINVAL;
  211. info = fb->fbdev;
  212. if (info) {
  213. struct intelfb_par *par = info->par;
  214. unregister_framebuffer(info);
  215. iounmap(info->screen_base);
  216. if (info->par)
  217. drm_fb_helper_free(&par->helper);
  218. framebuffer_release(info);
  219. }
  220. return 0;
  221. }
  222. EXPORT_SYMBOL(intelfb_remove);
  223. MODULE_LICENSE("GPL and additional rights");