intel_fb.c 7.3 KB

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