intel_fb.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. uint32_t surface_depth, uint32_t surface_bpp,
  101. struct drm_framebuffer **fb_p)
  102. {
  103. struct fb_info *info;
  104. struct intelfb_par *par;
  105. struct drm_framebuffer *fb;
  106. struct intel_framebuffer *intel_fb;
  107. struct drm_mode_fb_cmd mode_cmd;
  108. struct drm_gem_object *fbo = NULL;
  109. struct drm_i915_gem_object *obj_priv;
  110. struct device *device = &dev->pdev->dev;
  111. int size, ret, mmio_bar = IS_I9XX(dev) ? 0 : 1;
  112. mode_cmd.width = surface_width;
  113. mode_cmd.height = surface_height;
  114. mode_cmd.bpp = surface_bpp;
  115. mode_cmd.pitch = ALIGN(mode_cmd.width * ((mode_cmd.bpp + 1) / 8), 64);
  116. mode_cmd.depth = surface_depth;
  117. size = mode_cmd.pitch * mode_cmd.height;
  118. size = ALIGN(size, PAGE_SIZE);
  119. fbo = drm_gem_object_alloc(dev, size);
  120. if (!fbo) {
  121. DRM_ERROR("failed to allocate framebuffer\n");
  122. ret = -ENOMEM;
  123. goto out;
  124. }
  125. obj_priv = fbo->driver_private;
  126. mutex_lock(&dev->struct_mutex);
  127. ret = i915_gem_object_pin(fbo, PAGE_SIZE);
  128. if (ret) {
  129. DRM_ERROR("failed to pin fb: %d\n", ret);
  130. goto out_unref;
  131. }
  132. /* Flush everything out, we'll be doing GTT only from now on */
  133. i915_gem_object_set_to_gtt_domain(fbo, 1);
  134. ret = intel_framebuffer_create(dev, &mode_cmd, &fb, fbo);
  135. if (ret) {
  136. DRM_ERROR("failed to allocate fb.\n");
  137. goto out_unpin;
  138. }
  139. list_add(&fb->filp_head, &dev->mode_config.fb_kernel_list);
  140. intel_fb = to_intel_framebuffer(fb);
  141. *fb_p = fb;
  142. info = framebuffer_alloc(sizeof(struct intelfb_par), device);
  143. if (!info) {
  144. ret = -ENOMEM;
  145. goto out_unpin;
  146. }
  147. par = info->par;
  148. par->helper.funcs = &intel_fb_helper_funcs;
  149. par->helper.dev = dev;
  150. ret = drm_fb_helper_init_crtc_count(&par->helper, 2,
  151. INTELFB_CONN_LIMIT);
  152. if (ret)
  153. goto out_unref;
  154. strcpy(info->fix.id, "inteldrmfb");
  155. info->flags = FBINFO_DEFAULT;
  156. info->fbops = &intelfb_ops;
  157. /* setup aperture base/size for vesafb takeover */
  158. info->aperture_base = dev->mode_config.fb_base;
  159. if (IS_I9XX(dev))
  160. info->aperture_size = pci_resource_len(dev->pdev, 2);
  161. else
  162. info->aperture_size = pci_resource_len(dev->pdev, 0);
  163. info->fix.smem_start = dev->mode_config.fb_base + obj_priv->gtt_offset;
  164. info->fix.smem_len = size;
  165. info->flags = FBINFO_DEFAULT;
  166. info->screen_base = ioremap_wc(dev->agp->base + obj_priv->gtt_offset,
  167. size);
  168. if (!info->screen_base) {
  169. ret = -ENOSPC;
  170. goto out_unpin;
  171. }
  172. info->screen_size = size;
  173. // memset(info->screen_base, 0, size);
  174. drm_fb_helper_fill_fix(info, fb->pitch);
  175. drm_fb_helper_fill_var(info, fb, fb_width, fb_height);
  176. /* FIXME: we really shouldn't expose mmio space at all */
  177. info->fix.mmio_start = pci_resource_start(dev->pdev, mmio_bar);
  178. info->fix.mmio_len = pci_resource_len(dev->pdev, mmio_bar);
  179. info->pixmap.size = 64*1024;
  180. info->pixmap.buf_align = 8;
  181. info->pixmap.access_align = 32;
  182. info->pixmap.flags = FB_PIXMAP_SYSTEM;
  183. info->pixmap.scan_align = 1;
  184. fb->fbdev = info;
  185. par->intel_fb = intel_fb;
  186. /* To allow resizeing without swapping buffers */
  187. DRM_DEBUG("allocated %dx%d fb: 0x%08x, bo %p\n", intel_fb->base.width,
  188. intel_fb->base.height, obj_priv->gtt_offset, fbo);
  189. mutex_unlock(&dev->struct_mutex);
  190. return 0;
  191. out_unpin:
  192. i915_gem_object_unpin(fbo);
  193. out_unref:
  194. drm_gem_object_unreference(fbo);
  195. mutex_unlock(&dev->struct_mutex);
  196. out:
  197. return ret;
  198. }
  199. int intelfb_probe(struct drm_device *dev)
  200. {
  201. int ret;
  202. DRM_DEBUG("\n");
  203. ret = drm_fb_helper_single_fb_probe(dev, intelfb_create);
  204. return ret;
  205. }
  206. EXPORT_SYMBOL(intelfb_probe);
  207. int intelfb_remove(struct drm_device *dev, struct drm_framebuffer *fb)
  208. {
  209. struct fb_info *info;
  210. if (!fb)
  211. return -EINVAL;
  212. info = fb->fbdev;
  213. if (info) {
  214. struct intelfb_par *par = info->par;
  215. unregister_framebuffer(info);
  216. iounmap(info->screen_base);
  217. if (info->par)
  218. drm_fb_helper_free(&par->helper);
  219. framebuffer_release(info);
  220. }
  221. return 0;
  222. }
  223. EXPORT_SYMBOL(intelfb_remove);
  224. MODULE_LICENSE("GPL and additional rights");