drm_client_modeset.c 32 KB

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