intel_fbdev.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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/sysrq.h>
  33. #include <linux/delay.h>
  34. #include <linux/fb.h>
  35. #include <linux/init.h>
  36. #include <linux/vga_switcheroo.h>
  37. #include <drm/drmP.h>
  38. #include <drm/drm_crtc.h>
  39. #include <drm/drm_fb_helper.h>
  40. #include "intel_drv.h"
  41. #include <drm/i915_drm.h>
  42. #include "i915_drv.h"
  43. static struct fb_ops intelfb_ops = {
  44. .owner = THIS_MODULE,
  45. .fb_check_var = drm_fb_helper_check_var,
  46. .fb_set_par = drm_fb_helper_set_par,
  47. .fb_fillrect = cfb_fillrect,
  48. .fb_copyarea = cfb_copyarea,
  49. .fb_imageblit = cfb_imageblit,
  50. .fb_pan_display = drm_fb_helper_pan_display,
  51. .fb_blank = drm_fb_helper_blank,
  52. .fb_setcmap = drm_fb_helper_setcmap,
  53. .fb_debug_enter = drm_fb_helper_debug_enter,
  54. .fb_debug_leave = drm_fb_helper_debug_leave,
  55. };
  56. static int intelfb_create(struct drm_fb_helper *helper,
  57. struct drm_fb_helper_surface_size *sizes)
  58. {
  59. struct intel_fbdev *ifbdev =
  60. container_of(helper, struct intel_fbdev, helper);
  61. struct drm_device *dev = helper->dev;
  62. struct drm_i915_private *dev_priv = dev->dev_private;
  63. struct fb_info *info;
  64. struct drm_framebuffer *fb;
  65. struct drm_mode_fb_cmd2 mode_cmd = {};
  66. struct drm_i915_gem_object *obj;
  67. struct device *device = &dev->pdev->dev;
  68. int size, ret;
  69. /* we don't do packed 24bpp */
  70. if (sizes->surface_bpp == 24)
  71. sizes->surface_bpp = 32;
  72. mode_cmd.width = sizes->surface_width;
  73. mode_cmd.height = sizes->surface_height;
  74. mode_cmd.pitches[0] = ALIGN(mode_cmd.width * ((sizes->surface_bpp + 7) /
  75. 8), 64);
  76. mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
  77. sizes->surface_depth);
  78. size = mode_cmd.pitches[0] * mode_cmd.height;
  79. size = ALIGN(size, PAGE_SIZE);
  80. obj = i915_gem_object_create_stolen(dev, size);
  81. if (obj == NULL)
  82. obj = i915_gem_alloc_object(dev, size);
  83. if (!obj) {
  84. DRM_ERROR("failed to allocate framebuffer\n");
  85. ret = -ENOMEM;
  86. goto out;
  87. }
  88. mutex_lock(&dev->struct_mutex);
  89. /* Flush everything out, we'll be doing GTT only from now on */
  90. ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
  91. if (ret) {
  92. DRM_ERROR("failed to pin fb: %d\n", ret);
  93. goto out_unref;
  94. }
  95. info = framebuffer_alloc(0, device);
  96. if (!info) {
  97. ret = -ENOMEM;
  98. goto out_unpin;
  99. }
  100. info->par = helper;
  101. ret = intel_framebuffer_init(dev, &ifbdev->ifb, &mode_cmd, obj);
  102. if (ret)
  103. goto out_unpin;
  104. fb = &ifbdev->ifb.base;
  105. ifbdev->helper.fb = fb;
  106. ifbdev->helper.fbdev = info;
  107. strcpy(info->fix.id, "inteldrmfb");
  108. info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT;
  109. info->fbops = &intelfb_ops;
  110. ret = fb_alloc_cmap(&info->cmap, 256, 0);
  111. if (ret) {
  112. ret = -ENOMEM;
  113. goto out_unpin;
  114. }
  115. /* setup aperture base/size for vesafb takeover */
  116. info->apertures = alloc_apertures(1);
  117. if (!info->apertures) {
  118. ret = -ENOMEM;
  119. goto out_unpin;
  120. }
  121. info->apertures->ranges[0].base = dev->mode_config.fb_base;
  122. info->apertures->ranges[0].size = dev_priv->gtt.mappable_end;
  123. info->fix.smem_start = dev->mode_config.fb_base + i915_gem_obj_ggtt_offset(obj);
  124. info->fix.smem_len = size;
  125. info->screen_base =
  126. ioremap_wc(dev_priv->gtt.mappable_base + i915_gem_obj_ggtt_offset(obj),
  127. size);
  128. if (!info->screen_base) {
  129. ret = -ENOSPC;
  130. goto out_unpin;
  131. }
  132. info->screen_size = size;
  133. /* This driver doesn't need a VT switch to restore the mode on resume */
  134. info->skip_vt_switch = true;
  135. drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
  136. drm_fb_helper_fill_var(info, &ifbdev->helper, sizes->fb_width, sizes->fb_height);
  137. /* If the object is shmemfs backed, it will have given us zeroed pages.
  138. * If the object is stolen however, it will be full of whatever
  139. * garbage was left in there.
  140. */
  141. if (ifbdev->ifb.obj->stolen)
  142. memset_io(info->screen_base, 0, info->screen_size);
  143. /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
  144. DRM_DEBUG_KMS("allocated %dx%d fb: 0x%08lx, bo %p\n",
  145. fb->width, fb->height,
  146. i915_gem_obj_ggtt_offset(obj), obj);
  147. mutex_unlock(&dev->struct_mutex);
  148. vga_switcheroo_client_fb_set(dev->pdev, info);
  149. return 0;
  150. out_unpin:
  151. i915_gem_object_unpin(obj);
  152. out_unref:
  153. drm_gem_object_unreference(&obj->base);
  154. mutex_unlock(&dev->struct_mutex);
  155. out:
  156. return ret;
  157. }
  158. /** Sets the color ramps on behalf of RandR */
  159. static void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  160. u16 blue, int regno)
  161. {
  162. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  163. intel_crtc->lut_r[regno] = red >> 8;
  164. intel_crtc->lut_g[regno] = green >> 8;
  165. intel_crtc->lut_b[regno] = blue >> 8;
  166. }
  167. static void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
  168. u16 *blue, int regno)
  169. {
  170. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  171. *red = intel_crtc->lut_r[regno] << 8;
  172. *green = intel_crtc->lut_g[regno] << 8;
  173. *blue = intel_crtc->lut_b[regno] << 8;
  174. }
  175. static struct drm_fb_helper_funcs intel_fb_helper_funcs = {
  176. .gamma_set = intel_crtc_fb_gamma_set,
  177. .gamma_get = intel_crtc_fb_gamma_get,
  178. .fb_probe = intelfb_create,
  179. };
  180. static void intel_fbdev_destroy(struct drm_device *dev,
  181. struct intel_fbdev *ifbdev)
  182. {
  183. if (ifbdev->helper.fbdev) {
  184. struct fb_info *info = ifbdev->helper.fbdev;
  185. unregister_framebuffer(info);
  186. iounmap(info->screen_base);
  187. if (info->cmap.len)
  188. fb_dealloc_cmap(&info->cmap);
  189. framebuffer_release(info);
  190. }
  191. drm_fb_helper_fini(&ifbdev->helper);
  192. drm_framebuffer_unregister_private(&ifbdev->ifb.base);
  193. intel_framebuffer_fini(&ifbdev->ifb);
  194. }
  195. int intel_fbdev_init(struct drm_device *dev)
  196. {
  197. struct intel_fbdev *ifbdev;
  198. struct drm_i915_private *dev_priv = dev->dev_private;
  199. int ret;
  200. ifbdev = kzalloc(sizeof(*ifbdev), GFP_KERNEL);
  201. if (!ifbdev)
  202. return -ENOMEM;
  203. dev_priv->fbdev = ifbdev;
  204. ifbdev->helper.funcs = &intel_fb_helper_funcs;
  205. ret = drm_fb_helper_init(dev, &ifbdev->helper,
  206. INTEL_INFO(dev)->num_pipes,
  207. 4);
  208. if (ret) {
  209. kfree(ifbdev);
  210. return ret;
  211. }
  212. drm_fb_helper_single_add_all_connectors(&ifbdev->helper);
  213. return 0;
  214. }
  215. void intel_fbdev_initial_config(struct drm_device *dev)
  216. {
  217. struct drm_i915_private *dev_priv = dev->dev_private;
  218. /* Due to peculiar init order wrt to hpd handling this is separate. */
  219. drm_fb_helper_initial_config(&dev_priv->fbdev->helper, 32);
  220. }
  221. void intel_fbdev_fini(struct drm_device *dev)
  222. {
  223. struct drm_i915_private *dev_priv = dev->dev_private;
  224. if (!dev_priv->fbdev)
  225. return;
  226. intel_fbdev_destroy(dev, dev_priv->fbdev);
  227. kfree(dev_priv->fbdev);
  228. dev_priv->fbdev = NULL;
  229. }
  230. void intel_fbdev_set_suspend(struct drm_device *dev, int state)
  231. {
  232. struct drm_i915_private *dev_priv = dev->dev_private;
  233. struct intel_fbdev *ifbdev = dev_priv->fbdev;
  234. struct fb_info *info;
  235. if (!ifbdev)
  236. return;
  237. info = ifbdev->helper.fbdev;
  238. /* On resume from hibernation: If the object is shmemfs backed, it has
  239. * been restored from swap. If the object is stolen however, it will be
  240. * full of whatever garbage was left in there.
  241. */
  242. if (state == FBINFO_STATE_RUNNING && ifbdev->ifb.obj->stolen)
  243. memset_io(info->screen_base, 0, info->screen_size);
  244. fb_set_suspend(info, state);
  245. }
  246. MODULE_LICENSE("GPL and additional rights");
  247. void intel_fbdev_output_poll_changed(struct drm_device *dev)
  248. {
  249. struct drm_i915_private *dev_priv = dev->dev_private;
  250. drm_fb_helper_hotplug_event(&dev_priv->fbdev->helper);
  251. }
  252. void intel_fbdev_restore_mode(struct drm_device *dev)
  253. {
  254. int ret;
  255. struct drm_i915_private *dev_priv = dev->dev_private;
  256. if (INTEL_INFO(dev)->num_pipes == 0)
  257. return;
  258. drm_modeset_lock_all(dev);
  259. ret = drm_fb_helper_restore_fbdev_mode(&dev_priv->fbdev->helper);
  260. if (ret)
  261. DRM_DEBUG("failed to restore crtc mode\n");
  262. drm_modeset_unlock_all(dev);
  263. }