drm_connector.c 52 KB

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