intel_fbdev.c 9.4 KB

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