intel_fbdev.c 23 KB

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