intel_fbdev.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  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/init.h>
  37. #include <linux/vga_switcheroo.h>
  38. #include <drm/drmP.h>
  39. #include <drm/drm_crtc.h>
  40. #include <drm/drm_fb_helper.h>
  41. #include "intel_drv.h"
  42. #include "intel_frontbuffer.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. DRM_FB_HELPER_DEFAULT_OPS,
  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. };
  98. static int intelfb_alloc(struct drm_fb_helper *helper,
  99. struct drm_fb_helper_surface_size *sizes)
  100. {
  101. struct intel_fbdev *ifbdev =
  102. container_of(helper, struct intel_fbdev, helper);
  103. struct drm_framebuffer *fb;
  104. struct drm_device *dev = helper->dev;
  105. struct drm_i915_private *dev_priv = to_i915(dev);
  106. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  107. struct drm_mode_fb_cmd2 mode_cmd = {};
  108. struct drm_i915_gem_object *obj = NULL;
  109. int size, ret;
  110. /* we don't do packed 24bpp */
  111. if (sizes->surface_bpp == 24)
  112. sizes->surface_bpp = 32;
  113. mode_cmd.width = sizes->surface_width;
  114. mode_cmd.height = sizes->surface_height;
  115. mode_cmd.pitches[0] = ALIGN(mode_cmd.width *
  116. DIV_ROUND_UP(sizes->surface_bpp, 8), 64);
  117. mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
  118. sizes->surface_depth);
  119. mutex_lock(&dev->struct_mutex);
  120. size = mode_cmd.pitches[0] * mode_cmd.height;
  121. size = PAGE_ALIGN(size);
  122. /* If the FB is too big, just don't use it since fbdev is not very
  123. * important and we should probably use that space with FBC or other
  124. * features. */
  125. if (size * 2 < ggtt->stolen_usable_size)
  126. obj = i915_gem_object_create_stolen(dev_priv, size);
  127. if (obj == NULL)
  128. obj = i915_gem_object_create(dev_priv, size);
  129. if (IS_ERR(obj)) {
  130. DRM_ERROR("failed to allocate framebuffer\n");
  131. ret = PTR_ERR(obj);
  132. goto out;
  133. }
  134. fb = __intel_framebuffer_create(dev, &mode_cmd, obj);
  135. if (IS_ERR(fb)) {
  136. i915_gem_object_put(obj);
  137. ret = PTR_ERR(fb);
  138. goto out;
  139. }
  140. mutex_unlock(&dev->struct_mutex);
  141. ifbdev->fb = to_intel_framebuffer(fb);
  142. return 0;
  143. out:
  144. mutex_unlock(&dev->struct_mutex);
  145. return ret;
  146. }
  147. static int intelfb_create(struct drm_fb_helper *helper,
  148. struct drm_fb_helper_surface_size *sizes)
  149. {
  150. struct intel_fbdev *ifbdev =
  151. container_of(helper, struct intel_fbdev, helper);
  152. struct intel_framebuffer *intel_fb = ifbdev->fb;
  153. struct drm_device *dev = helper->dev;
  154. struct drm_i915_private *dev_priv = to_i915(dev);
  155. struct pci_dev *pdev = dev_priv->drm.pdev;
  156. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  157. struct fb_info *info;
  158. struct drm_framebuffer *fb;
  159. struct i915_vma *vma;
  160. bool prealloc = false;
  161. void __iomem *vaddr;
  162. int ret;
  163. if (intel_fb &&
  164. (sizes->fb_width > intel_fb->base.width ||
  165. sizes->fb_height > intel_fb->base.height)) {
  166. DRM_DEBUG_KMS("BIOS fb too small (%dx%d), we require (%dx%d),"
  167. " releasing it\n",
  168. intel_fb->base.width, intel_fb->base.height,
  169. sizes->fb_width, sizes->fb_height);
  170. drm_framebuffer_unreference(&intel_fb->base);
  171. intel_fb = ifbdev->fb = NULL;
  172. }
  173. if (!intel_fb || WARN_ON(!intel_fb->obj)) {
  174. DRM_DEBUG_KMS("no BIOS fb, allocating a new one\n");
  175. ret = intelfb_alloc(helper, sizes);
  176. if (ret)
  177. return ret;
  178. intel_fb = ifbdev->fb;
  179. } else {
  180. DRM_DEBUG_KMS("re-using BIOS fb\n");
  181. prealloc = true;
  182. sizes->fb_width = intel_fb->base.width;
  183. sizes->fb_height = intel_fb->base.height;
  184. }
  185. mutex_lock(&dev->struct_mutex);
  186. /* Pin the GGTT vma for our access via info->screen_base.
  187. * This also validates that any existing fb inherited from the
  188. * BIOS is suitable for own access.
  189. */
  190. vma = intel_pin_and_fence_fb_obj(&ifbdev->fb->base, DRM_ROTATE_0);
  191. if (IS_ERR(vma)) {
  192. ret = PTR_ERR(vma);
  193. goto out_unlock;
  194. }
  195. info = drm_fb_helper_alloc_fbi(helper);
  196. if (IS_ERR(info)) {
  197. DRM_ERROR("Failed to allocate fb_info\n");
  198. ret = PTR_ERR(info);
  199. goto out_unpin;
  200. }
  201. info->par = helper;
  202. fb = &ifbdev->fb->base;
  203. ifbdev->helper.fb = fb;
  204. strcpy(info->fix.id, "inteldrmfb");
  205. info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT;
  206. info->fbops = &intelfb_ops;
  207. /* setup aperture base/size for vesafb takeover */
  208. info->apertures->ranges[0].base = dev->mode_config.fb_base;
  209. info->apertures->ranges[0].size = ggtt->mappable_end;
  210. info->fix.smem_start = dev->mode_config.fb_base + i915_ggtt_offset(vma);
  211. info->fix.smem_len = vma->node.size;
  212. vaddr = i915_vma_pin_iomap(vma);
  213. if (IS_ERR(vaddr)) {
  214. DRM_ERROR("Failed to remap framebuffer into virtual memory\n");
  215. ret = PTR_ERR(vaddr);
  216. goto out_destroy_fbi;
  217. }
  218. info->screen_base = vaddr;
  219. info->screen_size = vma->node.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->format->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 (intel_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%08x\n",
  232. fb->width, fb->height, i915_ggtt_offset(vma));
  233. ifbdev->vma = vma;
  234. mutex_unlock(&dev->struct_mutex);
  235. vga_switcheroo_client_fb_set(pdev, info);
  236. return 0;
  237. out_destroy_fbi:
  238. drm_fb_helper_release_fbi(helper);
  239. out_unpin:
  240. intel_unpin_fb_vma(vma);
  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_i915_private *dev_priv = to_i915(fb_helper->dev);
  305. unsigned long conn_configured, mask;
  306. unsigned int count = min(fb_helper->connector_count, BITS_PER_LONG);
  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. int pass = 0;
  313. save_enabled = kcalloc(count, sizeof(bool), GFP_KERNEL);
  314. if (!save_enabled)
  315. return false;
  316. memcpy(save_enabled, enabled, count);
  317. mask = BIT(count) - 1;
  318. conn_configured = 0;
  319. retry:
  320. for (i = 0; i < count; i++) {
  321. struct drm_fb_helper_connector *fb_conn;
  322. struct drm_connector *connector;
  323. struct drm_encoder *encoder;
  324. struct drm_fb_helper_crtc *new_crtc;
  325. struct intel_crtc *intel_crtc;
  326. fb_conn = fb_helper->connector_info[i];
  327. connector = fb_conn->connector;
  328. if (conn_configured & BIT(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 |= BIT(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->state->best_encoder;
  347. if (!encoder || WARN_ON(!connector->state->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 |= BIT(i);
  354. continue;
  355. }
  356. num_connectors_enabled++;
  357. intel_crtc = to_intel_crtc(connector->state->crtc);
  358. for (j = 0; j < 256; j++) {
  359. intel_crtc->lut_r[j] = j;
  360. intel_crtc->lut_g[j] = j;
  361. intel_crtc->lut_b[j] = j;
  362. }
  363. new_crtc = intel_fb_helper_crtc(fb_helper,
  364. connector->state->crtc);
  365. /*
  366. * Make sure we're not trying to drive multiple connectors
  367. * with a single CRTC, since our cloning support may not
  368. * match the BIOS.
  369. */
  370. for (j = 0; j < count; j++) {
  371. if (crtcs[j] == new_crtc) {
  372. DRM_DEBUG_KMS("fallback: cloned configuration\n");
  373. goto bail;
  374. }
  375. }
  376. DRM_DEBUG_KMS("looking for cmdline mode on connector %s\n",
  377. connector->name);
  378. /* go for command line mode first */
  379. modes[i] = drm_pick_cmdline_mode(fb_conn);
  380. /* try for preferred next */
  381. if (!modes[i]) {
  382. DRM_DEBUG_KMS("looking for preferred mode on connector %s %d\n",
  383. connector->name, connector->has_tile);
  384. modes[i] = drm_has_preferred_mode(fb_conn, width,
  385. height);
  386. }
  387. /* No preferred mode marked by the EDID? Are there any modes? */
  388. if (!modes[i] && !list_empty(&connector->modes)) {
  389. DRM_DEBUG_KMS("using first mode listed on connector %s\n",
  390. connector->name);
  391. modes[i] = list_first_entry(&connector->modes,
  392. struct drm_display_mode,
  393. head);
  394. }
  395. /* last resort: use current mode */
  396. if (!modes[i]) {
  397. /*
  398. * IMPORTANT: We want to use the adjusted mode (i.e.
  399. * after the panel fitter upscaling) as the initial
  400. * config, not the input mode, which is what crtc->mode
  401. * usually contains. But since our current
  402. * code puts a mode derived from the post-pfit timings
  403. * into crtc->mode this works out correctly.
  404. *
  405. * This is crtc->mode and not crtc->state->mode for the
  406. * fastboot check to work correctly. crtc_state->mode has
  407. * I915_MODE_FLAG_INHERITED, which we clear to force check
  408. * state.
  409. */
  410. DRM_DEBUG_KMS("looking for current mode on connector %s\n",
  411. connector->name);
  412. modes[i] = &connector->state->crtc->mode;
  413. }
  414. crtcs[i] = new_crtc;
  415. DRM_DEBUG_KMS("connector %s on [CRTC:%d:%s]: %dx%d%s\n",
  416. connector->name,
  417. connector->state->crtc->base.id,
  418. connector->state->crtc->name,
  419. modes[i]->hdisplay, modes[i]->vdisplay,
  420. modes[i]->flags & DRM_MODE_FLAG_INTERLACE ? "i" :"");
  421. fallback = false;
  422. conn_configured |= BIT(i);
  423. }
  424. if ((conn_configured & mask) != mask) {
  425. pass++;
  426. goto retry;
  427. }
  428. /*
  429. * If the BIOS didn't enable everything it could, fall back to have the
  430. * same user experiencing of lighting up as much as possible like the
  431. * fbdev helper library.
  432. */
  433. if (num_connectors_enabled != num_connectors_detected &&
  434. num_connectors_enabled < INTEL_INFO(dev_priv)->num_pipes) {
  435. DRM_DEBUG_KMS("fallback: Not all outputs enabled\n");
  436. DRM_DEBUG_KMS("Enabled: %i, detected: %i\n", num_connectors_enabled,
  437. num_connectors_detected);
  438. fallback = true;
  439. }
  440. if (fallback) {
  441. bail:
  442. DRM_DEBUG_KMS("Not using firmware configuration\n");
  443. memcpy(enabled, save_enabled, count);
  444. kfree(save_enabled);
  445. return false;
  446. }
  447. kfree(save_enabled);
  448. return true;
  449. }
  450. static const struct drm_fb_helper_funcs intel_fb_helper_funcs = {
  451. .initial_config = intel_fb_initial_config,
  452. .gamma_set = intel_crtc_fb_gamma_set,
  453. .gamma_get = intel_crtc_fb_gamma_get,
  454. .fb_probe = intelfb_create,
  455. };
  456. static void intel_fbdev_destroy(struct intel_fbdev *ifbdev)
  457. {
  458. /* We rely on the object-free to release the VMA pinning for
  459. * the info->screen_base mmaping. Leaking the VMA is simpler than
  460. * trying to rectify all the possible error paths leading here.
  461. */
  462. drm_fb_helper_unregister_fbi(&ifbdev->helper);
  463. drm_fb_helper_release_fbi(&ifbdev->helper);
  464. drm_fb_helper_fini(&ifbdev->helper);
  465. if (ifbdev->fb) {
  466. mutex_lock(&ifbdev->helper.dev->struct_mutex);
  467. intel_unpin_fb_vma(ifbdev->vma);
  468. mutex_unlock(&ifbdev->helper.dev->struct_mutex);
  469. drm_framebuffer_remove(&ifbdev->fb->base);
  470. }
  471. kfree(ifbdev);
  472. }
  473. /*
  474. * Build an intel_fbdev struct using a BIOS allocated framebuffer, if possible.
  475. * The core display code will have read out the current plane configuration,
  476. * so we use that to figure out if there's an object for us to use as the
  477. * fb, and if so, we re-use it for the fbdev configuration.
  478. *
  479. * Note we only support a single fb shared across pipes for boot (mostly for
  480. * fbcon), so we just find the biggest and use that.
  481. */
  482. static bool intel_fbdev_init_bios(struct drm_device *dev,
  483. struct intel_fbdev *ifbdev)
  484. {
  485. struct intel_framebuffer *fb = NULL;
  486. struct drm_crtc *crtc;
  487. struct intel_crtc *intel_crtc;
  488. unsigned int max_size = 0;
  489. /* Find the largest fb */
  490. for_each_crtc(dev, crtc) {
  491. struct drm_i915_gem_object *obj =
  492. intel_fb_obj(crtc->primary->state->fb);
  493. intel_crtc = to_intel_crtc(crtc);
  494. if (!crtc->state->active || !obj) {
  495. DRM_DEBUG_KMS("pipe %c not active or no fb, skipping\n",
  496. pipe_name(intel_crtc->pipe));
  497. continue;
  498. }
  499. if (obj->base.size > max_size) {
  500. DRM_DEBUG_KMS("found possible fb from plane %c\n",
  501. pipe_name(intel_crtc->pipe));
  502. fb = to_intel_framebuffer(crtc->primary->state->fb);
  503. max_size = obj->base.size;
  504. }
  505. }
  506. if (!fb) {
  507. DRM_DEBUG_KMS("no active fbs found, not using BIOS config\n");
  508. goto out;
  509. }
  510. /* Now make sure all the pipes will fit into it */
  511. for_each_crtc(dev, crtc) {
  512. unsigned int cur_size;
  513. intel_crtc = to_intel_crtc(crtc);
  514. if (!crtc->state->active) {
  515. DRM_DEBUG_KMS("pipe %c not active, skipping\n",
  516. pipe_name(intel_crtc->pipe));
  517. continue;
  518. }
  519. DRM_DEBUG_KMS("checking plane %c for BIOS fb\n",
  520. pipe_name(intel_crtc->pipe));
  521. /*
  522. * See if the plane fb we found above will fit on this
  523. * pipe. Note we need to use the selected fb's pitch and bpp
  524. * rather than the current pipe's, since they differ.
  525. */
  526. cur_size = intel_crtc->config->base.adjusted_mode.crtc_hdisplay;
  527. cur_size = cur_size * fb->base.format->cpp[0];
  528. if (fb->base.pitches[0] < cur_size) {
  529. DRM_DEBUG_KMS("fb not wide enough for plane %c (%d vs %d)\n",
  530. pipe_name(intel_crtc->pipe),
  531. cur_size, fb->base.pitches[0]);
  532. fb = NULL;
  533. break;
  534. }
  535. cur_size = intel_crtc->config->base.adjusted_mode.crtc_vdisplay;
  536. cur_size = intel_fb_align_height(dev, cur_size,
  537. fb->base.format->format,
  538. fb->base.modifier);
  539. cur_size *= fb->base.pitches[0];
  540. DRM_DEBUG_KMS("pipe %c area: %dx%d, bpp: %d, size: %d\n",
  541. pipe_name(intel_crtc->pipe),
  542. intel_crtc->config->base.adjusted_mode.crtc_hdisplay,
  543. intel_crtc->config->base.adjusted_mode.crtc_vdisplay,
  544. fb->base.format->cpp[0] * 8,
  545. cur_size);
  546. if (cur_size > max_size) {
  547. DRM_DEBUG_KMS("fb not big enough for plane %c (%d vs %d)\n",
  548. pipe_name(intel_crtc->pipe),
  549. cur_size, max_size);
  550. fb = NULL;
  551. break;
  552. }
  553. DRM_DEBUG_KMS("fb big enough for plane %c (%d >= %d)\n",
  554. pipe_name(intel_crtc->pipe),
  555. max_size, cur_size);
  556. }
  557. if (!fb) {
  558. DRM_DEBUG_KMS("BIOS fb not suitable for all pipes, not using\n");
  559. goto out;
  560. }
  561. ifbdev->preferred_bpp = fb->base.format->cpp[0] * 8;
  562. ifbdev->fb = fb;
  563. drm_framebuffer_reference(&ifbdev->fb->base);
  564. /* Final pass to check if any active pipes don't have fbs */
  565. for_each_crtc(dev, crtc) {
  566. intel_crtc = to_intel_crtc(crtc);
  567. if (!crtc->state->active)
  568. continue;
  569. WARN(!crtc->primary->fb,
  570. "re-used BIOS config but lost an fb on crtc %d\n",
  571. crtc->base.id);
  572. }
  573. DRM_DEBUG_KMS("using BIOS fb for initial console\n");
  574. return true;
  575. out:
  576. return false;
  577. }
  578. static void intel_fbdev_suspend_worker(struct work_struct *work)
  579. {
  580. intel_fbdev_set_suspend(&container_of(work,
  581. struct drm_i915_private,
  582. fbdev_suspend_work)->drm,
  583. FBINFO_STATE_RUNNING,
  584. true);
  585. }
  586. int intel_fbdev_init(struct drm_device *dev)
  587. {
  588. struct drm_i915_private *dev_priv = to_i915(dev);
  589. struct intel_fbdev *ifbdev;
  590. int ret;
  591. if (WARN_ON(INTEL_INFO(dev_priv)->num_pipes == 0))
  592. return -ENODEV;
  593. ifbdev = kzalloc(sizeof(struct intel_fbdev), GFP_KERNEL);
  594. if (ifbdev == NULL)
  595. return -ENOMEM;
  596. drm_fb_helper_prepare(dev, &ifbdev->helper, &intel_fb_helper_funcs);
  597. if (!intel_fbdev_init_bios(dev, ifbdev))
  598. ifbdev->preferred_bpp = 32;
  599. ret = drm_fb_helper_init(dev, &ifbdev->helper,
  600. INTEL_INFO(dev_priv)->num_pipes, 4);
  601. if (ret) {
  602. kfree(ifbdev);
  603. return ret;
  604. }
  605. dev_priv->fbdev = ifbdev;
  606. INIT_WORK(&dev_priv->fbdev_suspend_work, intel_fbdev_suspend_worker);
  607. drm_fb_helper_single_add_all_connectors(&ifbdev->helper);
  608. return 0;
  609. }
  610. static void intel_fbdev_initial_config(void *data, async_cookie_t cookie)
  611. {
  612. struct intel_fbdev *ifbdev = data;
  613. /* Due to peculiar init order wrt to hpd handling this is separate. */
  614. if (drm_fb_helper_initial_config(&ifbdev->helper,
  615. ifbdev->preferred_bpp))
  616. intel_fbdev_fini(ifbdev->helper.dev);
  617. }
  618. void intel_fbdev_initial_config_async(struct drm_device *dev)
  619. {
  620. struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
  621. if (!ifbdev)
  622. return;
  623. ifbdev->cookie = async_schedule(intel_fbdev_initial_config, ifbdev);
  624. }
  625. static void intel_fbdev_sync(struct intel_fbdev *ifbdev)
  626. {
  627. if (!ifbdev->cookie)
  628. return;
  629. /* Only serialises with all preceding async calls, hence +1 */
  630. async_synchronize_cookie(ifbdev->cookie + 1);
  631. ifbdev->cookie = 0;
  632. }
  633. void intel_fbdev_fini(struct drm_device *dev)
  634. {
  635. struct drm_i915_private *dev_priv = to_i915(dev);
  636. struct intel_fbdev *ifbdev = dev_priv->fbdev;
  637. if (!ifbdev)
  638. return;
  639. cancel_work_sync(&dev_priv->fbdev_suspend_work);
  640. if (!current_is_async())
  641. intel_fbdev_sync(ifbdev);
  642. intel_fbdev_destroy(ifbdev);
  643. dev_priv->fbdev = NULL;
  644. }
  645. void intel_fbdev_set_suspend(struct drm_device *dev, int state, bool synchronous)
  646. {
  647. struct drm_i915_private *dev_priv = to_i915(dev);
  648. struct intel_fbdev *ifbdev = dev_priv->fbdev;
  649. struct fb_info *info;
  650. if (!ifbdev || !ifbdev->fb)
  651. return;
  652. info = ifbdev->helper.fbdev;
  653. if (synchronous) {
  654. /* Flush any pending work to turn the console on, and then
  655. * wait to turn it off. It must be synchronous as we are
  656. * about to suspend or unload the driver.
  657. *
  658. * Note that from within the work-handler, we cannot flush
  659. * ourselves, so only flush outstanding work upon suspend!
  660. */
  661. if (state != FBINFO_STATE_RUNNING)
  662. flush_work(&dev_priv->fbdev_suspend_work);
  663. console_lock();
  664. } else {
  665. /*
  666. * The console lock can be pretty contented on resume due
  667. * to all the printk activity. Try to keep it out of the hot
  668. * path of resume if possible.
  669. */
  670. WARN_ON(state != FBINFO_STATE_RUNNING);
  671. if (!console_trylock()) {
  672. /* Don't block our own workqueue as this can
  673. * be run in parallel with other i915.ko tasks.
  674. */
  675. schedule_work(&dev_priv->fbdev_suspend_work);
  676. return;
  677. }
  678. }
  679. /* On resume from hibernation: If the object is shmemfs backed, it has
  680. * been restored from swap. If the object is stolen however, it will be
  681. * full of whatever garbage was left in there.
  682. */
  683. if (state == FBINFO_STATE_RUNNING && ifbdev->fb->obj->stolen)
  684. memset_io(info->screen_base, 0, info->screen_size);
  685. drm_fb_helper_set_suspend(&ifbdev->helper, state);
  686. console_unlock();
  687. }
  688. void intel_fbdev_output_poll_changed(struct drm_device *dev)
  689. {
  690. struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
  691. if (ifbdev && ifbdev->fb)
  692. drm_fb_helper_hotplug_event(&ifbdev->helper);
  693. }
  694. void intel_fbdev_restore_mode(struct drm_device *dev)
  695. {
  696. struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
  697. if (!ifbdev)
  698. return;
  699. intel_fbdev_sync(ifbdev);
  700. if (!ifbdev->fb)
  701. return;
  702. if (drm_fb_helper_restore_fbdev_mode_unlocked(&ifbdev->helper)) {
  703. DRM_DEBUG("failed to restore crtc mode\n");
  704. } else {
  705. mutex_lock(&dev->struct_mutex);
  706. intel_fb_obj_invalidate(ifbdev->fb->obj, ORIGIN_GTT);
  707. mutex_unlock(&dev->struct_mutex);
  708. }
  709. }