drm_client_modeset.c 33 KB

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