intel_fb.c 7.3 KB

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