intel_fbdev.c 24 KB

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