intel_fbdev.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  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/async.h>
  27. #include <linux/module.h>
  28. #include <linux/kernel.h>
  29. #include <linux/console.h>
  30. #include <linux/errno.h>
  31. #include <linux/string.h>
  32. #include <linux/mm.h>
  33. #include <linux/tty.h>
  34. #include <linux/sysrq.h>
  35. #include <linux/delay.h>
  36. #include <linux/fb.h>
  37. #include <linux/init.h>
  38. #include <linux/vga_switcheroo.h>
  39. #include <drm/drmP.h>
  40. #include <drm/drm_crtc.h>
  41. #include <drm/drm_fb_helper.h>
  42. #include "intel_drv.h"
  43. #include <drm/i915_drm.h>
  44. #include "i915_drv.h"
  45. static int intel_fbdev_set_par(struct fb_info *info)
  46. {
  47. struct drm_fb_helper *fb_helper = info->par;
  48. struct intel_fbdev *ifbdev =
  49. container_of(fb_helper, struct intel_fbdev, helper);
  50. int ret;
  51. ret = drm_fb_helper_set_par(info);
  52. if (ret == 0) {
  53. /*
  54. * FIXME: fbdev presumes that all callbacks also work from
  55. * atomic contexts and relies on that for emergency oops
  56. * printing. KMS totally doesn't do that and the locking here is
  57. * by far not the only place this goes wrong. Ignore this for
  58. * now until we solve this for real.
  59. */
  60. mutex_lock(&fb_helper->dev->struct_mutex);
  61. ret = i915_gem_object_set_to_gtt_domain(ifbdev->fb->obj,
  62. true);
  63. mutex_unlock(&fb_helper->dev->struct_mutex);
  64. }
  65. return ret;
  66. }
  67. static int intel_fbdev_blank(int blank, struct fb_info *info)
  68. {
  69. struct drm_fb_helper *fb_helper = info->par;
  70. struct intel_fbdev *ifbdev =
  71. container_of(fb_helper, struct intel_fbdev, helper);
  72. int ret;
  73. ret = drm_fb_helper_blank(blank, info);
  74. if (ret == 0) {
  75. /*
  76. * FIXME: fbdev presumes that all callbacks also work from
  77. * atomic contexts and relies on that for emergency oops
  78. * printing. KMS totally doesn't do that and the locking here is
  79. * by far not the only place this goes wrong. Ignore this for
  80. * now until we solve this for real.
  81. */
  82. mutex_lock(&fb_helper->dev->struct_mutex);
  83. intel_fb_obj_invalidate(ifbdev->fb->obj, NULL, ORIGIN_GTT);
  84. mutex_unlock(&fb_helper->dev->struct_mutex);
  85. }
  86. return ret;
  87. }
  88. static struct fb_ops intelfb_ops = {
  89. .owner = THIS_MODULE,
  90. .fb_check_var = drm_fb_helper_check_var,
  91. .fb_set_par = intel_fbdev_set_par,
  92. .fb_fillrect = cfb_fillrect,
  93. .fb_copyarea = cfb_copyarea,
  94. .fb_imageblit = cfb_imageblit,
  95. .fb_pan_display = drm_fb_helper_pan_display,
  96. .fb_blank = intel_fbdev_blank,
  97. .fb_setcmap = drm_fb_helper_setcmap,
  98. .fb_debug_enter = drm_fb_helper_debug_enter,
  99. .fb_debug_leave = drm_fb_helper_debug_leave,
  100. };
  101. static int intelfb_alloc(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 drm_framebuffer *fb;
  107. struct drm_device *dev = helper->dev;
  108. struct drm_mode_fb_cmd2 mode_cmd = {};
  109. struct drm_i915_gem_object *obj;
  110. int size, ret;
  111. /* we don't do packed 24bpp */
  112. if (sizes->surface_bpp == 24)
  113. sizes->surface_bpp = 32;
  114. mode_cmd.width = sizes->surface_width;
  115. mode_cmd.height = sizes->surface_height;
  116. mode_cmd.pitches[0] = ALIGN(mode_cmd.width *
  117. DIV_ROUND_UP(sizes->surface_bpp, 8), 64);
  118. mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
  119. sizes->surface_depth);
  120. size = mode_cmd.pitches[0] * mode_cmd.height;
  121. size = PAGE_ALIGN(size);
  122. obj = i915_gem_object_create_stolen(dev, size);
  123. if (obj == NULL)
  124. obj = i915_gem_alloc_object(dev, size);
  125. if (!obj) {
  126. DRM_ERROR("failed to allocate framebuffer\n");
  127. ret = -ENOMEM;
  128. goto out;
  129. }
  130. fb = __intel_framebuffer_create(dev, &mode_cmd, obj);
  131. if (IS_ERR(fb)) {
  132. ret = PTR_ERR(fb);
  133. goto out_unref;
  134. }
  135. /* Flush everything out, we'll be doing GTT only from now on */
  136. ret = intel_pin_and_fence_fb_obj(NULL, fb, NULL, NULL);
  137. if (ret) {
  138. DRM_ERROR("failed to pin obj: %d\n", ret);
  139. goto out_fb;
  140. }
  141. ifbdev->fb = to_intel_framebuffer(fb);
  142. return 0;
  143. out_fb:
  144. drm_framebuffer_remove(fb);
  145. out_unref:
  146. drm_gem_object_unreference(&obj->base);
  147. out:
  148. return ret;
  149. }
  150. static int intelfb_create(struct drm_fb_helper *helper,
  151. struct drm_fb_helper_surface_size *sizes)
  152. {
  153. struct intel_fbdev *ifbdev =
  154. container_of(helper, struct intel_fbdev, helper);
  155. struct intel_framebuffer *intel_fb = ifbdev->fb;
  156. struct drm_device *dev = helper->dev;
  157. struct drm_i915_private *dev_priv = dev->dev_private;
  158. struct fb_info *info;
  159. struct drm_framebuffer *fb;
  160. struct drm_i915_gem_object *obj;
  161. int size, ret;
  162. bool prealloc = false;
  163. mutex_lock(&dev->struct_mutex);
  164. if (intel_fb &&
  165. (sizes->fb_width > intel_fb->base.width ||
  166. sizes->fb_height > intel_fb->base.height)) {
  167. DRM_DEBUG_KMS("BIOS fb too small (%dx%d), we require (%dx%d),"
  168. " releasing it\n",
  169. intel_fb->base.width, intel_fb->base.height,
  170. sizes->fb_width, sizes->fb_height);
  171. drm_framebuffer_unreference(&intel_fb->base);
  172. intel_fb = ifbdev->fb = NULL;
  173. }
  174. if (!intel_fb || WARN_ON(!intel_fb->obj)) {
  175. DRM_DEBUG_KMS("no BIOS fb, allocating a new one\n");
  176. ret = intelfb_alloc(helper, sizes);
  177. if (ret)
  178. goto out_unlock;
  179. intel_fb = ifbdev->fb;
  180. } else {
  181. DRM_DEBUG_KMS("re-using BIOS fb\n");
  182. prealloc = true;
  183. sizes->fb_width = intel_fb->base.width;
  184. sizes->fb_height = intel_fb->base.height;
  185. }
  186. obj = intel_fb->obj;
  187. size = obj->base.size;
  188. info = framebuffer_alloc(0, &dev->pdev->dev);
  189. if (!info) {
  190. ret = -ENOMEM;
  191. goto out_unpin;
  192. }
  193. info->par = helper;
  194. fb = &ifbdev->fb->base;
  195. ifbdev->helper.fb = fb;
  196. ifbdev->helper.fbdev = info;
  197. strcpy(info->fix.id, "inteldrmfb");
  198. info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT;
  199. info->fbops = &intelfb_ops;
  200. ret = fb_alloc_cmap(&info->cmap, 256, 0);
  201. if (ret) {
  202. ret = -ENOMEM;
  203. goto out_unpin;
  204. }
  205. /* setup aperture base/size for vesafb takeover */
  206. info->apertures = alloc_apertures(1);
  207. if (!info->apertures) {
  208. ret = -ENOMEM;
  209. goto out_unpin;
  210. }
  211. info->apertures->ranges[0].base = dev->mode_config.fb_base;
  212. info->apertures->ranges[0].size = dev_priv->gtt.mappable_end;
  213. info->fix.smem_start = dev->mode_config.fb_base + i915_gem_obj_ggtt_offset(obj);
  214. info->fix.smem_len = size;
  215. info->screen_base =
  216. ioremap_wc(dev_priv->gtt.mappable_base + i915_gem_obj_ggtt_offset(obj),
  217. size);
  218. if (!info->screen_base) {
  219. ret = -ENOSPC;
  220. goto out_unpin;
  221. }
  222. info->screen_size = size;
  223. /* This driver doesn't need a VT switch to restore the mode on resume */
  224. info->skip_vt_switch = true;
  225. drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
  226. drm_fb_helper_fill_var(info, &ifbdev->helper, sizes->fb_width, sizes->fb_height);
  227. /* If the object is shmemfs backed, it will have given us zeroed pages.
  228. * If the object is stolen however, it will be full of whatever
  229. * garbage was left in there.
  230. */
  231. if (ifbdev->fb->obj->stolen && !prealloc)
  232. memset_io(info->screen_base, 0, info->screen_size);
  233. /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
  234. DRM_DEBUG_KMS("allocated %dx%d fb: 0x%08lx, bo %p\n",
  235. fb->width, fb->height,
  236. i915_gem_obj_ggtt_offset(obj), obj);
  237. mutex_unlock(&dev->struct_mutex);
  238. vga_switcheroo_client_fb_set(dev->pdev, info);
  239. return 0;
  240. out_unpin:
  241. i915_gem_object_ggtt_unpin(obj);
  242. drm_gem_object_unreference(&obj->base);
  243. out_unlock:
  244. mutex_unlock(&dev->struct_mutex);
  245. return ret;
  246. }
  247. /** Sets the color ramps on behalf of RandR */
  248. static void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  249. u16 blue, int regno)
  250. {
  251. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  252. intel_crtc->lut_r[regno] = red >> 8;
  253. intel_crtc->lut_g[regno] = green >> 8;
  254. intel_crtc->lut_b[regno] = blue >> 8;
  255. }
  256. static void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
  257. u16 *blue, int regno)
  258. {
  259. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  260. *red = intel_crtc->lut_r[regno] << 8;
  261. *green = intel_crtc->lut_g[regno] << 8;
  262. *blue = intel_crtc->lut_b[regno] << 8;
  263. }
  264. static struct drm_fb_helper_crtc *
  265. intel_fb_helper_crtc(struct drm_fb_helper *fb_helper, struct drm_crtc *crtc)
  266. {
  267. int i;
  268. for (i = 0; i < fb_helper->crtc_count; i++)
  269. if (fb_helper->crtc_info[i].mode_set.crtc == crtc)
  270. return &fb_helper->crtc_info[i];
  271. return NULL;
  272. }
  273. /*
  274. * Try to read the BIOS display configuration and use it for the initial
  275. * fb configuration.
  276. *
  277. * The BIOS or boot loader will generally create an initial display
  278. * configuration for us that includes some set of active pipes and displays.
  279. * This routine tries to figure out which pipes and connectors are active
  280. * and stuffs them into the crtcs and modes array given to us by the
  281. * drm_fb_helper code.
  282. *
  283. * The overall sequence is:
  284. * intel_fbdev_init - from driver load
  285. * intel_fbdev_init_bios - initialize the intel_fbdev using BIOS data
  286. * drm_fb_helper_init - build fb helper structs
  287. * drm_fb_helper_single_add_all_connectors - more fb helper structs
  288. * intel_fbdev_initial_config - apply the config
  289. * drm_fb_helper_initial_config - call ->probe then register_framebuffer()
  290. * drm_setup_crtcs - build crtc config for fbdev
  291. * intel_fb_initial_config - find active connectors etc
  292. * drm_fb_helper_single_fb_probe - set up fbdev
  293. * intelfb_create - re-use or alloc fb, build out fbdev structs
  294. *
  295. * Note that we don't make special consideration whether we could actually
  296. * switch to the selected modes without a full modeset. E.g. when the display
  297. * is in VGA mode we need to recalculate watermarks and set a new high-res
  298. * framebuffer anyway.
  299. */
  300. static bool intel_fb_initial_config(struct drm_fb_helper *fb_helper,
  301. struct drm_fb_helper_crtc **crtcs,
  302. struct drm_display_mode **modes,
  303. struct drm_fb_offset *offsets,
  304. bool *enabled, int width, int height)
  305. {
  306. struct drm_device *dev = fb_helper->dev;
  307. int i, j;
  308. bool *save_enabled;
  309. bool fallback = true;
  310. int num_connectors_enabled = 0;
  311. int num_connectors_detected = 0;
  312. uint64_t conn_configured = 0, mask;
  313. int pass = 0;
  314. save_enabled = kcalloc(dev->mode_config.num_connector, sizeof(bool),
  315. GFP_KERNEL);
  316. if (!save_enabled)
  317. return false;
  318. memcpy(save_enabled, enabled, dev->mode_config.num_connector);
  319. mask = (1 << fb_helper->connector_count) - 1;
  320. retry:
  321. for (i = 0; i < fb_helper->connector_count; i++) {
  322. struct drm_fb_helper_connector *fb_conn;
  323. struct drm_connector *connector;
  324. struct drm_encoder *encoder;
  325. struct drm_fb_helper_crtc *new_crtc;
  326. fb_conn = fb_helper->connector_info[i];
  327. connector = fb_conn->connector;
  328. if (conn_configured & (1 << i))
  329. continue;
  330. if (pass == 0 && !connector->has_tile)
  331. continue;
  332. if (connector->status == connector_status_connected)
  333. num_connectors_detected++;
  334. if (!enabled[i]) {
  335. DRM_DEBUG_KMS("connector %s not enabled, skipping\n",
  336. connector->name);
  337. conn_configured |= (1 << i);
  338. continue;
  339. }
  340. if (connector->force == DRM_FORCE_OFF) {
  341. DRM_DEBUG_KMS("connector %s is disabled by user, skipping\n",
  342. connector->name);
  343. enabled[i] = false;
  344. continue;
  345. }
  346. encoder = connector->encoder;
  347. if (!encoder || WARN_ON(!encoder->crtc)) {
  348. if (connector->force > DRM_FORCE_OFF)
  349. goto bail;
  350. DRM_DEBUG_KMS("connector %s has no encoder or crtc, skipping\n",
  351. connector->name);
  352. enabled[i] = false;
  353. conn_configured |= (1 << i);
  354. continue;
  355. }
  356. num_connectors_enabled++;
  357. new_crtc = intel_fb_helper_crtc(fb_helper, encoder->crtc);
  358. /*
  359. * Make sure we're not trying to drive multiple connectors
  360. * with a single CRTC, since our cloning support may not
  361. * match the BIOS.
  362. */
  363. for (j = 0; j < fb_helper->connector_count; j++) {
  364. if (crtcs[j] == new_crtc) {
  365. DRM_DEBUG_KMS("fallback: cloned configuration\n");
  366. goto bail;
  367. }
  368. }
  369. DRM_DEBUG_KMS("looking for cmdline mode on connector %s\n",
  370. connector->name);
  371. /* go for command line mode first */
  372. modes[i] = drm_pick_cmdline_mode(fb_conn, width, height);
  373. /* try for preferred next */
  374. if (!modes[i]) {
  375. DRM_DEBUG_KMS("looking for preferred mode on connector %s %d\n",
  376. connector->name, connector->has_tile);
  377. modes[i] = drm_has_preferred_mode(fb_conn, width,
  378. height);
  379. }
  380. /* No preferred mode marked by the EDID? Are there any modes? */
  381. if (!modes[i] && !list_empty(&connector->modes)) {
  382. DRM_DEBUG_KMS("using first mode listed on connector %s\n",
  383. connector->name);
  384. modes[i] = list_first_entry(&connector->modes,
  385. struct drm_display_mode,
  386. head);
  387. }
  388. /* last resort: use current mode */
  389. if (!modes[i]) {
  390. /*
  391. * IMPORTANT: We want to use the adjusted mode (i.e.
  392. * after the panel fitter upscaling) as the initial
  393. * config, not the input mode, which is what crtc->mode
  394. * usually contains. But since our current fastboot
  395. * code puts a mode derived from the post-pfit timings
  396. * into crtc->mode this works out correctly. We don't
  397. * use hwmode anywhere right now, so use it for this
  398. * since the fb helper layer wants a pointer to
  399. * something we own.
  400. */
  401. DRM_DEBUG_KMS("looking for current mode on connector %s\n",
  402. connector->name);
  403. intel_mode_from_pipe_config(&encoder->crtc->hwmode,
  404. to_intel_crtc(encoder->crtc)->config);
  405. modes[i] = &encoder->crtc->hwmode;
  406. }
  407. crtcs[i] = new_crtc;
  408. DRM_DEBUG_KMS("connector %s on pipe %c [CRTC:%d]: %dx%d%s\n",
  409. connector->name,
  410. pipe_name(to_intel_crtc(encoder->crtc)->pipe),
  411. encoder->crtc->base.id,
  412. modes[i]->hdisplay, modes[i]->vdisplay,
  413. modes[i]->flags & DRM_MODE_FLAG_INTERLACE ? "i" :"");
  414. fallback = false;
  415. conn_configured |= (1 << i);
  416. }
  417. if ((conn_configured & mask) != mask) {
  418. pass++;
  419. goto retry;
  420. }
  421. /*
  422. * If the BIOS didn't enable everything it could, fall back to have the
  423. * same user experiencing of lighting up as much as possible like the
  424. * fbdev helper library.
  425. */
  426. if (num_connectors_enabled != num_connectors_detected &&
  427. num_connectors_enabled < INTEL_INFO(dev)->num_pipes) {
  428. DRM_DEBUG_KMS("fallback: Not all outputs enabled\n");
  429. DRM_DEBUG_KMS("Enabled: %i, detected: %i\n", num_connectors_enabled,
  430. num_connectors_detected);
  431. fallback = true;
  432. }
  433. if (fallback) {
  434. bail:
  435. DRM_DEBUG_KMS("Not using firmware configuration\n");
  436. memcpy(enabled, save_enabled, dev->mode_config.num_connector);
  437. kfree(save_enabled);
  438. return false;
  439. }
  440. kfree(save_enabled);
  441. return true;
  442. }
  443. static const struct drm_fb_helper_funcs intel_fb_helper_funcs = {
  444. .initial_config = intel_fb_initial_config,
  445. .gamma_set = intel_crtc_fb_gamma_set,
  446. .gamma_get = intel_crtc_fb_gamma_get,
  447. .fb_probe = intelfb_create,
  448. };
  449. static void intel_fbdev_destroy(struct drm_device *dev,
  450. struct intel_fbdev *ifbdev)
  451. {
  452. if (ifbdev->helper.fbdev) {
  453. struct fb_info *info = ifbdev->helper.fbdev;
  454. unregister_framebuffer(info);
  455. iounmap(info->screen_base);
  456. if (info->cmap.len)
  457. fb_dealloc_cmap(&info->cmap);
  458. framebuffer_release(info);
  459. }
  460. drm_fb_helper_fini(&ifbdev->helper);
  461. drm_framebuffer_unregister_private(&ifbdev->fb->base);
  462. drm_framebuffer_remove(&ifbdev->fb->base);
  463. }
  464. /*
  465. * Build an intel_fbdev struct using a BIOS allocated framebuffer, if possible.
  466. * The core display code will have read out the current plane configuration,
  467. * so we use that to figure out if there's an object for us to use as the
  468. * fb, and if so, we re-use it for the fbdev configuration.
  469. *
  470. * Note we only support a single fb shared across pipes for boot (mostly for
  471. * fbcon), so we just find the biggest and use that.
  472. */
  473. static bool intel_fbdev_init_bios(struct drm_device *dev,
  474. struct intel_fbdev *ifbdev)
  475. {
  476. struct intel_framebuffer *fb = NULL;
  477. struct drm_crtc *crtc;
  478. struct intel_crtc *intel_crtc;
  479. struct intel_initial_plane_config *plane_config = NULL;
  480. unsigned int max_size = 0;
  481. if (!i915.fastboot)
  482. return false;
  483. /* Find the largest fb */
  484. for_each_crtc(dev, crtc) {
  485. intel_crtc = to_intel_crtc(crtc);
  486. if (!intel_crtc->active || !crtc->primary->fb) {
  487. DRM_DEBUG_KMS("pipe %c not active or no fb, skipping\n",
  488. pipe_name(intel_crtc->pipe));
  489. continue;
  490. }
  491. if (intel_crtc->plane_config.size > max_size) {
  492. DRM_DEBUG_KMS("found possible fb from plane %c\n",
  493. pipe_name(intel_crtc->pipe));
  494. plane_config = &intel_crtc->plane_config;
  495. fb = to_intel_framebuffer(crtc->primary->fb);
  496. max_size = plane_config->size;
  497. }
  498. }
  499. if (!fb) {
  500. DRM_DEBUG_KMS("no active fbs found, not using BIOS config\n");
  501. goto out;
  502. }
  503. /* Now make sure all the pipes will fit into it */
  504. for_each_crtc(dev, crtc) {
  505. unsigned int cur_size;
  506. intel_crtc = to_intel_crtc(crtc);
  507. if (!intel_crtc->active) {
  508. DRM_DEBUG_KMS("pipe %c not active, skipping\n",
  509. pipe_name(intel_crtc->pipe));
  510. continue;
  511. }
  512. DRM_DEBUG_KMS("checking plane %c for BIOS fb\n",
  513. pipe_name(intel_crtc->pipe));
  514. /*
  515. * See if the plane fb we found above will fit on this
  516. * pipe. Note we need to use the selected fb's pitch and bpp
  517. * rather than the current pipe's, since they differ.
  518. */
  519. cur_size = intel_crtc->config->base.adjusted_mode.crtc_hdisplay;
  520. cur_size = cur_size * fb->base.bits_per_pixel / 8;
  521. if (fb->base.pitches[0] < cur_size) {
  522. DRM_DEBUG_KMS("fb not wide enough for plane %c (%d vs %d)\n",
  523. pipe_name(intel_crtc->pipe),
  524. cur_size, fb->base.pitches[0]);
  525. plane_config = NULL;
  526. fb = NULL;
  527. break;
  528. }
  529. cur_size = intel_crtc->config->base.adjusted_mode.crtc_vdisplay;
  530. cur_size = intel_fb_align_height(dev, cur_size,
  531. fb->base.pixel_format,
  532. fb->base.modifier[0]);
  533. cur_size *= fb->base.pitches[0];
  534. DRM_DEBUG_KMS("pipe %c area: %dx%d, bpp: %d, size: %d\n",
  535. pipe_name(intel_crtc->pipe),
  536. intel_crtc->config->base.adjusted_mode.crtc_hdisplay,
  537. intel_crtc->config->base.adjusted_mode.crtc_vdisplay,
  538. fb->base.bits_per_pixel,
  539. cur_size);
  540. if (cur_size > max_size) {
  541. DRM_DEBUG_KMS("fb not big enough for plane %c (%d vs %d)\n",
  542. pipe_name(intel_crtc->pipe),
  543. cur_size, max_size);
  544. plane_config = NULL;
  545. fb = NULL;
  546. break;
  547. }
  548. DRM_DEBUG_KMS("fb big enough for plane %c (%d >= %d)\n",
  549. pipe_name(intel_crtc->pipe),
  550. max_size, cur_size);
  551. }
  552. if (!fb) {
  553. DRM_DEBUG_KMS("BIOS fb not suitable for all pipes, not using\n");
  554. goto out;
  555. }
  556. ifbdev->preferred_bpp = fb->base.bits_per_pixel;
  557. ifbdev->fb = fb;
  558. drm_framebuffer_reference(&ifbdev->fb->base);
  559. /* Final pass to check if any active pipes don't have fbs */
  560. for_each_crtc(dev, crtc) {
  561. intel_crtc = to_intel_crtc(crtc);
  562. if (!intel_crtc->active)
  563. continue;
  564. WARN(!crtc->primary->fb,
  565. "re-used BIOS config but lost an fb on crtc %d\n",
  566. crtc->base.id);
  567. }
  568. DRM_DEBUG_KMS("using BIOS fb for initial console\n");
  569. return true;
  570. out:
  571. return false;
  572. }
  573. static void intel_fbdev_suspend_worker(struct work_struct *work)
  574. {
  575. intel_fbdev_set_suspend(container_of(work,
  576. struct drm_i915_private,
  577. fbdev_suspend_work)->dev,
  578. FBINFO_STATE_RUNNING,
  579. true);
  580. }
  581. int intel_fbdev_init(struct drm_device *dev)
  582. {
  583. struct intel_fbdev *ifbdev;
  584. struct drm_i915_private *dev_priv = dev->dev_private;
  585. int ret;
  586. if (WARN_ON(INTEL_INFO(dev)->num_pipes == 0))
  587. return -ENODEV;
  588. ifbdev = kzalloc(sizeof(struct intel_fbdev), GFP_KERNEL);
  589. if (ifbdev == NULL)
  590. return -ENOMEM;
  591. drm_fb_helper_prepare(dev, &ifbdev->helper, &intel_fb_helper_funcs);
  592. if (!intel_fbdev_init_bios(dev, ifbdev))
  593. ifbdev->preferred_bpp = 32;
  594. ret = drm_fb_helper_init(dev, &ifbdev->helper,
  595. INTEL_INFO(dev)->num_pipes, 4);
  596. if (ret) {
  597. kfree(ifbdev);
  598. return ret;
  599. }
  600. dev_priv->fbdev = ifbdev;
  601. INIT_WORK(&dev_priv->fbdev_suspend_work, intel_fbdev_suspend_worker);
  602. drm_fb_helper_single_add_all_connectors(&ifbdev->helper);
  603. return 0;
  604. }
  605. void intel_fbdev_initial_config(void *data, async_cookie_t cookie)
  606. {
  607. struct drm_i915_private *dev_priv = data;
  608. struct intel_fbdev *ifbdev = dev_priv->fbdev;
  609. /* Due to peculiar init order wrt to hpd handling this is separate. */
  610. drm_fb_helper_initial_config(&ifbdev->helper, ifbdev->preferred_bpp);
  611. }
  612. void intel_fbdev_fini(struct drm_device *dev)
  613. {
  614. struct drm_i915_private *dev_priv = dev->dev_private;
  615. if (!dev_priv->fbdev)
  616. return;
  617. flush_work(&dev_priv->fbdev_suspend_work);
  618. async_synchronize_full();
  619. intel_fbdev_destroy(dev, dev_priv->fbdev);
  620. kfree(dev_priv->fbdev);
  621. dev_priv->fbdev = NULL;
  622. }
  623. void intel_fbdev_set_suspend(struct drm_device *dev, int state, bool synchronous)
  624. {
  625. struct drm_i915_private *dev_priv = dev->dev_private;
  626. struct intel_fbdev *ifbdev = dev_priv->fbdev;
  627. struct fb_info *info;
  628. if (!ifbdev)
  629. return;
  630. info = ifbdev->helper.fbdev;
  631. if (synchronous) {
  632. /* Flush any pending work to turn the console on, and then
  633. * wait to turn it off. It must be synchronous as we are
  634. * about to suspend or unload the driver.
  635. *
  636. * Note that from within the work-handler, we cannot flush
  637. * ourselves, so only flush outstanding work upon suspend!
  638. */
  639. if (state != FBINFO_STATE_RUNNING)
  640. flush_work(&dev_priv->fbdev_suspend_work);
  641. console_lock();
  642. } else {
  643. /*
  644. * The console lock can be pretty contented on resume due
  645. * to all the printk activity. Try to keep it out of the hot
  646. * path of resume if possible.
  647. */
  648. WARN_ON(state != FBINFO_STATE_RUNNING);
  649. if (!console_trylock()) {
  650. /* Don't block our own workqueue as this can
  651. * be run in parallel with other i915.ko tasks.
  652. */
  653. schedule_work(&dev_priv->fbdev_suspend_work);
  654. return;
  655. }
  656. }
  657. /* On resume from hibernation: If the object is shmemfs backed, it has
  658. * been restored from swap. If the object is stolen however, it will be
  659. * full of whatever garbage was left in there.
  660. */
  661. if (state == FBINFO_STATE_RUNNING && ifbdev->fb->obj->stolen)
  662. memset_io(info->screen_base, 0, info->screen_size);
  663. fb_set_suspend(info, state);
  664. console_unlock();
  665. }
  666. void intel_fbdev_output_poll_changed(struct drm_device *dev)
  667. {
  668. struct drm_i915_private *dev_priv = dev->dev_private;
  669. if (dev_priv->fbdev)
  670. drm_fb_helper_hotplug_event(&dev_priv->fbdev->helper);
  671. }
  672. void intel_fbdev_restore_mode(struct drm_device *dev)
  673. {
  674. int ret;
  675. struct drm_i915_private *dev_priv = dev->dev_private;
  676. if (!dev_priv->fbdev)
  677. return;
  678. ret = drm_fb_helper_restore_fbdev_mode_unlocked(&dev_priv->fbdev->helper);
  679. if (ret)
  680. DRM_DEBUG("failed to restore crtc mode\n");
  681. }