drm_client_modeset.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  1. // SPDX-License-Identifier: MIT
  2. /*
  3. * Copyright 2018 Noralf Trønnes
  4. * Copyright (c) 2006-2009 Red Hat Inc.
  5. * Copyright (c) 2006-2008 Intel Corporation
  6. * Jesse Barnes <[email protected]>
  7. * Copyright (c) 2007 Dave Airlie <[email protected]>
  8. */
  9. #include "drm/drm_modeset_lock.h"
  10. #include <linux/module.h>
  11. #include <linux/mutex.h>
  12. #include <linux/slab.h>
  13. #include <linux/string_helpers.h>
  14. #include <drm/drm_atomic.h>
  15. #include <drm/drm_client.h>
  16. #include <drm/drm_connector.h>
  17. #include <drm/drm_crtc.h>
  18. #include <drm/drm_device.h>
  19. #include <drm/drm_drv.h>
  20. #include <drm/drm_edid.h>
  21. #include <drm/drm_encoder.h>
  22. #include <drm/drm_print.h>
  23. #include "drm_crtc_internal.h"
  24. #include "drm_internal.h"
  25. #define DRM_CLIENT_MAX_CLONED_CONNECTORS 8
  26. struct drm_client_offset {
  27. int x, y;
  28. };
  29. int drm_client_modeset_create(struct drm_client_dev *client)
  30. {
  31. struct drm_device *dev = client->dev;
  32. unsigned int num_crtc = dev->mode_config.num_crtc;
  33. unsigned int max_connector_count = 1;
  34. struct drm_mode_set *modeset;
  35. struct drm_crtc *crtc;
  36. unsigned int i = 0;
  37. /* Add terminating zero entry to enable index less iteration */
  38. client->modesets = kcalloc(num_crtc + 1, sizeof(*client->modesets), GFP_KERNEL);
  39. if (!client->modesets)
  40. return -ENOMEM;
  41. mutex_init(&client->modeset_mutex);
  42. drm_for_each_crtc(crtc, dev)
  43. client->modesets[i++].crtc = crtc;
  44. /* Cloning is only supported in the single crtc case. */
  45. if (num_crtc == 1)
  46. max_connector_count = DRM_CLIENT_MAX_CLONED_CONNECTORS;
  47. for (modeset = client->modesets; modeset->crtc; modeset++) {
  48. modeset->connectors = kcalloc(max_connector_count,
  49. sizeof(*modeset->connectors), GFP_KERNEL);
  50. if (!modeset->connectors)
  51. goto err_free;
  52. }
  53. return 0;
  54. err_free:
  55. drm_client_modeset_free(client);
  56. return -ENOMEM;
  57. }
  58. static void drm_client_modeset_release(struct drm_client_dev *client)
  59. {
  60. struct drm_mode_set *modeset;
  61. unsigned int i;
  62. drm_client_for_each_modeset(modeset, client) {
  63. drm_mode_destroy(client->dev, modeset->mode);
  64. modeset->mode = NULL;
  65. modeset->fb = NULL;
  66. for (i = 0; i < modeset->num_connectors; i++) {
  67. drm_connector_put(modeset->connectors[i]);
  68. modeset->connectors[i] = NULL;
  69. }
  70. modeset->num_connectors = 0;
  71. }
  72. }
  73. void drm_client_modeset_free(struct drm_client_dev *client)
  74. {
  75. struct drm_mode_set *modeset;
  76. mutex_lock(&client->modeset_mutex);
  77. drm_client_modeset_release(client);
  78. drm_client_for_each_modeset(modeset, client)
  79. kfree(modeset->connectors);
  80. mutex_unlock(&client->modeset_mutex);
  81. mutex_destroy(&client->modeset_mutex);
  82. kfree(client->modesets);
  83. }
  84. static struct drm_mode_set *
  85. drm_client_find_modeset(struct drm_client_dev *client, struct drm_crtc *crtc)
  86. {
  87. struct drm_mode_set *modeset;
  88. drm_client_for_each_modeset(modeset, client)
  89. if (modeset->crtc == crtc)
  90. return modeset;
  91. return NULL;
  92. }
  93. static struct drm_display_mode *
  94. drm_connector_get_tiled_mode(struct drm_connector *connector)
  95. {
  96. struct drm_display_mode *mode;
  97. list_for_each_entry(mode, &connector->modes, head) {
  98. if (mode->hdisplay == connector->tile_h_size &&
  99. mode->vdisplay == connector->tile_v_size)
  100. return mode;
  101. }
  102. return NULL;
  103. }
  104. static struct drm_display_mode *
  105. drm_connector_fallback_non_tiled_mode(struct drm_connector *connector)
  106. {
  107. struct drm_display_mode *mode;
  108. list_for_each_entry(mode, &connector->modes, head) {
  109. if (mode->hdisplay == connector->tile_h_size &&
  110. mode->vdisplay == connector->tile_v_size)
  111. continue;
  112. return mode;
  113. }
  114. return NULL;
  115. }
  116. static struct drm_display_mode *
  117. drm_connector_has_preferred_mode(struct drm_connector *connector, int width, int height)
  118. {
  119. struct drm_display_mode *mode;
  120. list_for_each_entry(mode, &connector->modes, head) {
  121. if (mode->hdisplay > width ||
  122. mode->vdisplay > height)
  123. continue;
  124. if (mode->type & DRM_MODE_TYPE_PREFERRED)
  125. return mode;
  126. }
  127. return NULL;
  128. }
  129. static struct drm_display_mode *drm_connector_pick_cmdline_mode(struct drm_connector *connector)
  130. {
  131. struct drm_cmdline_mode *cmdline_mode;
  132. struct drm_display_mode *mode;
  133. bool prefer_non_interlace;
  134. /*
  135. * Find a user-defined mode. If the user gave us a valid
  136. * mode on the kernel command line, it will show up in this
  137. * list.
  138. */
  139. list_for_each_entry(mode, &connector->modes, head) {
  140. if (mode->type & DRM_MODE_TYPE_USERDEF)
  141. return mode;
  142. }
  143. cmdline_mode = &connector->cmdline_mode;
  144. if (cmdline_mode->specified == false)
  145. return NULL;
  146. /*
  147. * Attempt to find a matching mode in the list of modes we
  148. * have gotten so far.
  149. */
  150. prefer_non_interlace = !cmdline_mode->interlace;
  151. again:
  152. list_for_each_entry(mode, &connector->modes, head) {
  153. /* Check (optional) mode name first */
  154. if (!strcmp(mode->name, cmdline_mode->name))
  155. return mode;
  156. /* check width/height */
  157. if (mode->hdisplay != cmdline_mode->xres ||
  158. mode->vdisplay != cmdline_mode->yres)
  159. continue;
  160. if (cmdline_mode->refresh_specified) {
  161. if (drm_mode_vrefresh(mode) != cmdline_mode->refresh)
  162. continue;
  163. }
  164. if (cmdline_mode->interlace) {
  165. if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
  166. continue;
  167. } else if (prefer_non_interlace) {
  168. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  169. continue;
  170. }
  171. return mode;
  172. }
  173. if (prefer_non_interlace) {
  174. prefer_non_interlace = false;
  175. goto again;
  176. }
  177. return NULL;
  178. }
  179. static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
  180. {
  181. bool enable;
  182. if (connector->display_info.non_desktop)
  183. return false;
  184. if (strict)
  185. enable = connector->status == connector_status_connected;
  186. else
  187. enable = connector->status != connector_status_disconnected;
  188. return enable;
  189. }
  190. static void drm_client_connectors_enabled(struct drm_connector **connectors,
  191. unsigned int connector_count,
  192. bool *enabled)
  193. {
  194. bool any_enabled = false;
  195. struct drm_connector *connector;
  196. int i = 0;
  197. for (i = 0; i < connector_count; i++) {
  198. connector = connectors[i];
  199. enabled[i] = drm_connector_enabled(connector, true);
  200. DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
  201. connector->display_info.non_desktop ? "non desktop" : str_yes_no(enabled[i]));
  202. any_enabled |= enabled[i];
  203. }
  204. if (any_enabled)
  205. return;
  206. for (i = 0; i < connector_count; i++)
  207. enabled[i] = drm_connector_enabled(connectors[i], false);
  208. }
  209. static bool drm_client_target_cloned(struct drm_device *dev,
  210. struct drm_connector **connectors,
  211. unsigned int connector_count,
  212. struct drm_display_mode **modes,
  213. struct drm_client_offset *offsets,
  214. bool *enabled, int width, int height)
  215. {
  216. int count, i, j;
  217. bool can_clone = false;
  218. struct drm_display_mode *dmt_mode, *mode;
  219. /* only contemplate cloning in the single crtc case */
  220. if (dev->mode_config.num_crtc > 1)
  221. return false;
  222. count = 0;
  223. for (i = 0; i < connector_count; i++) {
  224. if (enabled[i])
  225. count++;
  226. }
  227. /* only contemplate cloning if more than one connector is enabled */
  228. if (count <= 1)
  229. return false;
  230. /* check the command line or if nothing common pick 1024x768 */
  231. can_clone = true;
  232. for (i = 0; i < connector_count; i++) {
  233. if (!enabled[i])
  234. continue;
  235. modes[i] = drm_connector_pick_cmdline_mode(connectors[i]);
  236. if (!modes[i]) {
  237. can_clone = false;
  238. break;
  239. }
  240. for (j = 0; j < i; j++) {
  241. if (!enabled[j])
  242. continue;
  243. if (!drm_mode_match(modes[j], modes[i],
  244. DRM_MODE_MATCH_TIMINGS |
  245. DRM_MODE_MATCH_CLOCK |
  246. DRM_MODE_MATCH_FLAGS |
  247. DRM_MODE_MATCH_3D_FLAGS))
  248. can_clone = false;
  249. }
  250. }
  251. if (can_clone) {
  252. DRM_DEBUG_KMS("can clone using command line\n");
  253. return true;
  254. }
  255. /* try and find a 1024x768 mode on each connector */
  256. can_clone = true;
  257. dmt_mode = drm_mode_find_dmt(dev, 1024, 768, 60, false);
  258. if (!dmt_mode)
  259. goto fail;
  260. for (i = 0; i < connector_count; i++) {
  261. if (!enabled[i])
  262. continue;
  263. list_for_each_entry(mode, &connectors[i]->modes, head) {
  264. if (drm_mode_match(mode, dmt_mode,
  265. DRM_MODE_MATCH_TIMINGS |
  266. DRM_MODE_MATCH_CLOCK |
  267. DRM_MODE_MATCH_FLAGS |
  268. DRM_MODE_MATCH_3D_FLAGS))
  269. modes[i] = mode;
  270. }
  271. if (!modes[i])
  272. can_clone = false;
  273. }
  274. kfree(dmt_mode);
  275. if (can_clone) {
  276. DRM_DEBUG_KMS("can clone using 1024x768\n");
  277. return true;
  278. }
  279. fail:
  280. DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
  281. return false;
  282. }
  283. static int drm_client_get_tile_offsets(struct drm_connector **connectors,
  284. unsigned int connector_count,
  285. struct drm_display_mode **modes,
  286. struct drm_client_offset *offsets,
  287. int idx,
  288. int h_idx, int v_idx)
  289. {
  290. struct drm_connector *connector;
  291. int i;
  292. int hoffset = 0, voffset = 0;
  293. for (i = 0; i < connector_count; i++) {
  294. connector = connectors[i];
  295. if (!connector->has_tile)
  296. continue;
  297. if (!modes[i] && (h_idx || v_idx)) {
  298. DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i,
  299. connector->base.id);
  300. continue;
  301. }
  302. if (connector->tile_h_loc < h_idx)
  303. hoffset += modes[i]->hdisplay;
  304. if (connector->tile_v_loc < v_idx)
  305. voffset += modes[i]->vdisplay;
  306. }
  307. offsets[idx].x = hoffset;
  308. offsets[idx].y = voffset;
  309. DRM_DEBUG_KMS("returned %d %d for %d %d\n", hoffset, voffset, h_idx, v_idx);
  310. return 0;
  311. }
  312. static bool drm_client_target_preferred(struct drm_connector **connectors,
  313. unsigned int connector_count,
  314. struct drm_display_mode **modes,
  315. struct drm_client_offset *offsets,
  316. bool *enabled, int width, int height)
  317. {
  318. const u64 mask = BIT_ULL(connector_count) - 1;
  319. struct drm_connector *connector;
  320. u64 conn_configured = 0;
  321. int tile_pass = 0;
  322. int num_tiled_conns = 0;
  323. int i;
  324. for (i = 0; i < connector_count; i++) {
  325. if (connectors[i]->has_tile &&
  326. connectors[i]->status == connector_status_connected)
  327. num_tiled_conns++;
  328. }
  329. retry:
  330. for (i = 0; i < connector_count; i++) {
  331. connector = connectors[i];
  332. if (conn_configured & BIT_ULL(i))
  333. continue;
  334. if (enabled[i] == false) {
  335. conn_configured |= BIT_ULL(i);
  336. continue;
  337. }
  338. /* first pass over all the untiled connectors */
  339. if (tile_pass == 0 && connector->has_tile)
  340. continue;
  341. if (tile_pass == 1) {
  342. if (connector->tile_h_loc != 0 ||
  343. connector->tile_v_loc != 0)
  344. continue;
  345. } else {
  346. if (connector->tile_h_loc != tile_pass - 1 &&
  347. connector->tile_v_loc != tile_pass - 1)
  348. /* if this tile_pass doesn't cover any of the tiles - keep going */
  349. continue;
  350. /*
  351. * find the tile offsets for this pass - need to find
  352. * all tiles left and above
  353. */
  354. drm_client_get_tile_offsets(connectors, connector_count, modes, offsets, i,
  355. connector->tile_h_loc, connector->tile_v_loc);
  356. }
  357. DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
  358. connector->base.id);
  359. /* got for command line mode first */
  360. modes[i] = drm_connector_pick_cmdline_mode(connector);
  361. if (!modes[i]) {
  362. DRM_DEBUG_KMS("looking for preferred mode on connector %d %d\n",
  363. connector->base.id, connector->tile_group ? connector->tile_group->id : 0);
  364. modes[i] = drm_connector_has_preferred_mode(connector, width, height);
  365. }
  366. /* No preferred modes, pick one off the list */
  367. if (!modes[i] && !list_empty(&connector->modes)) {
  368. list_for_each_entry(modes[i], &connector->modes, head)
  369. break;
  370. }
  371. /*
  372. * In case of tiled mode if all tiles not present fallback to
  373. * first available non tiled mode.
  374. * After all tiles are present, try to find the tiled mode
  375. * for all and if tiled mode not present due to fbcon size
  376. * limitations, use first non tiled mode only for
  377. * tile 0,0 and set to no mode for all other tiles.
  378. */
  379. if (connector->has_tile) {
  380. if (num_tiled_conns <
  381. connector->num_h_tile * connector->num_v_tile ||
  382. (connector->tile_h_loc == 0 &&
  383. connector->tile_v_loc == 0 &&
  384. !drm_connector_get_tiled_mode(connector))) {
  385. DRM_DEBUG_KMS("Falling back to non tiled mode on Connector %d\n",
  386. connector->base.id);
  387. modes[i] = drm_connector_fallback_non_tiled_mode(connector);
  388. } else {
  389. modes[i] = drm_connector_get_tiled_mode(connector);
  390. }
  391. }
  392. DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
  393. "none");
  394. conn_configured |= BIT_ULL(i);
  395. }
  396. if ((conn_configured & mask) != mask) {
  397. tile_pass++;
  398. goto retry;
  399. }
  400. return true;
  401. }
  402. static bool connector_has_possible_crtc(struct drm_connector *connector,
  403. struct drm_crtc *crtc)
  404. {
  405. struct drm_encoder *encoder;
  406. drm_connector_for_each_possible_encoder(connector, encoder) {
  407. if (encoder->possible_crtcs & drm_crtc_mask(crtc))
  408. return true;
  409. }
  410. return false;
  411. }
  412. static int drm_client_pick_crtcs(struct drm_client_dev *client,
  413. struct drm_connector **connectors,
  414. unsigned int connector_count,
  415. struct drm_crtc **best_crtcs,
  416. struct drm_display_mode **modes,
  417. int n, int width, int height)
  418. {
  419. struct drm_device *dev = client->dev;
  420. struct drm_connector *connector;
  421. int my_score, best_score, score;
  422. struct drm_crtc **crtcs, *crtc;
  423. struct drm_mode_set *modeset;
  424. int o;
  425. if (n == connector_count)
  426. return 0;
  427. connector = connectors[n];
  428. best_crtcs[n] = NULL;
  429. best_score = drm_client_pick_crtcs(client, connectors, connector_count,
  430. best_crtcs, modes, n + 1, width, height);
  431. if (modes[n] == NULL)
  432. return best_score;
  433. crtcs = kcalloc(connector_count, sizeof(*crtcs), GFP_KERNEL);
  434. if (!crtcs)
  435. return best_score;
  436. my_score = 1;
  437. if (connector->status == connector_status_connected)
  438. my_score++;
  439. if (connector->cmdline_mode.specified)
  440. my_score++;
  441. if (drm_connector_has_preferred_mode(connector, width, height))
  442. my_score++;
  443. /*
  444. * select a crtc for this connector and then attempt to configure
  445. * remaining connectors
  446. */
  447. drm_client_for_each_modeset(modeset, client) {
  448. crtc = modeset->crtc;
  449. if (!connector_has_possible_crtc(connector, crtc))
  450. continue;
  451. for (o = 0; o < n; o++)
  452. if (best_crtcs[o] == crtc)
  453. break;
  454. if (o < n) {
  455. /* ignore cloning unless only a single crtc */
  456. if (dev->mode_config.num_crtc > 1)
  457. continue;
  458. if (!drm_mode_equal(modes[o], modes[n]))
  459. continue;
  460. }
  461. crtcs[n] = crtc;
  462. memcpy(crtcs, best_crtcs, n * sizeof(*crtcs));
  463. score = my_score + drm_client_pick_crtcs(client, connectors, connector_count,
  464. crtcs, modes, n + 1, width, height);
  465. if (score > best_score) {
  466. best_score = score;
  467. memcpy(best_crtcs, crtcs, connector_count * sizeof(*crtcs));
  468. }
  469. }
  470. kfree(crtcs);
  471. return best_score;
  472. }
  473. /* Try to read the BIOS display configuration and use it for the initial config */
  474. static bool drm_client_firmware_config(struct drm_client_dev *client,
  475. struct drm_connector **connectors,
  476. unsigned int connector_count,
  477. struct drm_crtc **crtcs,
  478. struct drm_display_mode **modes,
  479. struct drm_client_offset *offsets,
  480. bool *enabled, int width, int height)
  481. {
  482. const int count = min_t(unsigned int, connector_count, BITS_PER_LONG);
  483. unsigned long conn_configured, conn_seq, mask;
  484. struct drm_device *dev = client->dev;
  485. int i, j;
  486. bool *save_enabled;
  487. bool fallback = true, ret = true;
  488. int num_connectors_enabled = 0;
  489. int num_connectors_detected = 0;
  490. int num_tiled_conns = 0;
  491. struct drm_modeset_acquire_ctx ctx;
  492. if (!drm_drv_uses_atomic_modeset(dev))
  493. return false;
  494. if (WARN_ON(count <= 0))
  495. return false;
  496. save_enabled = kcalloc(count, sizeof(bool), GFP_KERNEL);
  497. if (!save_enabled)
  498. return false;
  499. drm_modeset_acquire_init(&ctx, 0);
  500. while (drm_modeset_lock_all_ctx(dev, &ctx) != 0)
  501. drm_modeset_backoff(&ctx);
  502. memcpy(save_enabled, enabled, count);
  503. mask = GENMASK(count - 1, 0);
  504. conn_configured = 0;
  505. for (i = 0; i < count; i++) {
  506. if (connectors[i]->has_tile &&
  507. connectors[i]->status == connector_status_connected)
  508. num_tiled_conns++;
  509. }
  510. retry:
  511. conn_seq = conn_configured;
  512. for (i = 0; i < count; i++) {
  513. struct drm_connector *connector;
  514. struct drm_encoder *encoder;
  515. struct drm_crtc *new_crtc;
  516. connector = connectors[i];
  517. if (conn_configured & BIT(i))
  518. continue;
  519. if (conn_seq == 0 && !connector->has_tile)
  520. continue;
  521. if (connector->status == connector_status_connected)
  522. num_connectors_detected++;
  523. if (!enabled[i]) {
  524. DRM_DEBUG_KMS("connector %s not enabled, skipping\n",
  525. connector->name);
  526. conn_configured |= BIT(i);
  527. continue;
  528. }
  529. if (connector->force == DRM_FORCE_OFF) {
  530. DRM_DEBUG_KMS("connector %s is disabled by user, skipping\n",
  531. connector->name);
  532. enabled[i] = false;
  533. continue;
  534. }
  535. encoder = connector->state->best_encoder;
  536. if (!encoder || WARN_ON(!connector->state->crtc)) {
  537. if (connector->force > DRM_FORCE_OFF)
  538. goto bail;
  539. DRM_DEBUG_KMS("connector %s has no encoder or crtc, skipping\n",
  540. connector->name);
  541. enabled[i] = false;
  542. conn_configured |= BIT(i);
  543. continue;
  544. }
  545. num_connectors_enabled++;
  546. new_crtc = connector->state->crtc;
  547. /*
  548. * Make sure we're not trying to drive multiple connectors
  549. * with a single CRTC, since our cloning support may not
  550. * match the BIOS.
  551. */
  552. for (j = 0; j < count; j++) {
  553. if (crtcs[j] == new_crtc) {
  554. DRM_DEBUG_KMS("fallback: cloned configuration\n");
  555. goto bail;
  556. }
  557. }
  558. DRM_DEBUG_KMS("looking for cmdline mode on connector %s\n",
  559. connector->name);
  560. /* go for command line mode first */
  561. modes[i] = drm_connector_pick_cmdline_mode(connector);
  562. /* try for preferred next */
  563. if (!modes[i]) {
  564. DRM_DEBUG_KMS("looking for preferred mode on connector %s %d\n",
  565. connector->name, connector->has_tile);
  566. modes[i] = drm_connector_has_preferred_mode(connector, width, height);
  567. }
  568. /* No preferred mode marked by the EDID? Are there any modes? */
  569. if (!modes[i] && !list_empty(&connector->modes)) {
  570. DRM_DEBUG_KMS("using first mode listed on connector %s\n",
  571. connector->name);
  572. modes[i] = list_first_entry(&connector->modes,
  573. struct drm_display_mode,
  574. head);
  575. }
  576. /* last resort: use current mode */
  577. if (!modes[i]) {
  578. /*
  579. * IMPORTANT: We want to use the adjusted mode (i.e.
  580. * after the panel fitter upscaling) as the initial
  581. * config, not the input mode, which is what crtc->mode
  582. * usually contains. But since our current
  583. * code puts a mode derived from the post-pfit timings
  584. * into crtc->mode this works out correctly.
  585. *
  586. * This is crtc->mode and not crtc->state->mode for the
  587. * fastboot check to work correctly.
  588. */
  589. DRM_DEBUG_KMS("looking for current mode on connector %s\n",
  590. connector->name);
  591. modes[i] = &connector->state->crtc->mode;
  592. }
  593. /*
  594. * In case of tiled modes, if all tiles are not present
  595. * then fallback to a non tiled mode.
  596. */
  597. if (connector->has_tile &&
  598. num_tiled_conns < connector->num_h_tile * connector->num_v_tile) {
  599. DRM_DEBUG_KMS("Falling back to non tiled mode on Connector %d\n",
  600. connector->base.id);
  601. modes[i] = drm_connector_fallback_non_tiled_mode(connector);
  602. }
  603. crtcs[i] = new_crtc;
  604. DRM_DEBUG_KMS("connector %s on [CRTC:%d:%s]: %dx%d%s\n",
  605. connector->name,
  606. connector->state->crtc->base.id,
  607. connector->state->crtc->name,
  608. modes[i]->hdisplay, modes[i]->vdisplay,
  609. modes[i]->flags & DRM_MODE_FLAG_INTERLACE ? "i" : "");
  610. fallback = false;
  611. conn_configured |= BIT(i);
  612. }
  613. if ((conn_configured & mask) != mask && conn_configured != conn_seq)
  614. goto retry;
  615. /*
  616. * If the BIOS didn't enable everything it could, fall back to have the
  617. * same user experiencing of lighting up as much as possible like the
  618. * fbdev helper library.
  619. */
  620. if (num_connectors_enabled != num_connectors_detected &&
  621. num_connectors_enabled < dev->mode_config.num_crtc) {
  622. DRM_DEBUG_KMS("fallback: Not all outputs enabled\n");
  623. DRM_DEBUG_KMS("Enabled: %i, detected: %i\n", num_connectors_enabled,
  624. num_connectors_detected);
  625. fallback = true;
  626. }
  627. if (fallback) {
  628. bail:
  629. DRM_DEBUG_KMS("Not using firmware configuration\n");
  630. memcpy(enabled, save_enabled, count);
  631. ret = false;
  632. }
  633. drm_modeset_drop_locks(&ctx);
  634. drm_modeset_acquire_fini(&ctx);
  635. kfree(save_enabled);
  636. return ret;
  637. }
  638. /**
  639. * drm_client_modeset_probe() - Probe for displays
  640. * @client: DRM client
  641. * @width: Maximum display mode width (optional)
  642. * @height: Maximum display mode height (optional)
  643. *
  644. * This function sets up display pipelines for enabled connectors and stores the
  645. * config in the client's modeset array.
  646. *
  647. * Returns:
  648. * Zero on success or negative error code on failure.
  649. */
  650. int drm_client_modeset_probe(struct drm_client_dev *client, unsigned int width, unsigned int height)
  651. {
  652. struct drm_connector *connector, **connectors = NULL;
  653. struct drm_connector_list_iter conn_iter;
  654. struct drm_device *dev = client->dev;
  655. unsigned int total_modes_count = 0;
  656. struct drm_client_offset *offsets;
  657. unsigned int connector_count = 0;
  658. struct drm_display_mode **modes;
  659. struct drm_crtc **crtcs;
  660. int i, ret = 0;
  661. bool *enabled;
  662. DRM_DEBUG_KMS("\n");
  663. if (!width)
  664. width = dev->mode_config.max_width;
  665. if (!height)
  666. height = dev->mode_config.max_height;
  667. drm_connector_list_iter_begin(dev, &conn_iter);
  668. drm_client_for_each_connector_iter(connector, &conn_iter) {
  669. struct drm_connector **tmp;
  670. tmp = krealloc(connectors, (connector_count + 1) * sizeof(*connectors), GFP_KERNEL);
  671. if (!tmp) {
  672. ret = -ENOMEM;
  673. goto free_connectors;
  674. }
  675. connectors = tmp;
  676. drm_connector_get(connector);
  677. connectors[connector_count++] = connector;
  678. }
  679. drm_connector_list_iter_end(&conn_iter);
  680. if (!connector_count)
  681. return 0;
  682. crtcs = kcalloc(connector_count, sizeof(*crtcs), GFP_KERNEL);
  683. modes = kcalloc(connector_count, sizeof(*modes), GFP_KERNEL);
  684. offsets = kcalloc(connector_count, sizeof(*offsets), GFP_KERNEL);
  685. enabled = kcalloc(connector_count, sizeof(bool), GFP_KERNEL);
  686. if (!crtcs || !modes || !enabled || !offsets) {
  687. DRM_ERROR("Memory allocation failed\n");
  688. ret = -ENOMEM;
  689. goto out;
  690. }
  691. mutex_lock(&client->modeset_mutex);
  692. mutex_lock(&dev->mode_config.mutex);
  693. for (i = 0; i < connector_count; i++)
  694. total_modes_count += connectors[i]->funcs->fill_modes(connectors[i], width, height);
  695. if (!total_modes_count)
  696. DRM_DEBUG_KMS("No connectors reported connected with modes\n");
  697. drm_client_connectors_enabled(connectors, connector_count, enabled);
  698. if (!drm_client_firmware_config(client, connectors, connector_count, crtcs,
  699. modes, offsets, enabled, width, height)) {
  700. memset(modes, 0, connector_count * sizeof(*modes));
  701. memset(crtcs, 0, connector_count * sizeof(*crtcs));
  702. memset(offsets, 0, connector_count * sizeof(*offsets));
  703. if (!drm_client_target_cloned(dev, connectors, connector_count, modes,
  704. offsets, enabled, width, height) &&
  705. !drm_client_target_preferred(connectors, connector_count, modes,
  706. offsets, enabled, width, height))
  707. DRM_ERROR("Unable to find initial modes\n");
  708. DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
  709. width, height);
  710. drm_client_pick_crtcs(client, connectors, connector_count,
  711. crtcs, modes, 0, width, height);
  712. }
  713. mutex_unlock(&dev->mode_config.mutex);
  714. drm_client_modeset_release(client);
  715. for (i = 0; i < connector_count; i++) {
  716. struct drm_display_mode *mode = modes[i];
  717. struct drm_crtc *crtc = crtcs[i];
  718. struct drm_client_offset *offset = &offsets[i];
  719. if (mode && crtc) {
  720. struct drm_mode_set *modeset = drm_client_find_modeset(client, crtc);
  721. struct drm_connector *connector = connectors[i];
  722. DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n",
  723. mode->name, crtc->base.id, offset->x, offset->y);
  724. if (WARN_ON_ONCE(modeset->num_connectors == DRM_CLIENT_MAX_CLONED_CONNECTORS ||
  725. (dev->mode_config.num_crtc > 1 && modeset->num_connectors == 1))) {
  726. ret = -EINVAL;
  727. break;
  728. }
  729. kfree(modeset->mode);
  730. modeset->mode = drm_mode_duplicate(dev, mode);
  731. drm_connector_get(connector);
  732. modeset->connectors[modeset->num_connectors++] = connector;
  733. modeset->x = offset->x;
  734. modeset->y = offset->y;
  735. }
  736. }
  737. mutex_unlock(&client->modeset_mutex);
  738. out:
  739. kfree(crtcs);
  740. kfree(modes);
  741. kfree(offsets);
  742. kfree(enabled);
  743. free_connectors:
  744. for (i = 0; i < connector_count; i++)
  745. drm_connector_put(connectors[i]);
  746. kfree(connectors);
  747. return ret;
  748. }
  749. EXPORT_SYMBOL(drm_client_modeset_probe);
  750. /**
  751. * drm_client_rotation() - Check the initial rotation value
  752. * @modeset: DRM modeset
  753. * @rotation: Returned rotation value
  754. *
  755. * This function checks if the primary plane in @modeset can hw rotate
  756. * to match the rotation needed on its connector.
  757. *
  758. * Note: Currently only 0 and 180 degrees are supported.
  759. *
  760. * Return:
  761. * True if the plane can do the rotation, false otherwise.
  762. */
  763. bool drm_client_rotation(struct drm_mode_set *modeset, unsigned int *rotation)
  764. {
  765. struct drm_connector *connector = modeset->connectors[0];
  766. struct drm_plane *plane = modeset->crtc->primary;
  767. struct drm_cmdline_mode *cmdline;
  768. u64 valid_mask = 0;
  769. unsigned int i;
  770. if (!modeset->num_connectors)
  771. return false;
  772. switch (connector->display_info.panel_orientation) {
  773. case DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP:
  774. *rotation = DRM_MODE_ROTATE_180;
  775. break;
  776. case DRM_MODE_PANEL_ORIENTATION_LEFT_UP:
  777. *rotation = DRM_MODE_ROTATE_90;
  778. break;
  779. case DRM_MODE_PANEL_ORIENTATION_RIGHT_UP:
  780. *rotation = DRM_MODE_ROTATE_270;
  781. break;
  782. default:
  783. *rotation = DRM_MODE_ROTATE_0;
  784. }
  785. /**
  786. * The panel already defined the default rotation
  787. * through its orientation. Whatever has been provided
  788. * on the command line needs to be added to that.
  789. *
  790. * Unfortunately, the rotations are at different bit
  791. * indices, so the math to add them up are not as
  792. * trivial as they could.
  793. *
  794. * Reflections on the other hand are pretty trivial to deal with, a
  795. * simple XOR between the two handle the addition nicely.
  796. */
  797. cmdline = &connector->cmdline_mode;
  798. if (cmdline->specified && cmdline->rotation_reflection) {
  799. unsigned int cmdline_rest, panel_rest;
  800. unsigned int cmdline_rot, panel_rot;
  801. unsigned int sum_rot, sum_rest;
  802. panel_rot = ilog2(*rotation & DRM_MODE_ROTATE_MASK);
  803. cmdline_rot = ilog2(cmdline->rotation_reflection & DRM_MODE_ROTATE_MASK);
  804. sum_rot = (panel_rot + cmdline_rot) % 4;
  805. panel_rest = *rotation & ~DRM_MODE_ROTATE_MASK;
  806. cmdline_rest = cmdline->rotation_reflection & ~DRM_MODE_ROTATE_MASK;
  807. sum_rest = panel_rest ^ cmdline_rest;
  808. *rotation = (1 << sum_rot) | sum_rest;
  809. }
  810. /*
  811. * TODO: support 90 / 270 degree hardware rotation,
  812. * depending on the hardware this may require the framebuffer
  813. * to be in a specific tiling format.
  814. */
  815. if (((*rotation & DRM_MODE_ROTATE_MASK) != DRM_MODE_ROTATE_0 &&
  816. (*rotation & DRM_MODE_ROTATE_MASK) != DRM_MODE_ROTATE_180) ||
  817. !plane->rotation_property)
  818. return false;
  819. for (i = 0; i < plane->rotation_property->num_values; i++)
  820. valid_mask |= (1ULL << plane->rotation_property->values[i]);
  821. if (!(*rotation & valid_mask))
  822. return false;
  823. return true;
  824. }
  825. EXPORT_SYMBOL(drm_client_rotation);
  826. static int drm_client_modeset_commit_atomic(struct drm_client_dev *client, bool active, bool check)
  827. {
  828. struct drm_device *dev = client->dev;
  829. struct drm_plane *plane;
  830. struct drm_atomic_state *state;
  831. struct drm_modeset_acquire_ctx ctx;
  832. struct drm_mode_set *mode_set;
  833. int ret;
  834. drm_modeset_acquire_init(&ctx, 0);
  835. state = drm_atomic_state_alloc(dev);
  836. if (!state) {
  837. ret = -ENOMEM;
  838. goto out_ctx;
  839. }
  840. state->acquire_ctx = &ctx;
  841. retry:
  842. drm_for_each_plane(plane, dev) {
  843. struct drm_plane_state *plane_state;
  844. plane_state = drm_atomic_get_plane_state(state, plane);
  845. if (IS_ERR(plane_state)) {
  846. ret = PTR_ERR(plane_state);
  847. goto out_state;
  848. }
  849. plane_state->rotation = DRM_MODE_ROTATE_0;
  850. /* disable non-primary: */
  851. if (plane->type == DRM_PLANE_TYPE_PRIMARY)
  852. continue;
  853. ret = __drm_atomic_helper_disable_plane(plane, plane_state);
  854. if (ret != 0)
  855. goto out_state;
  856. }
  857. drm_client_for_each_modeset(mode_set, client) {
  858. struct drm_plane *primary = mode_set->crtc->primary;
  859. unsigned int rotation;
  860. if (drm_client_rotation(mode_set, &rotation)) {
  861. struct drm_plane_state *plane_state;
  862. /* Cannot fail as we've already gotten the plane state above */
  863. plane_state = drm_atomic_get_new_plane_state(state, primary);
  864. plane_state->rotation = rotation;
  865. }
  866. ret = __drm_atomic_helper_set_config(mode_set, state);
  867. if (ret != 0)
  868. goto out_state;
  869. /*
  870. * __drm_atomic_helper_set_config() sets active when a
  871. * mode is set, unconditionally clear it if we force DPMS off
  872. */
  873. if (!active) {
  874. struct drm_crtc *crtc = mode_set->crtc;
  875. struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
  876. crtc_state->active = false;
  877. }
  878. }
  879. if (check)
  880. ret = drm_atomic_check_only(state);
  881. else
  882. ret = drm_atomic_commit(state);
  883. out_state:
  884. if (ret == -EDEADLK)
  885. goto backoff;
  886. drm_atomic_state_put(state);
  887. out_ctx:
  888. drm_modeset_drop_locks(&ctx);
  889. drm_modeset_acquire_fini(&ctx);
  890. return ret;
  891. backoff:
  892. drm_atomic_state_clear(state);
  893. drm_modeset_backoff(&ctx);
  894. goto retry;
  895. }
  896. static int drm_client_modeset_commit_legacy(struct drm_client_dev *client)
  897. {
  898. struct drm_device *dev = client->dev;
  899. struct drm_mode_set *mode_set;
  900. struct drm_plane *plane;
  901. int ret = 0;
  902. drm_modeset_lock_all(dev);
  903. drm_for_each_plane(plane, dev) {
  904. if (plane->type != DRM_PLANE_TYPE_PRIMARY)
  905. drm_plane_force_disable(plane);
  906. if (plane->rotation_property)
  907. drm_mode_plane_set_obj_prop(plane,
  908. plane->rotation_property,
  909. DRM_MODE_ROTATE_0);
  910. }
  911. drm_client_for_each_modeset(mode_set, client) {
  912. struct drm_crtc *crtc = mode_set->crtc;
  913. if (crtc->funcs->cursor_set2) {
  914. ret = crtc->funcs->cursor_set2(crtc, NULL, 0, 0, 0, 0, 0);
  915. if (ret)
  916. goto out;
  917. } else if (crtc->funcs->cursor_set) {
  918. ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0);
  919. if (ret)
  920. goto out;
  921. }
  922. ret = drm_mode_set_config_internal(mode_set);
  923. if (ret)
  924. goto out;
  925. }
  926. out:
  927. drm_modeset_unlock_all(dev);
  928. return ret;
  929. }
  930. /**
  931. * drm_client_modeset_check() - Check modeset configuration
  932. * @client: DRM client
  933. *
  934. * Check modeset configuration.
  935. *
  936. * Returns:
  937. * Zero on success or negative error code on failure.
  938. */
  939. int drm_client_modeset_check(struct drm_client_dev *client)
  940. {
  941. int ret;
  942. if (!drm_drv_uses_atomic_modeset(client->dev))
  943. return 0;
  944. mutex_lock(&client->modeset_mutex);
  945. ret = drm_client_modeset_commit_atomic(client, true, true);
  946. mutex_unlock(&client->modeset_mutex);
  947. return ret;
  948. }
  949. EXPORT_SYMBOL(drm_client_modeset_check);
  950. /**
  951. * drm_client_modeset_commit_locked() - Force commit CRTC configuration
  952. * @client: DRM client
  953. *
  954. * Commit modeset configuration to crtcs without checking if there is a DRM
  955. * master. The assumption is that the caller already holds an internal DRM
  956. * master reference acquired with drm_master_internal_acquire().
  957. *
  958. * Returns:
  959. * Zero on success or negative error code on failure.
  960. */
  961. int drm_client_modeset_commit_locked(struct drm_client_dev *client)
  962. {
  963. struct drm_device *dev = client->dev;
  964. int ret;
  965. mutex_lock(&client->modeset_mutex);
  966. if (drm_drv_uses_atomic_modeset(dev))
  967. ret = drm_client_modeset_commit_atomic(client, true, false);
  968. else
  969. ret = drm_client_modeset_commit_legacy(client);
  970. mutex_unlock(&client->modeset_mutex);
  971. return ret;
  972. }
  973. EXPORT_SYMBOL(drm_client_modeset_commit_locked);
  974. /**
  975. * drm_client_modeset_commit() - Commit CRTC configuration
  976. * @client: DRM client
  977. *
  978. * Commit modeset configuration to crtcs.
  979. *
  980. * Returns:
  981. * Zero on success or negative error code on failure.
  982. */
  983. int drm_client_modeset_commit(struct drm_client_dev *client)
  984. {
  985. struct drm_device *dev = client->dev;
  986. int ret;
  987. if (!drm_master_internal_acquire(dev))
  988. return -EBUSY;
  989. ret = drm_client_modeset_commit_locked(client);
  990. drm_master_internal_release(dev);
  991. return ret;
  992. }
  993. EXPORT_SYMBOL(drm_client_modeset_commit);
  994. static void drm_client_modeset_dpms_legacy(struct drm_client_dev *client, int dpms_mode)
  995. {
  996. struct drm_device *dev = client->dev;
  997. struct drm_connector *connector;
  998. struct drm_mode_set *modeset;
  999. struct drm_modeset_acquire_ctx ctx;
  1000. int j;
  1001. int ret;
  1002. DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, ret);
  1003. drm_client_for_each_modeset(modeset, client) {
  1004. if (!modeset->crtc->enabled)
  1005. continue;
  1006. for (j = 0; j < modeset->num_connectors; j++) {
  1007. connector = modeset->connectors[j];
  1008. connector->funcs->dpms(connector, dpms_mode);
  1009. drm_object_property_set_value(&connector->base,
  1010. dev->mode_config.dpms_property, dpms_mode);
  1011. }
  1012. }
  1013. DRM_MODESET_LOCK_ALL_END(dev, ctx, ret);
  1014. }
  1015. /**
  1016. * drm_client_modeset_dpms() - Set DPMS mode
  1017. * @client: DRM client
  1018. * @mode: DPMS mode
  1019. *
  1020. * Note: For atomic drivers @mode is reduced to on/off.
  1021. *
  1022. * Returns:
  1023. * Zero on success or negative error code on failure.
  1024. */
  1025. int drm_client_modeset_dpms(struct drm_client_dev *client, int mode)
  1026. {
  1027. struct drm_device *dev = client->dev;
  1028. int ret = 0;
  1029. if (!drm_master_internal_acquire(dev))
  1030. return -EBUSY;
  1031. mutex_lock(&client->modeset_mutex);
  1032. if (drm_drv_uses_atomic_modeset(dev))
  1033. ret = drm_client_modeset_commit_atomic(client, mode == DRM_MODE_DPMS_ON, false);
  1034. else
  1035. drm_client_modeset_dpms_legacy(client, mode);
  1036. mutex_unlock(&client->modeset_mutex);
  1037. drm_master_internal_release(dev);
  1038. return ret;
  1039. }
  1040. EXPORT_SYMBOL(drm_client_modeset_dpms);