intel_fbdev.c 9.5 KB

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