drm_connector.c 57 KB

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