drm_connector.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561
  1. /*
  2. * Copyright (c) 2016 Intel Corporation
  3. *
  4. * Permission to use, copy, modify, distribute, and sell this software and its
  5. * documentation for any purpose is hereby granted without fee, provided that
  6. * the above copyright notice appear in all copies and that both that copyright
  7. * notice and this permission notice appear in supporting documentation, and
  8. * that the name of the copyright holders not be used in advertising or
  9. * publicity pertaining to distribution of the software without specific,
  10. * written prior permission. The copyright holders make no representations
  11. * about the suitability of this software for any purpose. It is provided "as
  12. * is" without express or implied warranty.
  13. *
  14. * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  16. * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  17. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  18. * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  19. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  20. * OF THIS SOFTWARE.
  21. */
  22. #include <drm/drmP.h>
  23. #include <drm/drm_connector.h>
  24. #include <drm/drm_edid.h>
  25. #include <drm/drm_encoder.h>
  26. #include "drm_crtc_internal.h"
  27. #include "drm_internal.h"
  28. /**
  29. * DOC: overview
  30. *
  31. * In DRM connectors are the general abstraction for display sinks, and include
  32. * als fixed panels or anything else that can display pixels in some form. As
  33. * opposed to all other KMS objects representing hardware (like CRTC, encoder or
  34. * plane abstractions) connectors can be hotplugged and unplugged at runtime.
  35. * Hence they are reference-counted using drm_connector_get() and
  36. * drm_connector_put().
  37. *
  38. * KMS driver must create, initialize, register and attach at a &struct
  39. * drm_connector for each such sink. The instance is created as other KMS
  40. * objects and initialized by setting the following fields. The connector is
  41. * initialized with a call to drm_connector_init() with a pointer to the
  42. * &struct drm_connector_funcs and a connector type, and then exposed to
  43. * userspace with a call to drm_connector_register().
  44. *
  45. * Connectors must be attached to an encoder to be used. For devices that map
  46. * connectors to encoders 1:1, the connector should be attached at
  47. * initialization time with a call to drm_mode_connector_attach_encoder(). The
  48. * driver must also set the &drm_connector.encoder field to point to the
  49. * attached encoder.
  50. *
  51. * For connectors which are not fixed (like built-in panels) the driver needs to
  52. * support hotplug notifications. The simplest way to do that is by using the
  53. * probe helpers, see drm_kms_helper_poll_init() for connectors which don't have
  54. * hardware support for hotplug interrupts. Connectors with hardware hotplug
  55. * support can instead use e.g. drm_helper_hpd_irq_event().
  56. */
  57. struct drm_conn_prop_enum_list {
  58. int type;
  59. const char *name;
  60. struct ida ida;
  61. };
  62. /*
  63. * Connector and encoder types.
  64. */
  65. static struct drm_conn_prop_enum_list drm_connector_enum_list[] = {
  66. { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
  67. { DRM_MODE_CONNECTOR_VGA, "VGA" },
  68. { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
  69. { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
  70. { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
  71. { DRM_MODE_CONNECTOR_Composite, "Composite" },
  72. { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
  73. { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
  74. { DRM_MODE_CONNECTOR_Component, "Component" },
  75. { DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
  76. { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
  77. { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
  78. { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
  79. { DRM_MODE_CONNECTOR_TV, "TV" },
  80. { DRM_MODE_CONNECTOR_eDP, "eDP" },
  81. { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
  82. { DRM_MODE_CONNECTOR_DSI, "DSI" },
  83. { DRM_MODE_CONNECTOR_DPI, "DPI" },
  84. };
  85. void drm_connector_ida_init(void)
  86. {
  87. int i;
  88. for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
  89. ida_init(&drm_connector_enum_list[i].ida);
  90. }
  91. void drm_connector_ida_destroy(void)
  92. {
  93. int i;
  94. for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
  95. ida_destroy(&drm_connector_enum_list[i].ida);
  96. }
  97. /**
  98. * drm_connector_get_cmdline_mode - reads the user's cmdline mode
  99. * @connector: connector to quwery
  100. *
  101. * The kernel supports per-connector configuration of its consoles through
  102. * use of the video= parameter. This function parses that option and
  103. * extracts the user's specified mode (or enable/disable status) for a
  104. * particular connector. This is typically only used during the early fbdev
  105. * setup.
  106. */
  107. static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
  108. {
  109. struct drm_cmdline_mode *mode = &connector->cmdline_mode;
  110. char *option = NULL;
  111. if (fb_get_options(connector->name, &option))
  112. return;
  113. if (!drm_mode_parse_command_line_for_connector(option,
  114. connector,
  115. mode))
  116. return;
  117. if (mode->force) {
  118. DRM_INFO("forcing %s connector %s\n", connector->name,
  119. drm_get_connector_force_name(mode->force));
  120. connector->force = mode->force;
  121. }
  122. DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
  123. connector->name,
  124. mode->xres, mode->yres,
  125. mode->refresh_specified ? mode->refresh : 60,
  126. mode->rb ? " reduced blanking" : "",
  127. mode->margins ? " with margins" : "",
  128. mode->interlace ? " interlaced" : "");
  129. }
  130. static void drm_connector_free(struct kref *kref)
  131. {
  132. struct drm_connector *connector =
  133. container_of(kref, struct drm_connector, base.refcount);
  134. struct drm_device *dev = connector->dev;
  135. drm_mode_object_unregister(dev, &connector->base);
  136. connector->funcs->destroy(connector);
  137. }
  138. static void drm_connector_free_work_fn(struct work_struct *work)
  139. {
  140. struct drm_connector *connector =
  141. container_of(work, struct drm_connector, free_work);
  142. struct drm_device *dev = connector->dev;
  143. drm_mode_object_unregister(dev, &connector->base);
  144. connector->funcs->destroy(connector);
  145. }
  146. /**
  147. * drm_connector_init - Init a preallocated connector
  148. * @dev: DRM device
  149. * @connector: the connector to init
  150. * @funcs: callbacks for this connector
  151. * @connector_type: user visible type of the connector
  152. *
  153. * Initialises a preallocated connector. Connectors should be
  154. * subclassed as part of driver connector objects.
  155. *
  156. * Returns:
  157. * Zero on success, error code on failure.
  158. */
  159. int drm_connector_init(struct drm_device *dev,
  160. struct drm_connector *connector,
  161. const struct drm_connector_funcs *funcs,
  162. int connector_type)
  163. {
  164. struct drm_mode_config *config = &dev->mode_config;
  165. int ret;
  166. struct ida *connector_ida =
  167. &drm_connector_enum_list[connector_type].ida;
  168. ret = __drm_mode_object_add(dev, &connector->base,
  169. DRM_MODE_OBJECT_CONNECTOR,
  170. false, drm_connector_free);
  171. if (ret)
  172. return ret;
  173. INIT_WORK(&connector->free_work, drm_connector_free_work_fn);
  174. connector->base.properties = &connector->properties;
  175. connector->dev = dev;
  176. connector->funcs = funcs;
  177. ret = ida_simple_get(&config->connector_ida, 0, 0, GFP_KERNEL);
  178. if (ret < 0)
  179. goto out_put;
  180. connector->index = ret;
  181. ret = 0;
  182. connector->connector_type = connector_type;
  183. connector->connector_type_id =
  184. ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
  185. if (connector->connector_type_id < 0) {
  186. ret = connector->connector_type_id;
  187. goto out_put_id;
  188. }
  189. connector->name =
  190. kasprintf(GFP_KERNEL, "%s-%d",
  191. drm_connector_enum_list[connector_type].name,
  192. connector->connector_type_id);
  193. if (!connector->name) {
  194. ret = -ENOMEM;
  195. goto out_put_type_id;
  196. }
  197. INIT_LIST_HEAD(&connector->probed_modes);
  198. INIT_LIST_HEAD(&connector->modes);
  199. mutex_init(&connector->mutex);
  200. connector->edid_blob_ptr = NULL;
  201. connector->status = connector_status_unknown;
  202. drm_connector_get_cmdline_mode(connector);
  203. /* We should add connectors at the end to avoid upsetting the connector
  204. * index too much. */
  205. spin_lock_irq(&config->connector_list_lock);
  206. list_add_tail(&connector->head, &config->connector_list);
  207. config->num_connector++;
  208. spin_unlock_irq(&config->connector_list_lock);
  209. if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
  210. drm_object_attach_property(&connector->base,
  211. config->edid_property,
  212. 0);
  213. drm_object_attach_property(&connector->base,
  214. config->dpms_property, 0);
  215. drm_object_attach_property(&connector->base,
  216. config->link_status_property,
  217. 0);
  218. drm_object_attach_property(&connector->base,
  219. config->non_desktop_property,
  220. 0);
  221. if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
  222. drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
  223. }
  224. connector->debugfs_entry = NULL;
  225. out_put_type_id:
  226. if (ret)
  227. ida_simple_remove(connector_ida, connector->connector_type_id);
  228. out_put_id:
  229. if (ret)
  230. ida_simple_remove(&config->connector_ida, connector->index);
  231. out_put:
  232. if (ret)
  233. drm_mode_object_unregister(dev, &connector->base);
  234. return ret;
  235. }
  236. EXPORT_SYMBOL(drm_connector_init);
  237. /**
  238. * drm_mode_connector_attach_encoder - attach a connector to an encoder
  239. * @connector: connector to attach
  240. * @encoder: encoder to attach @connector to
  241. *
  242. * This function links up a connector to an encoder. Note that the routing
  243. * restrictions between encoders and crtcs are exposed to userspace through the
  244. * possible_clones and possible_crtcs bitmasks.
  245. *
  246. * Returns:
  247. * Zero on success, negative errno on failure.
  248. */
  249. int drm_mode_connector_attach_encoder(struct drm_connector *connector,
  250. struct drm_encoder *encoder)
  251. {
  252. int i;
  253. /*
  254. * In the past, drivers have attempted to model the static association
  255. * of connector to encoder in simple connector/encoder devices using a
  256. * direct assignment of connector->encoder = encoder. This connection
  257. * is a logical one and the responsibility of the core, so drivers are
  258. * expected not to mess with this.
  259. *
  260. * Note that the error return should've been enough here, but a large
  261. * majority of drivers ignores the return value, so add in a big WARN
  262. * to get people's attention.
  263. */
  264. if (WARN_ON(connector->encoder))
  265. return -EINVAL;
  266. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  267. if (connector->encoder_ids[i] == 0) {
  268. connector->encoder_ids[i] = encoder->base.id;
  269. return 0;
  270. }
  271. }
  272. return -ENOMEM;
  273. }
  274. EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
  275. static void drm_mode_remove(struct drm_connector *connector,
  276. struct drm_display_mode *mode)
  277. {
  278. list_del(&mode->head);
  279. drm_mode_destroy(connector->dev, mode);
  280. }
  281. /**
  282. * drm_connector_cleanup - cleans up an initialised connector
  283. * @connector: connector to cleanup
  284. *
  285. * Cleans up the connector but doesn't free the object.
  286. */
  287. void drm_connector_cleanup(struct drm_connector *connector)
  288. {
  289. struct drm_device *dev = connector->dev;
  290. struct drm_display_mode *mode, *t;
  291. /* The connector should have been removed from userspace long before
  292. * it is finally destroyed.
  293. */
  294. if (WARN_ON(connector->registered))
  295. drm_connector_unregister(connector);
  296. if (connector->tile_group) {
  297. drm_mode_put_tile_group(dev, connector->tile_group);
  298. connector->tile_group = NULL;
  299. }
  300. list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
  301. drm_mode_remove(connector, mode);
  302. list_for_each_entry_safe(mode, t, &connector->modes, head)
  303. drm_mode_remove(connector, mode);
  304. ida_simple_remove(&drm_connector_enum_list[connector->connector_type].ida,
  305. connector->connector_type_id);
  306. ida_simple_remove(&dev->mode_config.connector_ida,
  307. connector->index);
  308. kfree(connector->display_info.bus_formats);
  309. drm_mode_object_unregister(dev, &connector->base);
  310. kfree(connector->name);
  311. connector->name = NULL;
  312. spin_lock_irq(&dev->mode_config.connector_list_lock);
  313. list_del(&connector->head);
  314. dev->mode_config.num_connector--;
  315. spin_unlock_irq(&dev->mode_config.connector_list_lock);
  316. WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
  317. if (connector->state && connector->funcs->atomic_destroy_state)
  318. connector->funcs->atomic_destroy_state(connector,
  319. connector->state);
  320. mutex_destroy(&connector->mutex);
  321. memset(connector, 0, sizeof(*connector));
  322. }
  323. EXPORT_SYMBOL(drm_connector_cleanup);
  324. /**
  325. * drm_connector_register - register a connector
  326. * @connector: the connector to register
  327. *
  328. * Register userspace interfaces for a connector
  329. *
  330. * Returns:
  331. * Zero on success, error code on failure.
  332. */
  333. int drm_connector_register(struct drm_connector *connector)
  334. {
  335. int ret = 0;
  336. if (!connector->dev->registered)
  337. return 0;
  338. mutex_lock(&connector->mutex);
  339. if (connector->registered)
  340. goto unlock;
  341. ret = drm_sysfs_connector_add(connector);
  342. if (ret)
  343. goto unlock;
  344. ret = drm_debugfs_connector_add(connector);
  345. if (ret) {
  346. goto err_sysfs;
  347. }
  348. if (connector->funcs->late_register) {
  349. ret = connector->funcs->late_register(connector);
  350. if (ret)
  351. goto err_debugfs;
  352. }
  353. drm_mode_object_register(connector->dev, &connector->base);
  354. connector->registered = true;
  355. goto unlock;
  356. err_debugfs:
  357. drm_debugfs_connector_remove(connector);
  358. err_sysfs:
  359. drm_sysfs_connector_remove(connector);
  360. unlock:
  361. mutex_unlock(&connector->mutex);
  362. return ret;
  363. }
  364. EXPORT_SYMBOL(drm_connector_register);
  365. /**
  366. * drm_connector_unregister - unregister a connector
  367. * @connector: the connector to unregister
  368. *
  369. * Unregister userspace interfaces for a connector
  370. */
  371. void drm_connector_unregister(struct drm_connector *connector)
  372. {
  373. mutex_lock(&connector->mutex);
  374. if (!connector->registered) {
  375. mutex_unlock(&connector->mutex);
  376. return;
  377. }
  378. if (connector->funcs->early_unregister)
  379. connector->funcs->early_unregister(connector);
  380. drm_sysfs_connector_remove(connector);
  381. drm_debugfs_connector_remove(connector);
  382. connector->registered = false;
  383. mutex_unlock(&connector->mutex);
  384. }
  385. EXPORT_SYMBOL(drm_connector_unregister);
  386. void drm_connector_unregister_all(struct drm_device *dev)
  387. {
  388. struct drm_connector *connector;
  389. struct drm_connector_list_iter conn_iter;
  390. drm_connector_list_iter_begin(dev, &conn_iter);
  391. drm_for_each_connector_iter(connector, &conn_iter)
  392. drm_connector_unregister(connector);
  393. drm_connector_list_iter_end(&conn_iter);
  394. }
  395. int drm_connector_register_all(struct drm_device *dev)
  396. {
  397. struct drm_connector *connector;
  398. struct drm_connector_list_iter conn_iter;
  399. int ret = 0;
  400. drm_connector_list_iter_begin(dev, &conn_iter);
  401. drm_for_each_connector_iter(connector, &conn_iter) {
  402. ret = drm_connector_register(connector);
  403. if (ret)
  404. break;
  405. }
  406. drm_connector_list_iter_end(&conn_iter);
  407. if (ret)
  408. drm_connector_unregister_all(dev);
  409. return ret;
  410. }
  411. /**
  412. * drm_get_connector_status_name - return a string for connector status
  413. * @status: connector status to compute name of
  414. *
  415. * In contrast to the other drm_get_*_name functions this one here returns a
  416. * const pointer and hence is threadsafe.
  417. */
  418. const char *drm_get_connector_status_name(enum drm_connector_status status)
  419. {
  420. if (status == connector_status_connected)
  421. return "connected";
  422. else if (status == connector_status_disconnected)
  423. return "disconnected";
  424. else
  425. return "unknown";
  426. }
  427. EXPORT_SYMBOL(drm_get_connector_status_name);
  428. /**
  429. * drm_get_connector_force_name - return a string for connector force
  430. * @force: connector force to get name of
  431. *
  432. * Returns: const pointer to name.
  433. */
  434. const char *drm_get_connector_force_name(enum drm_connector_force force)
  435. {
  436. switch (force) {
  437. case DRM_FORCE_UNSPECIFIED:
  438. return "unspecified";
  439. case DRM_FORCE_OFF:
  440. return "off";
  441. case DRM_FORCE_ON:
  442. return "on";
  443. case DRM_FORCE_ON_DIGITAL:
  444. return "digital";
  445. default:
  446. return "unknown";
  447. }
  448. }
  449. #ifdef CONFIG_LOCKDEP
  450. static struct lockdep_map connector_list_iter_dep_map = {
  451. .name = "drm_connector_list_iter"
  452. };
  453. #endif
  454. /**
  455. * drm_connector_list_iter_begin - initialize a connector_list iterator
  456. * @dev: DRM device
  457. * @iter: connector_list iterator
  458. *
  459. * Sets @iter up to walk the &drm_mode_config.connector_list of @dev. @iter
  460. * must always be cleaned up again by calling drm_connector_list_iter_end().
  461. * Iteration itself happens using drm_connector_list_iter_next() or
  462. * drm_for_each_connector_iter().
  463. */
  464. void drm_connector_list_iter_begin(struct drm_device *dev,
  465. struct drm_connector_list_iter *iter)
  466. {
  467. iter->dev = dev;
  468. iter->conn = NULL;
  469. lock_acquire_shared_recursive(&connector_list_iter_dep_map, 0, 1, NULL, _RET_IP_);
  470. }
  471. EXPORT_SYMBOL(drm_connector_list_iter_begin);
  472. /*
  473. * Extra-safe connector put function that works in any context. Should only be
  474. * used from the connector_iter functions, where we never really expect to
  475. * actually release the connector when dropping our final reference.
  476. */
  477. static void
  478. drm_connector_put_safe(struct drm_connector *conn)
  479. {
  480. if (refcount_dec_and_test(&conn->base.refcount.refcount))
  481. schedule_work(&conn->free_work);
  482. }
  483. /**
  484. * drm_connector_list_iter_next - return next connector
  485. * @iter: connectr_list iterator
  486. *
  487. * Returns the next connector for @iter, or NULL when the list walk has
  488. * completed.
  489. */
  490. struct drm_connector *
  491. drm_connector_list_iter_next(struct drm_connector_list_iter *iter)
  492. {
  493. struct drm_connector *old_conn = iter->conn;
  494. struct drm_mode_config *config = &iter->dev->mode_config;
  495. struct list_head *lhead;
  496. unsigned long flags;
  497. spin_lock_irqsave(&config->connector_list_lock, flags);
  498. lhead = old_conn ? &old_conn->head : &config->connector_list;
  499. do {
  500. if (lhead->next == &config->connector_list) {
  501. iter->conn = NULL;
  502. break;
  503. }
  504. lhead = lhead->next;
  505. iter->conn = list_entry(lhead, struct drm_connector, head);
  506. /* loop until it's not a zombie connector */
  507. } while (!kref_get_unless_zero(&iter->conn->base.refcount));
  508. spin_unlock_irqrestore(&config->connector_list_lock, flags);
  509. if (old_conn)
  510. drm_connector_put_safe(old_conn);
  511. return iter->conn;
  512. }
  513. EXPORT_SYMBOL(drm_connector_list_iter_next);
  514. /**
  515. * drm_connector_list_iter_end - tear down a connector_list iterator
  516. * @iter: connector_list iterator
  517. *
  518. * Tears down @iter and releases any resources (like &drm_connector references)
  519. * acquired while walking the list. This must always be called, both when the
  520. * iteration completes fully or when it was aborted without walking the entire
  521. * list.
  522. */
  523. void drm_connector_list_iter_end(struct drm_connector_list_iter *iter)
  524. {
  525. iter->dev = NULL;
  526. if (iter->conn)
  527. drm_connector_put_safe(iter->conn);
  528. lock_release(&connector_list_iter_dep_map, 0, _RET_IP_);
  529. }
  530. EXPORT_SYMBOL(drm_connector_list_iter_end);
  531. static const struct drm_prop_enum_list drm_subpixel_enum_list[] = {
  532. { SubPixelUnknown, "Unknown" },
  533. { SubPixelHorizontalRGB, "Horizontal RGB" },
  534. { SubPixelHorizontalBGR, "Horizontal BGR" },
  535. { SubPixelVerticalRGB, "Vertical RGB" },
  536. { SubPixelVerticalBGR, "Vertical BGR" },
  537. { SubPixelNone, "None" },
  538. };
  539. /**
  540. * drm_get_subpixel_order_name - return a string for a given subpixel enum
  541. * @order: enum of subpixel_order
  542. *
  543. * Note you could abuse this and return something out of bounds, but that
  544. * would be a caller error. No unscrubbed user data should make it here.
  545. */
  546. const char *drm_get_subpixel_order_name(enum subpixel_order order)
  547. {
  548. return drm_subpixel_enum_list[order].name;
  549. }
  550. EXPORT_SYMBOL(drm_get_subpixel_order_name);
  551. static const struct drm_prop_enum_list drm_dpms_enum_list[] = {
  552. { DRM_MODE_DPMS_ON, "On" },
  553. { DRM_MODE_DPMS_STANDBY, "Standby" },
  554. { DRM_MODE_DPMS_SUSPEND, "Suspend" },
  555. { DRM_MODE_DPMS_OFF, "Off" }
  556. };
  557. DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
  558. static const struct drm_prop_enum_list drm_link_status_enum_list[] = {
  559. { DRM_MODE_LINK_STATUS_GOOD, "Good" },
  560. { DRM_MODE_LINK_STATUS_BAD, "Bad" },
  561. };
  562. /**
  563. * drm_display_info_set_bus_formats - set the supported bus formats
  564. * @info: display info to store bus formats in
  565. * @formats: array containing the supported bus formats
  566. * @num_formats: the number of entries in the fmts array
  567. *
  568. * Store the supported bus formats in display info structure.
  569. * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
  570. * a full list of available formats.
  571. */
  572. int drm_display_info_set_bus_formats(struct drm_display_info *info,
  573. const u32 *formats,
  574. unsigned int num_formats)
  575. {
  576. u32 *fmts = NULL;
  577. if (!formats && num_formats)
  578. return -EINVAL;
  579. if (formats && num_formats) {
  580. fmts = kmemdup(formats, sizeof(*formats) * num_formats,
  581. GFP_KERNEL);
  582. if (!fmts)
  583. return -ENOMEM;
  584. }
  585. kfree(info->bus_formats);
  586. info->bus_formats = fmts;
  587. info->num_bus_formats = num_formats;
  588. return 0;
  589. }
  590. EXPORT_SYMBOL(drm_display_info_set_bus_formats);
  591. /* Optional connector properties. */
  592. static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] = {
  593. { DRM_MODE_SCALE_NONE, "None" },
  594. { DRM_MODE_SCALE_FULLSCREEN, "Full" },
  595. { DRM_MODE_SCALE_CENTER, "Center" },
  596. { DRM_MODE_SCALE_ASPECT, "Full aspect" },
  597. };
  598. static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
  599. { DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
  600. { DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
  601. { DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
  602. };
  603. static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] = {
  604. { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
  605. { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
  606. { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
  607. };
  608. DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
  609. static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] = {
  610. { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
  611. { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
  612. { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
  613. };
  614. DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
  615. drm_dvi_i_subconnector_enum_list)
  616. static const struct drm_prop_enum_list drm_tv_select_enum_list[] = {
  617. { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
  618. { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
  619. { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
  620. { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
  621. { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
  622. };
  623. DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
  624. static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] = {
  625. { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
  626. { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
  627. { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
  628. { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
  629. { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
  630. };
  631. DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
  632. drm_tv_subconnector_enum_list)
  633. /**
  634. * DOC: standard connector properties
  635. *
  636. * DRM connectors have a few standardized properties:
  637. *
  638. * EDID:
  639. * Blob property which contains the current EDID read from the sink. This
  640. * is useful to parse sink identification information like vendor, model
  641. * and serial. Drivers should update this property by calling
  642. * drm_mode_connector_update_edid_property(), usually after having parsed
  643. * the EDID using drm_add_edid_modes(). Userspace cannot change this
  644. * property.
  645. * DPMS:
  646. * Legacy property for setting the power state of the connector. For atomic
  647. * drivers this is only provided for backwards compatibility with existing
  648. * drivers, it remaps to controlling the "ACTIVE" property on the CRTC the
  649. * connector is linked to. Drivers should never set this property directly,
  650. * it is handled by the DRM core by calling the &drm_connector_funcs.dpms
  651. * callback. For atomic drivers the remapping to the "ACTIVE" property is
  652. * implemented in the DRM core. This is the only standard connector
  653. * property that userspace can change.
  654. *
  655. * Note that this property cannot be set through the MODE_ATOMIC ioctl,
  656. * userspace must use "ACTIVE" on the CRTC instead.
  657. *
  658. * WARNING:
  659. *
  660. * For userspace also running on legacy drivers the "DPMS" semantics are a
  661. * lot more complicated. First, userspace cannot rely on the "DPMS" value
  662. * returned by the GETCONNECTOR actually reflecting reality, because many
  663. * drivers fail to update it. For atomic drivers this is taken care of in
  664. * drm_atomic_helper_update_legacy_modeset_state().
  665. *
  666. * The second issue is that the DPMS state is only well-defined when the
  667. * connector is connected to a CRTC. In atomic the DRM core enforces that
  668. * "ACTIVE" is off in such a case, no such checks exists for "DPMS".
  669. *
  670. * Finally, when enabling an output using the legacy SETCONFIG ioctl then
  671. * "DPMS" is forced to ON. But see above, that might not be reflected in
  672. * the software value on legacy drivers.
  673. *
  674. * Summarizing: Only set "DPMS" when the connector is known to be enabled,
  675. * assume that a successful SETCONFIG call also sets "DPMS" to on, and
  676. * never read back the value of "DPMS" because it can be incorrect.
  677. * PATH:
  678. * Connector path property to identify how this sink is physically
  679. * connected. Used by DP MST. This should be set by calling
  680. * drm_mode_connector_set_path_property(), in the case of DP MST with the
  681. * path property the MST manager created. Userspace cannot change this
  682. * property.
  683. * TILE:
  684. * Connector tile group property to indicate how a set of DRM connector
  685. * compose together into one logical screen. This is used by both high-res
  686. * external screens (often only using a single cable, but exposing multiple
  687. * DP MST sinks), or high-res integrated panels (like dual-link DSI) which
  688. * are not gen-locked. Note that for tiled panels which are genlocked, like
  689. * dual-link LVDS or dual-link DSI, the driver should try to not expose the
  690. * tiling and virtualize both &drm_crtc and &drm_plane if needed. Drivers
  691. * should update this value using drm_mode_connector_set_tile_property().
  692. * Userspace cannot change this property.
  693. * link-status:
  694. * Connector link-status property to indicate the status of link. The default
  695. * value of link-status is "GOOD". If something fails during or after modeset,
  696. * the kernel driver may set this to "BAD" and issue a hotplug uevent. Drivers
  697. * should update this value using drm_mode_connector_set_link_status_property().
  698. * non_desktop:
  699. * Indicates the output should be ignored for purposes of displaying a
  700. * standard desktop environment or console. This is most likely because
  701. * the output device is not rectilinear.
  702. *
  703. * Connectors also have one standardized atomic property:
  704. *
  705. * CRTC_ID:
  706. * Mode object ID of the &drm_crtc this connector should be connected to.
  707. */
  708. int drm_connector_create_standard_properties(struct drm_device *dev)
  709. {
  710. struct drm_property *prop;
  711. prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
  712. DRM_MODE_PROP_IMMUTABLE,
  713. "EDID", 0);
  714. if (!prop)
  715. return -ENOMEM;
  716. dev->mode_config.edid_property = prop;
  717. prop = drm_property_create_enum(dev, 0,
  718. "DPMS", drm_dpms_enum_list,
  719. ARRAY_SIZE(drm_dpms_enum_list));
  720. if (!prop)
  721. return -ENOMEM;
  722. dev->mode_config.dpms_property = prop;
  723. prop = drm_property_create(dev,
  724. DRM_MODE_PROP_BLOB |
  725. DRM_MODE_PROP_IMMUTABLE,
  726. "PATH", 0);
  727. if (!prop)
  728. return -ENOMEM;
  729. dev->mode_config.path_property = prop;
  730. prop = drm_property_create(dev,
  731. DRM_MODE_PROP_BLOB |
  732. DRM_MODE_PROP_IMMUTABLE,
  733. "TILE", 0);
  734. if (!prop)
  735. return -ENOMEM;
  736. dev->mode_config.tile_property = prop;
  737. prop = drm_property_create_enum(dev, 0, "link-status",
  738. drm_link_status_enum_list,
  739. ARRAY_SIZE(drm_link_status_enum_list));
  740. if (!prop)
  741. return -ENOMEM;
  742. dev->mode_config.link_status_property = prop;
  743. prop = drm_property_create_bool(dev, DRM_MODE_PROP_IMMUTABLE, "non-desktop");
  744. if (!prop)
  745. return -ENOMEM;
  746. dev->mode_config.non_desktop_property = prop;
  747. return 0;
  748. }
  749. /**
  750. * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
  751. * @dev: DRM device
  752. *
  753. * Called by a driver the first time a DVI-I connector is made.
  754. */
  755. int drm_mode_create_dvi_i_properties(struct drm_device *dev)
  756. {
  757. struct drm_property *dvi_i_selector;
  758. struct drm_property *dvi_i_subconnector;
  759. if (dev->mode_config.dvi_i_select_subconnector_property)
  760. return 0;
  761. dvi_i_selector =
  762. drm_property_create_enum(dev, 0,
  763. "select subconnector",
  764. drm_dvi_i_select_enum_list,
  765. ARRAY_SIZE(drm_dvi_i_select_enum_list));
  766. dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
  767. dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  768. "subconnector",
  769. drm_dvi_i_subconnector_enum_list,
  770. ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
  771. dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
  772. return 0;
  773. }
  774. EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
  775. /**
  776. * drm_create_tv_properties - create TV specific connector properties
  777. * @dev: DRM device
  778. * @num_modes: number of different TV formats (modes) supported
  779. * @modes: array of pointers to strings containing name of each format
  780. *
  781. * Called by a driver's TV initialization routine, this function creates
  782. * the TV specific connector properties for a given device. Caller is
  783. * responsible for allocating a list of format names and passing them to
  784. * this routine.
  785. */
  786. int drm_mode_create_tv_properties(struct drm_device *dev,
  787. unsigned int num_modes,
  788. const char * const modes[])
  789. {
  790. struct drm_property *tv_selector;
  791. struct drm_property *tv_subconnector;
  792. unsigned int i;
  793. if (dev->mode_config.tv_select_subconnector_property)
  794. return 0;
  795. /*
  796. * Basic connector properties
  797. */
  798. tv_selector = drm_property_create_enum(dev, 0,
  799. "select subconnector",
  800. drm_tv_select_enum_list,
  801. ARRAY_SIZE(drm_tv_select_enum_list));
  802. if (!tv_selector)
  803. goto nomem;
  804. dev->mode_config.tv_select_subconnector_property = tv_selector;
  805. tv_subconnector =
  806. drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  807. "subconnector",
  808. drm_tv_subconnector_enum_list,
  809. ARRAY_SIZE(drm_tv_subconnector_enum_list));
  810. if (!tv_subconnector)
  811. goto nomem;
  812. dev->mode_config.tv_subconnector_property = tv_subconnector;
  813. /*
  814. * Other, TV specific properties: margins & TV modes.
  815. */
  816. dev->mode_config.tv_left_margin_property =
  817. drm_property_create_range(dev, 0, "left margin", 0, 100);
  818. if (!dev->mode_config.tv_left_margin_property)
  819. goto nomem;
  820. dev->mode_config.tv_right_margin_property =
  821. drm_property_create_range(dev, 0, "right margin", 0, 100);
  822. if (!dev->mode_config.tv_right_margin_property)
  823. goto nomem;
  824. dev->mode_config.tv_top_margin_property =
  825. drm_property_create_range(dev, 0, "top margin", 0, 100);
  826. if (!dev->mode_config.tv_top_margin_property)
  827. goto nomem;
  828. dev->mode_config.tv_bottom_margin_property =
  829. drm_property_create_range(dev, 0, "bottom margin", 0, 100);
  830. if (!dev->mode_config.tv_bottom_margin_property)
  831. goto nomem;
  832. dev->mode_config.tv_mode_property =
  833. drm_property_create(dev, DRM_MODE_PROP_ENUM,
  834. "mode", num_modes);
  835. if (!dev->mode_config.tv_mode_property)
  836. goto nomem;
  837. for (i = 0; i < num_modes; i++)
  838. drm_property_add_enum(dev->mode_config.tv_mode_property, i,
  839. i, modes[i]);
  840. dev->mode_config.tv_brightness_property =
  841. drm_property_create_range(dev, 0, "brightness", 0, 100);
  842. if (!dev->mode_config.tv_brightness_property)
  843. goto nomem;
  844. dev->mode_config.tv_contrast_property =
  845. drm_property_create_range(dev, 0, "contrast", 0, 100);
  846. if (!dev->mode_config.tv_contrast_property)
  847. goto nomem;
  848. dev->mode_config.tv_flicker_reduction_property =
  849. drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
  850. if (!dev->mode_config.tv_flicker_reduction_property)
  851. goto nomem;
  852. dev->mode_config.tv_overscan_property =
  853. drm_property_create_range(dev, 0, "overscan", 0, 100);
  854. if (!dev->mode_config.tv_overscan_property)
  855. goto nomem;
  856. dev->mode_config.tv_saturation_property =
  857. drm_property_create_range(dev, 0, "saturation", 0, 100);
  858. if (!dev->mode_config.tv_saturation_property)
  859. goto nomem;
  860. dev->mode_config.tv_hue_property =
  861. drm_property_create_range(dev, 0, "hue", 0, 100);
  862. if (!dev->mode_config.tv_hue_property)
  863. goto nomem;
  864. return 0;
  865. nomem:
  866. return -ENOMEM;
  867. }
  868. EXPORT_SYMBOL(drm_mode_create_tv_properties);
  869. /**
  870. * drm_mode_create_scaling_mode_property - create scaling mode property
  871. * @dev: DRM device
  872. *
  873. * Called by a driver the first time it's needed, must be attached to desired
  874. * connectors.
  875. *
  876. * Atomic drivers should use drm_connector_attach_scaling_mode_property()
  877. * instead to correctly assign &drm_connector_state.picture_aspect_ratio
  878. * in the atomic state.
  879. */
  880. int drm_mode_create_scaling_mode_property(struct drm_device *dev)
  881. {
  882. struct drm_property *scaling_mode;
  883. if (dev->mode_config.scaling_mode_property)
  884. return 0;
  885. scaling_mode =
  886. drm_property_create_enum(dev, 0, "scaling mode",
  887. drm_scaling_mode_enum_list,
  888. ARRAY_SIZE(drm_scaling_mode_enum_list));
  889. dev->mode_config.scaling_mode_property = scaling_mode;
  890. return 0;
  891. }
  892. EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
  893. /**
  894. * drm_connector_attach_scaling_mode_property - attach atomic scaling mode property
  895. * @connector: connector to attach scaling mode property on.
  896. * @scaling_mode_mask: or'ed mask of BIT(%DRM_MODE_SCALE_\*).
  897. *
  898. * This is used to add support for scaling mode to atomic drivers.
  899. * The scaling mode will be set to &drm_connector_state.picture_aspect_ratio
  900. * and can be used from &drm_connector_helper_funcs->atomic_check for validation.
  901. *
  902. * This is the atomic version of drm_mode_create_scaling_mode_property().
  903. *
  904. * Returns:
  905. * Zero on success, negative errno on failure.
  906. */
  907. int drm_connector_attach_scaling_mode_property(struct drm_connector *connector,
  908. u32 scaling_mode_mask)
  909. {
  910. struct drm_device *dev = connector->dev;
  911. struct drm_property *scaling_mode_property;
  912. int i, j = 0;
  913. const unsigned valid_scaling_mode_mask =
  914. (1U << ARRAY_SIZE(drm_scaling_mode_enum_list)) - 1;
  915. if (WARN_ON(hweight32(scaling_mode_mask) < 2 ||
  916. scaling_mode_mask & ~valid_scaling_mode_mask))
  917. return -EINVAL;
  918. scaling_mode_property =
  919. drm_property_create(dev, DRM_MODE_PROP_ENUM, "scaling mode",
  920. hweight32(scaling_mode_mask));
  921. if (!scaling_mode_property)
  922. return -ENOMEM;
  923. for (i = 0; i < ARRAY_SIZE(drm_scaling_mode_enum_list); i++) {
  924. int ret;
  925. if (!(BIT(i) & scaling_mode_mask))
  926. continue;
  927. ret = drm_property_add_enum(scaling_mode_property, j++,
  928. drm_scaling_mode_enum_list[i].type,
  929. drm_scaling_mode_enum_list[i].name);
  930. if (ret) {
  931. drm_property_destroy(dev, scaling_mode_property);
  932. return ret;
  933. }
  934. }
  935. drm_object_attach_property(&connector->base,
  936. scaling_mode_property, 0);
  937. connector->scaling_mode_property = scaling_mode_property;
  938. return 0;
  939. }
  940. EXPORT_SYMBOL(drm_connector_attach_scaling_mode_property);
  941. /**
  942. * drm_mode_create_aspect_ratio_property - create aspect ratio property
  943. * @dev: DRM device
  944. *
  945. * Called by a driver the first time it's needed, must be attached to desired
  946. * connectors.
  947. *
  948. * Returns:
  949. * Zero on success, negative errno on failure.
  950. */
  951. int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
  952. {
  953. if (dev->mode_config.aspect_ratio_property)
  954. return 0;
  955. dev->mode_config.aspect_ratio_property =
  956. drm_property_create_enum(dev, 0, "aspect ratio",
  957. drm_aspect_ratio_enum_list,
  958. ARRAY_SIZE(drm_aspect_ratio_enum_list));
  959. if (dev->mode_config.aspect_ratio_property == NULL)
  960. return -ENOMEM;
  961. return 0;
  962. }
  963. EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
  964. /**
  965. * drm_mode_create_suggested_offset_properties - create suggests offset properties
  966. * @dev: DRM device
  967. *
  968. * Create the the suggested x/y offset property for connectors.
  969. */
  970. int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
  971. {
  972. if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
  973. return 0;
  974. dev->mode_config.suggested_x_property =
  975. drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
  976. dev->mode_config.suggested_y_property =
  977. drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
  978. if (dev->mode_config.suggested_x_property == NULL ||
  979. dev->mode_config.suggested_y_property == NULL)
  980. return -ENOMEM;
  981. return 0;
  982. }
  983. EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
  984. /**
  985. * drm_mode_connector_set_path_property - set tile property on connector
  986. * @connector: connector to set property on.
  987. * @path: path to use for property; must not be NULL.
  988. *
  989. * This creates a property to expose to userspace to specify a
  990. * connector path. This is mainly used for DisplayPort MST where
  991. * connectors have a topology and we want to allow userspace to give
  992. * them more meaningful names.
  993. *
  994. * Returns:
  995. * Zero on success, negative errno on failure.
  996. */
  997. int drm_mode_connector_set_path_property(struct drm_connector *connector,
  998. const char *path)
  999. {
  1000. struct drm_device *dev = connector->dev;
  1001. int ret;
  1002. ret = drm_property_replace_global_blob(dev,
  1003. &connector->path_blob_ptr,
  1004. strlen(path) + 1,
  1005. path,
  1006. &connector->base,
  1007. dev->mode_config.path_property);
  1008. return ret;
  1009. }
  1010. EXPORT_SYMBOL(drm_mode_connector_set_path_property);
  1011. /**
  1012. * drm_mode_connector_set_tile_property - set tile property on connector
  1013. * @connector: connector to set property on.
  1014. *
  1015. * This looks up the tile information for a connector, and creates a
  1016. * property for userspace to parse if it exists. The property is of
  1017. * the form of 8 integers using ':' as a separator.
  1018. *
  1019. * Returns:
  1020. * Zero on success, errno on failure.
  1021. */
  1022. int drm_mode_connector_set_tile_property(struct drm_connector *connector)
  1023. {
  1024. struct drm_device *dev = connector->dev;
  1025. char tile[256];
  1026. int ret;
  1027. if (!connector->has_tile) {
  1028. ret = drm_property_replace_global_blob(dev,
  1029. &connector->tile_blob_ptr,
  1030. 0,
  1031. NULL,
  1032. &connector->base,
  1033. dev->mode_config.tile_property);
  1034. return ret;
  1035. }
  1036. snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
  1037. connector->tile_group->id, connector->tile_is_single_monitor,
  1038. connector->num_h_tile, connector->num_v_tile,
  1039. connector->tile_h_loc, connector->tile_v_loc,
  1040. connector->tile_h_size, connector->tile_v_size);
  1041. ret = drm_property_replace_global_blob(dev,
  1042. &connector->tile_blob_ptr,
  1043. strlen(tile) + 1,
  1044. tile,
  1045. &connector->base,
  1046. dev->mode_config.tile_property);
  1047. return ret;
  1048. }
  1049. EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
  1050. /**
  1051. * drm_mode_connector_update_edid_property - update the edid property of a connector
  1052. * @connector: drm connector
  1053. * @edid: new value of the edid property
  1054. *
  1055. * This function creates a new blob modeset object and assigns its id to the
  1056. * connector's edid property.
  1057. *
  1058. * Returns:
  1059. * Zero on success, negative errno on failure.
  1060. */
  1061. int drm_mode_connector_update_edid_property(struct drm_connector *connector,
  1062. const struct edid *edid)
  1063. {
  1064. struct drm_device *dev = connector->dev;
  1065. size_t size = 0;
  1066. int ret;
  1067. /* ignore requests to set edid when overridden */
  1068. if (connector->override_edid)
  1069. return 0;
  1070. if (edid)
  1071. size = EDID_LENGTH * (1 + edid->extensions);
  1072. /* Set the display info, using edid if available, otherwise
  1073. * reseting the values to defaults. This duplicates the work
  1074. * done in drm_add_edid_modes, but that function is not
  1075. * consistently called before this one in all drivers and the
  1076. * computation is cheap enough that it seems better to
  1077. * duplicate it rather than attempt to ensure some arbitrary
  1078. * ordering of calls.
  1079. */
  1080. if (edid)
  1081. drm_add_display_info(connector, edid);
  1082. else
  1083. drm_reset_display_info(connector);
  1084. drm_object_property_set_value(&connector->base,
  1085. dev->mode_config.non_desktop_property,
  1086. connector->display_info.non_desktop);
  1087. ret = drm_property_replace_global_blob(dev,
  1088. &connector->edid_blob_ptr,
  1089. size,
  1090. edid,
  1091. &connector->base,
  1092. dev->mode_config.edid_property);
  1093. return ret;
  1094. }
  1095. EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
  1096. /**
  1097. * drm_mode_connector_set_link_status_property - Set link status property of a connector
  1098. * @connector: drm connector
  1099. * @link_status: new value of link status property (0: Good, 1: Bad)
  1100. *
  1101. * In usual working scenario, this link status property will always be set to
  1102. * "GOOD". If something fails during or after a mode set, the kernel driver
  1103. * may set this link status property to "BAD". The caller then needs to send a
  1104. * hotplug uevent for userspace to re-check the valid modes through
  1105. * GET_CONNECTOR_IOCTL and retry modeset.
  1106. *
  1107. * Note: Drivers cannot rely on userspace to support this property and
  1108. * issue a modeset. As such, they may choose to handle issues (like
  1109. * re-training a link) without userspace's intervention.
  1110. *
  1111. * The reason for adding this property is to handle link training failures, but
  1112. * it is not limited to DP or link training. For example, if we implement
  1113. * asynchronous setcrtc, this property can be used to report any failures in that.
  1114. */
  1115. void drm_mode_connector_set_link_status_property(struct drm_connector *connector,
  1116. uint64_t link_status)
  1117. {
  1118. struct drm_device *dev = connector->dev;
  1119. drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
  1120. connector->state->link_status = link_status;
  1121. drm_modeset_unlock(&dev->mode_config.connection_mutex);
  1122. }
  1123. EXPORT_SYMBOL(drm_mode_connector_set_link_status_property);
  1124. int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
  1125. struct drm_property *property,
  1126. uint64_t value)
  1127. {
  1128. int ret = -EINVAL;
  1129. struct drm_connector *connector = obj_to_connector(obj);
  1130. /* Do DPMS ourselves */
  1131. if (property == connector->dev->mode_config.dpms_property) {
  1132. ret = (*connector->funcs->dpms)(connector, (int)value);
  1133. } else if (connector->funcs->set_property)
  1134. ret = connector->funcs->set_property(connector, property, value);
  1135. if (!ret)
  1136. drm_object_property_set_value(&connector->base, property, value);
  1137. return ret;
  1138. }
  1139. int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
  1140. void *data, struct drm_file *file_priv)
  1141. {
  1142. struct drm_mode_connector_set_property *conn_set_prop = data;
  1143. struct drm_mode_obj_set_property obj_set_prop = {
  1144. .value = conn_set_prop->value,
  1145. .prop_id = conn_set_prop->prop_id,
  1146. .obj_id = conn_set_prop->connector_id,
  1147. .obj_type = DRM_MODE_OBJECT_CONNECTOR
  1148. };
  1149. /* It does all the locking and checking we need */
  1150. return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
  1151. }
  1152. static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
  1153. {
  1154. /* For atomic drivers only state objects are synchronously updated and
  1155. * protected by modeset locks, so check those first. */
  1156. if (connector->state)
  1157. return connector->state->best_encoder;
  1158. return connector->encoder;
  1159. }
  1160. static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
  1161. const struct drm_file *file_priv)
  1162. {
  1163. /*
  1164. * If user-space hasn't configured the driver to expose the stereo 3D
  1165. * modes, don't expose them.
  1166. */
  1167. if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
  1168. return false;
  1169. return true;
  1170. }
  1171. int drm_mode_getconnector(struct drm_device *dev, void *data,
  1172. struct drm_file *file_priv)
  1173. {
  1174. struct drm_mode_get_connector *out_resp = data;
  1175. struct drm_connector *connector;
  1176. struct drm_encoder *encoder;
  1177. struct drm_display_mode *mode;
  1178. int mode_count = 0;
  1179. int encoders_count = 0;
  1180. int ret = 0;
  1181. int copied = 0;
  1182. int i;
  1183. struct drm_mode_modeinfo u_mode;
  1184. struct drm_mode_modeinfo __user *mode_ptr;
  1185. uint32_t __user *encoder_ptr;
  1186. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1187. return -EINVAL;
  1188. memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
  1189. connector = drm_connector_lookup(dev, file_priv, out_resp->connector_id);
  1190. if (!connector)
  1191. return -ENOENT;
  1192. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
  1193. if (connector->encoder_ids[i] != 0)
  1194. encoders_count++;
  1195. if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
  1196. copied = 0;
  1197. encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
  1198. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  1199. if (connector->encoder_ids[i] != 0) {
  1200. if (put_user(connector->encoder_ids[i],
  1201. encoder_ptr + copied)) {
  1202. ret = -EFAULT;
  1203. goto out;
  1204. }
  1205. copied++;
  1206. }
  1207. }
  1208. }
  1209. out_resp->count_encoders = encoders_count;
  1210. out_resp->connector_id = connector->base.id;
  1211. out_resp->connector_type = connector->connector_type;
  1212. out_resp->connector_type_id = connector->connector_type_id;
  1213. mutex_lock(&dev->mode_config.mutex);
  1214. if (out_resp->count_modes == 0) {
  1215. connector->funcs->fill_modes(connector,
  1216. dev->mode_config.max_width,
  1217. dev->mode_config.max_height);
  1218. }
  1219. out_resp->mm_width = connector->display_info.width_mm;
  1220. out_resp->mm_height = connector->display_info.height_mm;
  1221. out_resp->subpixel = connector->display_info.subpixel_order;
  1222. out_resp->connection = connector->status;
  1223. /* delayed so we get modes regardless of pre-fill_modes state */
  1224. list_for_each_entry(mode, &connector->modes, head)
  1225. if (drm_mode_expose_to_userspace(mode, file_priv))
  1226. mode_count++;
  1227. /*
  1228. * This ioctl is called twice, once to determine how much space is
  1229. * needed, and the 2nd time to fill it.
  1230. */
  1231. if ((out_resp->count_modes >= mode_count) && mode_count) {
  1232. copied = 0;
  1233. mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
  1234. list_for_each_entry(mode, &connector->modes, head) {
  1235. if (!drm_mode_expose_to_userspace(mode, file_priv))
  1236. continue;
  1237. drm_mode_convert_to_umode(&u_mode, mode);
  1238. if (copy_to_user(mode_ptr + copied,
  1239. &u_mode, sizeof(u_mode))) {
  1240. ret = -EFAULT;
  1241. mutex_unlock(&dev->mode_config.mutex);
  1242. goto out;
  1243. }
  1244. copied++;
  1245. }
  1246. }
  1247. out_resp->count_modes = mode_count;
  1248. mutex_unlock(&dev->mode_config.mutex);
  1249. drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
  1250. encoder = drm_connector_get_encoder(connector);
  1251. if (encoder)
  1252. out_resp->encoder_id = encoder->base.id;
  1253. else
  1254. out_resp->encoder_id = 0;
  1255. /* Only grab properties after probing, to make sure EDID and other
  1256. * properties reflect the latest status. */
  1257. ret = drm_mode_object_get_properties(&connector->base, file_priv->atomic,
  1258. (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
  1259. (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
  1260. &out_resp->count_props);
  1261. drm_modeset_unlock(&dev->mode_config.connection_mutex);
  1262. out:
  1263. drm_connector_put(connector);
  1264. return ret;
  1265. }
  1266. /**
  1267. * DOC: Tile group
  1268. *
  1269. * Tile groups are used to represent tiled monitors with a unique integer
  1270. * identifier. Tiled monitors using DisplayID v1.3 have a unique 8-byte handle,
  1271. * we store this in a tile group, so we have a common identifier for all tiles
  1272. * in a monitor group. The property is called "TILE". Drivers can manage tile
  1273. * groups using drm_mode_create_tile_group(), drm_mode_put_tile_group() and
  1274. * drm_mode_get_tile_group(). But this is only needed for internal panels where
  1275. * the tile group information is exposed through a non-standard way.
  1276. */
  1277. static void drm_tile_group_free(struct kref *kref)
  1278. {
  1279. struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
  1280. struct drm_device *dev = tg->dev;
  1281. mutex_lock(&dev->mode_config.idr_mutex);
  1282. idr_remove(&dev->mode_config.tile_idr, tg->id);
  1283. mutex_unlock(&dev->mode_config.idr_mutex);
  1284. kfree(tg);
  1285. }
  1286. /**
  1287. * drm_mode_put_tile_group - drop a reference to a tile group.
  1288. * @dev: DRM device
  1289. * @tg: tile group to drop reference to.
  1290. *
  1291. * drop reference to tile group and free if 0.
  1292. */
  1293. void drm_mode_put_tile_group(struct drm_device *dev,
  1294. struct drm_tile_group *tg)
  1295. {
  1296. kref_put(&tg->refcount, drm_tile_group_free);
  1297. }
  1298. EXPORT_SYMBOL(drm_mode_put_tile_group);
  1299. /**
  1300. * drm_mode_get_tile_group - get a reference to an existing tile group
  1301. * @dev: DRM device
  1302. * @topology: 8-bytes unique per monitor.
  1303. *
  1304. * Use the unique bytes to get a reference to an existing tile group.
  1305. *
  1306. * RETURNS:
  1307. * tile group or NULL if not found.
  1308. */
  1309. struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
  1310. char topology[8])
  1311. {
  1312. struct drm_tile_group *tg;
  1313. int id;
  1314. mutex_lock(&dev->mode_config.idr_mutex);
  1315. idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
  1316. if (!memcmp(tg->group_data, topology, 8)) {
  1317. if (!kref_get_unless_zero(&tg->refcount))
  1318. tg = NULL;
  1319. mutex_unlock(&dev->mode_config.idr_mutex);
  1320. return tg;
  1321. }
  1322. }
  1323. mutex_unlock(&dev->mode_config.idr_mutex);
  1324. return NULL;
  1325. }
  1326. EXPORT_SYMBOL(drm_mode_get_tile_group);
  1327. /**
  1328. * drm_mode_create_tile_group - create a tile group from a displayid description
  1329. * @dev: DRM device
  1330. * @topology: 8-bytes unique per monitor.
  1331. *
  1332. * Create a tile group for the unique monitor, and get a unique
  1333. * identifier for the tile group.
  1334. *
  1335. * RETURNS:
  1336. * new tile group or error.
  1337. */
  1338. struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
  1339. char topology[8])
  1340. {
  1341. struct drm_tile_group *tg;
  1342. int ret;
  1343. tg = kzalloc(sizeof(*tg), GFP_KERNEL);
  1344. if (!tg)
  1345. return ERR_PTR(-ENOMEM);
  1346. kref_init(&tg->refcount);
  1347. memcpy(tg->group_data, topology, 8);
  1348. tg->dev = dev;
  1349. mutex_lock(&dev->mode_config.idr_mutex);
  1350. ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
  1351. if (ret >= 0) {
  1352. tg->id = ret;
  1353. } else {
  1354. kfree(tg);
  1355. tg = ERR_PTR(ret);
  1356. }
  1357. mutex_unlock(&dev->mode_config.idr_mutex);
  1358. return tg;
  1359. }
  1360. EXPORT_SYMBOL(drm_mode_create_tile_group);