drm_connector.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271
  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_crtc_internal.h"
  26. #include "drm_internal.h"
  27. /**
  28. * DOC: overview
  29. *
  30. * In DRM connectors are the general abstraction for display sinks, and include
  31. * als fixed panels or anything else that can display pixels in some form. As
  32. * opposed to all other KMS objects representing hardware (like CRTC, encoder or
  33. * plane abstractions) connectors can be hotplugged and unplugged at runtime.
  34. * Hence they are reference-counted using drm_connector_reference() and
  35. * drm_connector_unreference().
  36. *
  37. * KMS driver must create, initialize, register and attach at a struct
  38. * &drm_connector for each such sink. The instance is created as other KMS
  39. * objects and initialized by setting the following fields.
  40. *
  41. * The connector is then registered with a call to drm_connector_init() with a
  42. * pointer to the connector functions and a connector type, and exposed through
  43. * sysfs with a call to drm_connector_register().
  44. *
  45. * Connectors must be attached to an encoder to be used. For devices that map
  46. * connectors to encoders 1:1, the connector should be attached at
  47. * initialization time with a call to drm_mode_connector_attach_encoder(). The
  48. * driver must also set the struct &drm_connector encoder field to point to the
  49. * attached encoder.
  50. *
  51. * For connectors which are not fixed (like built-in panels) the driver needs to
  52. * support hotplug notifications. The simplest way to do that is by using the
  53. * probe helpers, see drm_kms_helper_poll_init() for connectors which don't have
  54. * hardware support for hotplug interrupts. Connectors with hardware hotplug
  55. * support can instead use e.g. drm_helper_hpd_irq_event().
  56. */
  57. struct drm_conn_prop_enum_list {
  58. int type;
  59. const char *name;
  60. struct ida ida;
  61. };
  62. /*
  63. * Connector and encoder types.
  64. */
  65. static struct drm_conn_prop_enum_list drm_connector_enum_list[] = {
  66. { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
  67. { DRM_MODE_CONNECTOR_VGA, "VGA" },
  68. { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
  69. { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
  70. { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
  71. { DRM_MODE_CONNECTOR_Composite, "Composite" },
  72. { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
  73. { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
  74. { DRM_MODE_CONNECTOR_Component, "Component" },
  75. { DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
  76. { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
  77. { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
  78. { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
  79. { DRM_MODE_CONNECTOR_TV, "TV" },
  80. { DRM_MODE_CONNECTOR_eDP, "eDP" },
  81. { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
  82. { DRM_MODE_CONNECTOR_DSI, "DSI" },
  83. { DRM_MODE_CONNECTOR_DPI, "DPI" },
  84. };
  85. void drm_connector_ida_init(void)
  86. {
  87. int i;
  88. for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
  89. ida_init(&drm_connector_enum_list[i].ida);
  90. }
  91. void drm_connector_ida_destroy(void)
  92. {
  93. int i;
  94. for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
  95. ida_destroy(&drm_connector_enum_list[i].ida);
  96. }
  97. /**
  98. * drm_connector_get_cmdline_mode - reads the user's cmdline mode
  99. * @connector: connector to quwery
  100. *
  101. * The kernel supports per-connector configuration of its consoles through
  102. * use of the video= parameter. This function parses that option and
  103. * extracts the user's specified mode (or enable/disable status) for a
  104. * particular connector. This is typically only used during the early fbdev
  105. * setup.
  106. */
  107. static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
  108. {
  109. struct drm_cmdline_mode *mode = &connector->cmdline_mode;
  110. char *option = NULL;
  111. if (fb_get_options(connector->name, &option))
  112. return;
  113. if (!drm_mode_parse_command_line_for_connector(option,
  114. connector,
  115. mode))
  116. return;
  117. if (mode->force) {
  118. const char *s;
  119. switch (mode->force) {
  120. case DRM_FORCE_OFF:
  121. s = "OFF";
  122. break;
  123. case DRM_FORCE_ON_DIGITAL:
  124. s = "ON - dig";
  125. break;
  126. default:
  127. case DRM_FORCE_ON:
  128. s = "ON";
  129. break;
  130. }
  131. DRM_INFO("forcing %s connector %s\n", connector->name, s);
  132. connector->force = mode->force;
  133. }
  134. DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
  135. connector->name,
  136. mode->xres, mode->yres,
  137. mode->refresh_specified ? mode->refresh : 60,
  138. mode->rb ? " reduced blanking" : "",
  139. mode->margins ? " with margins" : "",
  140. mode->interlace ? " interlaced" : "");
  141. }
  142. static void drm_connector_free(struct kref *kref)
  143. {
  144. struct drm_connector *connector =
  145. container_of(kref, struct drm_connector, base.refcount);
  146. struct drm_device *dev = connector->dev;
  147. drm_mode_object_unregister(dev, &connector->base);
  148. connector->funcs->destroy(connector);
  149. }
  150. /**
  151. * drm_connector_init - Init a preallocated connector
  152. * @dev: DRM device
  153. * @connector: the connector to init
  154. * @funcs: callbacks for this connector
  155. * @connector_type: user visible type of the connector
  156. *
  157. * Initialises a preallocated connector. Connectors should be
  158. * subclassed as part of driver connector objects.
  159. *
  160. * Returns:
  161. * Zero on success, error code on failure.
  162. */
  163. int drm_connector_init(struct drm_device *dev,
  164. struct drm_connector *connector,
  165. const struct drm_connector_funcs *funcs,
  166. int connector_type)
  167. {
  168. struct drm_mode_config *config = &dev->mode_config;
  169. int ret;
  170. struct ida *connector_ida =
  171. &drm_connector_enum_list[connector_type].ida;
  172. drm_modeset_lock_all(dev);
  173. ret = drm_mode_object_get_reg(dev, &connector->base,
  174. DRM_MODE_OBJECT_CONNECTOR,
  175. false, drm_connector_free);
  176. if (ret)
  177. goto out_unlock;
  178. connector->base.properties = &connector->properties;
  179. connector->dev = dev;
  180. connector->funcs = funcs;
  181. ret = ida_simple_get(&config->connector_ida, 0, 0, GFP_KERNEL);
  182. if (ret < 0)
  183. goto out_put;
  184. connector->index = ret;
  185. ret = 0;
  186. connector->connector_type = connector_type;
  187. connector->connector_type_id =
  188. ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
  189. if (connector->connector_type_id < 0) {
  190. ret = connector->connector_type_id;
  191. goto out_put_id;
  192. }
  193. connector->name =
  194. kasprintf(GFP_KERNEL, "%s-%d",
  195. drm_connector_enum_list[connector_type].name,
  196. connector->connector_type_id);
  197. if (!connector->name) {
  198. ret = -ENOMEM;
  199. goto out_put_type_id;
  200. }
  201. INIT_LIST_HEAD(&connector->probed_modes);
  202. INIT_LIST_HEAD(&connector->modes);
  203. connector->edid_blob_ptr = NULL;
  204. connector->status = connector_status_unknown;
  205. drm_connector_get_cmdline_mode(connector);
  206. /* We should add connectors at the end to avoid upsetting the connector
  207. * index too much. */
  208. list_add_tail(&connector->head, &config->connector_list);
  209. config->num_connector++;
  210. if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
  211. drm_object_attach_property(&connector->base,
  212. config->edid_property,
  213. 0);
  214. drm_object_attach_property(&connector->base,
  215. config->dpms_property, 0);
  216. if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
  217. drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
  218. }
  219. connector->debugfs_entry = NULL;
  220. out_put_type_id:
  221. if (ret)
  222. ida_simple_remove(connector_ida, connector->connector_type_id);
  223. out_put_id:
  224. if (ret)
  225. ida_simple_remove(&config->connector_ida, connector->index);
  226. out_put:
  227. if (ret)
  228. drm_mode_object_unregister(dev, &connector->base);
  229. out_unlock:
  230. drm_modeset_unlock_all(dev);
  231. return ret;
  232. }
  233. EXPORT_SYMBOL(drm_connector_init);
  234. /**
  235. * drm_mode_connector_attach_encoder - attach a connector to an encoder
  236. * @connector: connector to attach
  237. * @encoder: encoder to attach @connector to
  238. *
  239. * This function links up a connector to an encoder. Note that the routing
  240. * restrictions between encoders and crtcs are exposed to userspace through the
  241. * possible_clones and possible_crtcs bitmasks.
  242. *
  243. * Returns:
  244. * Zero on success, negative errno on failure.
  245. */
  246. int drm_mode_connector_attach_encoder(struct drm_connector *connector,
  247. struct drm_encoder *encoder)
  248. {
  249. int i;
  250. /*
  251. * In the past, drivers have attempted to model the static association
  252. * of connector to encoder in simple connector/encoder devices using a
  253. * direct assignment of connector->encoder = encoder. This connection
  254. * is a logical one and the responsibility of the core, so drivers are
  255. * expected not to mess with this.
  256. *
  257. * Note that the error return should've been enough here, but a large
  258. * majority of drivers ignores the return value, so add in a big WARN
  259. * to get people's attention.
  260. */
  261. if (WARN_ON(connector->encoder))
  262. return -EINVAL;
  263. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  264. if (connector->encoder_ids[i] == 0) {
  265. connector->encoder_ids[i] = encoder->base.id;
  266. return 0;
  267. }
  268. }
  269. return -ENOMEM;
  270. }
  271. EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
  272. static void drm_mode_remove(struct drm_connector *connector,
  273. struct drm_display_mode *mode)
  274. {
  275. list_del(&mode->head);
  276. drm_mode_destroy(connector->dev, mode);
  277. }
  278. /**
  279. * drm_connector_cleanup - cleans up an initialised connector
  280. * @connector: connector to cleanup
  281. *
  282. * Cleans up the connector but doesn't free the object.
  283. */
  284. void drm_connector_cleanup(struct drm_connector *connector)
  285. {
  286. struct drm_device *dev = connector->dev;
  287. struct drm_display_mode *mode, *t;
  288. /* The connector should have been removed from userspace long before
  289. * it is finally destroyed.
  290. */
  291. if (WARN_ON(connector->registered))
  292. drm_connector_unregister(connector);
  293. if (connector->tile_group) {
  294. drm_mode_put_tile_group(dev, connector->tile_group);
  295. connector->tile_group = NULL;
  296. }
  297. list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
  298. drm_mode_remove(connector, mode);
  299. list_for_each_entry_safe(mode, t, &connector->modes, head)
  300. drm_mode_remove(connector, mode);
  301. ida_simple_remove(&drm_connector_enum_list[connector->connector_type].ida,
  302. connector->connector_type_id);
  303. ida_simple_remove(&dev->mode_config.connector_ida,
  304. connector->index);
  305. kfree(connector->display_info.bus_formats);
  306. drm_mode_object_unregister(dev, &connector->base);
  307. kfree(connector->name);
  308. connector->name = NULL;
  309. list_del(&connector->head);
  310. dev->mode_config.num_connector--;
  311. WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
  312. if (connector->state && connector->funcs->atomic_destroy_state)
  313. connector->funcs->atomic_destroy_state(connector,
  314. connector->state);
  315. memset(connector, 0, sizeof(*connector));
  316. }
  317. EXPORT_SYMBOL(drm_connector_cleanup);
  318. /**
  319. * drm_connector_register - register a connector
  320. * @connector: the connector to register
  321. *
  322. * Register userspace interfaces for a connector
  323. *
  324. * Returns:
  325. * Zero on success, error code on failure.
  326. */
  327. int drm_connector_register(struct drm_connector *connector)
  328. {
  329. int ret;
  330. if (connector->registered)
  331. return 0;
  332. ret = drm_sysfs_connector_add(connector);
  333. if (ret)
  334. return ret;
  335. ret = drm_debugfs_connector_add(connector);
  336. if (ret) {
  337. goto err_sysfs;
  338. }
  339. if (connector->funcs->late_register) {
  340. ret = connector->funcs->late_register(connector);
  341. if (ret)
  342. goto err_debugfs;
  343. }
  344. drm_mode_object_register(connector->dev, &connector->base);
  345. connector->registered = true;
  346. return 0;
  347. err_debugfs:
  348. drm_debugfs_connector_remove(connector);
  349. err_sysfs:
  350. drm_sysfs_connector_remove(connector);
  351. return ret;
  352. }
  353. EXPORT_SYMBOL(drm_connector_register);
  354. /**
  355. * drm_connector_unregister - unregister a connector
  356. * @connector: the connector to unregister
  357. *
  358. * Unregister userspace interfaces for a connector
  359. */
  360. void drm_connector_unregister(struct drm_connector *connector)
  361. {
  362. if (!connector->registered)
  363. return;
  364. if (connector->funcs->early_unregister)
  365. connector->funcs->early_unregister(connector);
  366. drm_sysfs_connector_remove(connector);
  367. drm_debugfs_connector_remove(connector);
  368. connector->registered = false;
  369. }
  370. EXPORT_SYMBOL(drm_connector_unregister);
  371. void drm_connector_unregister_all(struct drm_device *dev)
  372. {
  373. struct drm_connector *connector;
  374. /* FIXME: taking the mode config mutex ends up in a clash with sysfs */
  375. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  376. drm_connector_unregister(connector);
  377. }
  378. int drm_connector_register_all(struct drm_device *dev)
  379. {
  380. struct drm_connector *connector;
  381. int ret;
  382. /* FIXME: taking the mode config mutex ends up in a clash with
  383. * fbcon/backlight registration */
  384. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  385. ret = drm_connector_register(connector);
  386. if (ret)
  387. goto err;
  388. }
  389. return 0;
  390. err:
  391. mutex_unlock(&dev->mode_config.mutex);
  392. drm_connector_unregister_all(dev);
  393. return ret;
  394. }
  395. /**
  396. * drm_get_connector_status_name - return a string for connector status
  397. * @status: connector status to compute name of
  398. *
  399. * In contrast to the other drm_get_*_name functions this one here returns a
  400. * const pointer and hence is threadsafe.
  401. */
  402. const char *drm_get_connector_status_name(enum drm_connector_status status)
  403. {
  404. if (status == connector_status_connected)
  405. return "connected";
  406. else if (status == connector_status_disconnected)
  407. return "disconnected";
  408. else
  409. return "unknown";
  410. }
  411. EXPORT_SYMBOL(drm_get_connector_status_name);
  412. static const struct drm_prop_enum_list drm_subpixel_enum_list[] = {
  413. { SubPixelUnknown, "Unknown" },
  414. { SubPixelHorizontalRGB, "Horizontal RGB" },
  415. { SubPixelHorizontalBGR, "Horizontal BGR" },
  416. { SubPixelVerticalRGB, "Vertical RGB" },
  417. { SubPixelVerticalBGR, "Vertical BGR" },
  418. { SubPixelNone, "None" },
  419. };
  420. /**
  421. * drm_get_subpixel_order_name - return a string for a given subpixel enum
  422. * @order: enum of subpixel_order
  423. *
  424. * Note you could abuse this and return something out of bounds, but that
  425. * would be a caller error. No unscrubbed user data should make it here.
  426. */
  427. const char *drm_get_subpixel_order_name(enum subpixel_order order)
  428. {
  429. return drm_subpixel_enum_list[order].name;
  430. }
  431. EXPORT_SYMBOL(drm_get_subpixel_order_name);
  432. static const struct drm_prop_enum_list drm_dpms_enum_list[] = {
  433. { DRM_MODE_DPMS_ON, "On" },
  434. { DRM_MODE_DPMS_STANDBY, "Standby" },
  435. { DRM_MODE_DPMS_SUSPEND, "Suspend" },
  436. { DRM_MODE_DPMS_OFF, "Off" }
  437. };
  438. DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
  439. /**
  440. * drm_display_info_set_bus_formats - set the supported bus formats
  441. * @info: display info to store bus formats in
  442. * @formats: array containing the supported bus formats
  443. * @num_formats: the number of entries in the fmts array
  444. *
  445. * Store the supported bus formats in display info structure.
  446. * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
  447. * a full list of available formats.
  448. */
  449. int drm_display_info_set_bus_formats(struct drm_display_info *info,
  450. const u32 *formats,
  451. unsigned int num_formats)
  452. {
  453. u32 *fmts = NULL;
  454. if (!formats && num_formats)
  455. return -EINVAL;
  456. if (formats && num_formats) {
  457. fmts = kmemdup(formats, sizeof(*formats) * num_formats,
  458. GFP_KERNEL);
  459. if (!fmts)
  460. return -ENOMEM;
  461. }
  462. kfree(info->bus_formats);
  463. info->bus_formats = fmts;
  464. info->num_bus_formats = num_formats;
  465. return 0;
  466. }
  467. EXPORT_SYMBOL(drm_display_info_set_bus_formats);
  468. /* Optional connector properties. */
  469. static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] = {
  470. { DRM_MODE_SCALE_NONE, "None" },
  471. { DRM_MODE_SCALE_FULLSCREEN, "Full" },
  472. { DRM_MODE_SCALE_CENTER, "Center" },
  473. { DRM_MODE_SCALE_ASPECT, "Full aspect" },
  474. };
  475. static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
  476. { DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
  477. { DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
  478. { DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
  479. };
  480. static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] = {
  481. { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
  482. { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
  483. { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
  484. };
  485. DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
  486. static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] = {
  487. { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
  488. { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
  489. { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
  490. };
  491. DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
  492. drm_dvi_i_subconnector_enum_list)
  493. static const struct drm_prop_enum_list drm_tv_select_enum_list[] = {
  494. { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
  495. { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
  496. { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
  497. { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
  498. { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
  499. };
  500. DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
  501. static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] = {
  502. { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
  503. { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
  504. { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
  505. { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
  506. { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
  507. };
  508. DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
  509. drm_tv_subconnector_enum_list)
  510. /**
  511. * DOC: standard connector properties
  512. *
  513. * DRM connectors have a few standardized properties:
  514. *
  515. * EDID:
  516. * Blob property which contains the current EDID read from the sink. This
  517. * is useful to parse sink identification information like vendor, model
  518. * and serial. Drivers should update this property by calling
  519. * drm_mode_connector_update_edid_property(), usually after having parsed
  520. * the EDID using drm_add_edid_modes(). Userspace cannot change this
  521. * property.
  522. * DPMS:
  523. * Legacy property for setting the power state of the connector. For atomic
  524. * drivers this is only provided for backwards compatibility with existing
  525. * drivers, it remaps to controlling the "ACTIVE" property on the CRTC the
  526. * connector is linked to. Drivers should never set this property directly,
  527. * it is handled by the DRM core by calling the ->dpms() callback in
  528. * &drm_connector_funcs. Atomic drivers should implement this hook using
  529. * drm_atomic_helper_connector_dpms(). This is the only property standard
  530. * connector property that userspace can change.
  531. * PATH:
  532. * Connector path property to identify how this sink is physically
  533. * connected. Used by DP MST. This should be set by calling
  534. * drm_mode_connector_set_path_property(), in the case of DP MST with the
  535. * path property the MST manager created. Userspace cannot change this
  536. * property.
  537. * TILE:
  538. * Connector tile group property to indicate how a set of DRM connector
  539. * compose together into one logical screen. This is used by both high-res
  540. * external screens (often only using a single cable, but exposing multiple
  541. * DP MST sinks), or high-res integrated panels (like dual-link DSI) which
  542. * are not gen-locked. Note that for tiled panels which are genlocked, like
  543. * dual-link LVDS or dual-link DSI, the driver should try to not expose the
  544. * tiling and virtualize both &drm_crtc and &drm_plane if needed. Drivers
  545. * should update this value using drm_mode_connector_set_tile_property().
  546. * Userspace cannot change this property.
  547. *
  548. * Connectors also have one standardized atomic property:
  549. *
  550. * CRTC_ID:
  551. * Mode object ID of the &drm_crtc this connector should be connected to.
  552. */
  553. int drm_connector_create_standard_properties(struct drm_device *dev)
  554. {
  555. struct drm_property *prop;
  556. prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
  557. DRM_MODE_PROP_IMMUTABLE,
  558. "EDID", 0);
  559. if (!prop)
  560. return -ENOMEM;
  561. dev->mode_config.edid_property = prop;
  562. prop = drm_property_create_enum(dev, 0,
  563. "DPMS", drm_dpms_enum_list,
  564. ARRAY_SIZE(drm_dpms_enum_list));
  565. if (!prop)
  566. return -ENOMEM;
  567. dev->mode_config.dpms_property = prop;
  568. prop = drm_property_create(dev,
  569. DRM_MODE_PROP_BLOB |
  570. DRM_MODE_PROP_IMMUTABLE,
  571. "PATH", 0);
  572. if (!prop)
  573. return -ENOMEM;
  574. dev->mode_config.path_property = prop;
  575. prop = drm_property_create(dev,
  576. DRM_MODE_PROP_BLOB |
  577. DRM_MODE_PROP_IMMUTABLE,
  578. "TILE", 0);
  579. if (!prop)
  580. return -ENOMEM;
  581. dev->mode_config.tile_property = prop;
  582. return 0;
  583. }
  584. /**
  585. * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
  586. * @dev: DRM device
  587. *
  588. * Called by a driver the first time a DVI-I connector is made.
  589. */
  590. int drm_mode_create_dvi_i_properties(struct drm_device *dev)
  591. {
  592. struct drm_property *dvi_i_selector;
  593. struct drm_property *dvi_i_subconnector;
  594. if (dev->mode_config.dvi_i_select_subconnector_property)
  595. return 0;
  596. dvi_i_selector =
  597. drm_property_create_enum(dev, 0,
  598. "select subconnector",
  599. drm_dvi_i_select_enum_list,
  600. ARRAY_SIZE(drm_dvi_i_select_enum_list));
  601. dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
  602. dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  603. "subconnector",
  604. drm_dvi_i_subconnector_enum_list,
  605. ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
  606. dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
  607. return 0;
  608. }
  609. EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
  610. /**
  611. * drm_create_tv_properties - create TV specific connector properties
  612. * @dev: DRM device
  613. * @num_modes: number of different TV formats (modes) supported
  614. * @modes: array of pointers to strings containing name of each format
  615. *
  616. * Called by a driver's TV initialization routine, this function creates
  617. * the TV specific connector properties for a given device. Caller is
  618. * responsible for allocating a list of format names and passing them to
  619. * this routine.
  620. */
  621. int drm_mode_create_tv_properties(struct drm_device *dev,
  622. unsigned int num_modes,
  623. const char * const modes[])
  624. {
  625. struct drm_property *tv_selector;
  626. struct drm_property *tv_subconnector;
  627. unsigned int i;
  628. if (dev->mode_config.tv_select_subconnector_property)
  629. return 0;
  630. /*
  631. * Basic connector properties
  632. */
  633. tv_selector = drm_property_create_enum(dev, 0,
  634. "select subconnector",
  635. drm_tv_select_enum_list,
  636. ARRAY_SIZE(drm_tv_select_enum_list));
  637. if (!tv_selector)
  638. goto nomem;
  639. dev->mode_config.tv_select_subconnector_property = tv_selector;
  640. tv_subconnector =
  641. drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  642. "subconnector",
  643. drm_tv_subconnector_enum_list,
  644. ARRAY_SIZE(drm_tv_subconnector_enum_list));
  645. if (!tv_subconnector)
  646. goto nomem;
  647. dev->mode_config.tv_subconnector_property = tv_subconnector;
  648. /*
  649. * Other, TV specific properties: margins & TV modes.
  650. */
  651. dev->mode_config.tv_left_margin_property =
  652. drm_property_create_range(dev, 0, "left margin", 0, 100);
  653. if (!dev->mode_config.tv_left_margin_property)
  654. goto nomem;
  655. dev->mode_config.tv_right_margin_property =
  656. drm_property_create_range(dev, 0, "right margin", 0, 100);
  657. if (!dev->mode_config.tv_right_margin_property)
  658. goto nomem;
  659. dev->mode_config.tv_top_margin_property =
  660. drm_property_create_range(dev, 0, "top margin", 0, 100);
  661. if (!dev->mode_config.tv_top_margin_property)
  662. goto nomem;
  663. dev->mode_config.tv_bottom_margin_property =
  664. drm_property_create_range(dev, 0, "bottom margin", 0, 100);
  665. if (!dev->mode_config.tv_bottom_margin_property)
  666. goto nomem;
  667. dev->mode_config.tv_mode_property =
  668. drm_property_create(dev, DRM_MODE_PROP_ENUM,
  669. "mode", num_modes);
  670. if (!dev->mode_config.tv_mode_property)
  671. goto nomem;
  672. for (i = 0; i < num_modes; i++)
  673. drm_property_add_enum(dev->mode_config.tv_mode_property, i,
  674. i, modes[i]);
  675. dev->mode_config.tv_brightness_property =
  676. drm_property_create_range(dev, 0, "brightness", 0, 100);
  677. if (!dev->mode_config.tv_brightness_property)
  678. goto nomem;
  679. dev->mode_config.tv_contrast_property =
  680. drm_property_create_range(dev, 0, "contrast", 0, 100);
  681. if (!dev->mode_config.tv_contrast_property)
  682. goto nomem;
  683. dev->mode_config.tv_flicker_reduction_property =
  684. drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
  685. if (!dev->mode_config.tv_flicker_reduction_property)
  686. goto nomem;
  687. dev->mode_config.tv_overscan_property =
  688. drm_property_create_range(dev, 0, "overscan", 0, 100);
  689. if (!dev->mode_config.tv_overscan_property)
  690. goto nomem;
  691. dev->mode_config.tv_saturation_property =
  692. drm_property_create_range(dev, 0, "saturation", 0, 100);
  693. if (!dev->mode_config.tv_saturation_property)
  694. goto nomem;
  695. dev->mode_config.tv_hue_property =
  696. drm_property_create_range(dev, 0, "hue", 0, 100);
  697. if (!dev->mode_config.tv_hue_property)
  698. goto nomem;
  699. return 0;
  700. nomem:
  701. return -ENOMEM;
  702. }
  703. EXPORT_SYMBOL(drm_mode_create_tv_properties);
  704. /**
  705. * drm_mode_create_scaling_mode_property - create scaling mode property
  706. * @dev: DRM device
  707. *
  708. * Called by a driver the first time it's needed, must be attached to desired
  709. * connectors.
  710. */
  711. int drm_mode_create_scaling_mode_property(struct drm_device *dev)
  712. {
  713. struct drm_property *scaling_mode;
  714. if (dev->mode_config.scaling_mode_property)
  715. return 0;
  716. scaling_mode =
  717. drm_property_create_enum(dev, 0, "scaling mode",
  718. drm_scaling_mode_enum_list,
  719. ARRAY_SIZE(drm_scaling_mode_enum_list));
  720. dev->mode_config.scaling_mode_property = scaling_mode;
  721. return 0;
  722. }
  723. EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
  724. /**
  725. * drm_mode_create_aspect_ratio_property - create aspect ratio property
  726. * @dev: DRM device
  727. *
  728. * Called by a driver the first time it's needed, must be attached to desired
  729. * connectors.
  730. *
  731. * Returns:
  732. * Zero on success, negative errno on failure.
  733. */
  734. int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
  735. {
  736. if (dev->mode_config.aspect_ratio_property)
  737. return 0;
  738. dev->mode_config.aspect_ratio_property =
  739. drm_property_create_enum(dev, 0, "aspect ratio",
  740. drm_aspect_ratio_enum_list,
  741. ARRAY_SIZE(drm_aspect_ratio_enum_list));
  742. if (dev->mode_config.aspect_ratio_property == NULL)
  743. return -ENOMEM;
  744. return 0;
  745. }
  746. EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
  747. /**
  748. * drm_mode_create_suggested_offset_properties - create suggests offset properties
  749. * @dev: DRM device
  750. *
  751. * Create the the suggested x/y offset property for connectors.
  752. */
  753. int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
  754. {
  755. if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
  756. return 0;
  757. dev->mode_config.suggested_x_property =
  758. drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
  759. dev->mode_config.suggested_y_property =
  760. drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
  761. if (dev->mode_config.suggested_x_property == NULL ||
  762. dev->mode_config.suggested_y_property == NULL)
  763. return -ENOMEM;
  764. return 0;
  765. }
  766. EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
  767. /**
  768. * drm_mode_connector_set_path_property - set tile property on connector
  769. * @connector: connector to set property on.
  770. * @path: path to use for property; must not be NULL.
  771. *
  772. * This creates a property to expose to userspace to specify a
  773. * connector path. This is mainly used for DisplayPort MST where
  774. * connectors have a topology and we want to allow userspace to give
  775. * them more meaningful names.
  776. *
  777. * Returns:
  778. * Zero on success, negative errno on failure.
  779. */
  780. int drm_mode_connector_set_path_property(struct drm_connector *connector,
  781. const char *path)
  782. {
  783. struct drm_device *dev = connector->dev;
  784. int ret;
  785. ret = drm_property_replace_global_blob(dev,
  786. &connector->path_blob_ptr,
  787. strlen(path) + 1,
  788. path,
  789. &connector->base,
  790. dev->mode_config.path_property);
  791. return ret;
  792. }
  793. EXPORT_SYMBOL(drm_mode_connector_set_path_property);
  794. /**
  795. * drm_mode_connector_set_tile_property - set tile property on connector
  796. * @connector: connector to set property on.
  797. *
  798. * This looks up the tile information for a connector, and creates a
  799. * property for userspace to parse if it exists. The property is of
  800. * the form of 8 integers using ':' as a separator.
  801. *
  802. * Returns:
  803. * Zero on success, errno on failure.
  804. */
  805. int drm_mode_connector_set_tile_property(struct drm_connector *connector)
  806. {
  807. struct drm_device *dev = connector->dev;
  808. char tile[256];
  809. int ret;
  810. if (!connector->has_tile) {
  811. ret = drm_property_replace_global_blob(dev,
  812. &connector->tile_blob_ptr,
  813. 0,
  814. NULL,
  815. &connector->base,
  816. dev->mode_config.tile_property);
  817. return ret;
  818. }
  819. snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
  820. connector->tile_group->id, connector->tile_is_single_monitor,
  821. connector->num_h_tile, connector->num_v_tile,
  822. connector->tile_h_loc, connector->tile_v_loc,
  823. connector->tile_h_size, connector->tile_v_size);
  824. ret = drm_property_replace_global_blob(dev,
  825. &connector->tile_blob_ptr,
  826. strlen(tile) + 1,
  827. tile,
  828. &connector->base,
  829. dev->mode_config.tile_property);
  830. return ret;
  831. }
  832. EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
  833. /**
  834. * drm_mode_connector_update_edid_property - update the edid property of a connector
  835. * @connector: drm connector
  836. * @edid: new value of the edid property
  837. *
  838. * This function creates a new blob modeset object and assigns its id to the
  839. * connector's edid property.
  840. *
  841. * Returns:
  842. * Zero on success, negative errno on failure.
  843. */
  844. int drm_mode_connector_update_edid_property(struct drm_connector *connector,
  845. const struct edid *edid)
  846. {
  847. struct drm_device *dev = connector->dev;
  848. size_t size = 0;
  849. int ret;
  850. /* ignore requests to set edid when overridden */
  851. if (connector->override_edid)
  852. return 0;
  853. if (edid)
  854. size = EDID_LENGTH * (1 + edid->extensions);
  855. ret = drm_property_replace_global_blob(dev,
  856. &connector->edid_blob_ptr,
  857. size,
  858. edid,
  859. &connector->base,
  860. dev->mode_config.edid_property);
  861. return ret;
  862. }
  863. EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
  864. int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
  865. struct drm_property *property,
  866. uint64_t value)
  867. {
  868. int ret = -EINVAL;
  869. struct drm_connector *connector = obj_to_connector(obj);
  870. /* Do DPMS ourselves */
  871. if (property == connector->dev->mode_config.dpms_property) {
  872. ret = (*connector->funcs->dpms)(connector, (int)value);
  873. } else if (connector->funcs->set_property)
  874. ret = connector->funcs->set_property(connector, property, value);
  875. /* store the property value if successful */
  876. if (!ret)
  877. drm_object_property_set_value(&connector->base, property, value);
  878. return ret;
  879. }
  880. int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
  881. void *data, struct drm_file *file_priv)
  882. {
  883. struct drm_mode_connector_set_property *conn_set_prop = data;
  884. struct drm_mode_obj_set_property obj_set_prop = {
  885. .value = conn_set_prop->value,
  886. .prop_id = conn_set_prop->prop_id,
  887. .obj_id = conn_set_prop->connector_id,
  888. .obj_type = DRM_MODE_OBJECT_CONNECTOR
  889. };
  890. /* It does all the locking and checking we need */
  891. return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
  892. }
  893. static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
  894. {
  895. /* For atomic drivers only state objects are synchronously updated and
  896. * protected by modeset locks, so check those first. */
  897. if (connector->state)
  898. return connector->state->best_encoder;
  899. return connector->encoder;
  900. }
  901. static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
  902. const struct drm_file *file_priv)
  903. {
  904. /*
  905. * If user-space hasn't configured the driver to expose the stereo 3D
  906. * modes, don't expose them.
  907. */
  908. if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
  909. return false;
  910. return true;
  911. }
  912. int drm_mode_getconnector(struct drm_device *dev, void *data,
  913. struct drm_file *file_priv)
  914. {
  915. struct drm_mode_get_connector *out_resp = data;
  916. struct drm_connector *connector;
  917. struct drm_encoder *encoder;
  918. struct drm_display_mode *mode;
  919. int mode_count = 0;
  920. int encoders_count = 0;
  921. int ret = 0;
  922. int copied = 0;
  923. int i;
  924. struct drm_mode_modeinfo u_mode;
  925. struct drm_mode_modeinfo __user *mode_ptr;
  926. uint32_t __user *encoder_ptr;
  927. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  928. return -EINVAL;
  929. memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
  930. mutex_lock(&dev->mode_config.mutex);
  931. connector = drm_connector_lookup(dev, out_resp->connector_id);
  932. if (!connector) {
  933. ret = -ENOENT;
  934. goto out_unlock;
  935. }
  936. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
  937. if (connector->encoder_ids[i] != 0)
  938. encoders_count++;
  939. if (out_resp->count_modes == 0) {
  940. connector->funcs->fill_modes(connector,
  941. dev->mode_config.max_width,
  942. dev->mode_config.max_height);
  943. }
  944. /* delayed so we get modes regardless of pre-fill_modes state */
  945. list_for_each_entry(mode, &connector->modes, head)
  946. if (drm_mode_expose_to_userspace(mode, file_priv))
  947. mode_count++;
  948. out_resp->connector_id = connector->base.id;
  949. out_resp->connector_type = connector->connector_type;
  950. out_resp->connector_type_id = connector->connector_type_id;
  951. out_resp->mm_width = connector->display_info.width_mm;
  952. out_resp->mm_height = connector->display_info.height_mm;
  953. out_resp->subpixel = connector->display_info.subpixel_order;
  954. out_resp->connection = connector->status;
  955. drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
  956. encoder = drm_connector_get_encoder(connector);
  957. if (encoder)
  958. out_resp->encoder_id = encoder->base.id;
  959. else
  960. out_resp->encoder_id = 0;
  961. /*
  962. * This ioctl is called twice, once to determine how much space is
  963. * needed, and the 2nd time to fill it.
  964. */
  965. if ((out_resp->count_modes >= mode_count) && mode_count) {
  966. copied = 0;
  967. mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
  968. list_for_each_entry(mode, &connector->modes, head) {
  969. if (!drm_mode_expose_to_userspace(mode, file_priv))
  970. continue;
  971. drm_mode_convert_to_umode(&u_mode, mode);
  972. if (copy_to_user(mode_ptr + copied,
  973. &u_mode, sizeof(u_mode))) {
  974. ret = -EFAULT;
  975. goto out;
  976. }
  977. copied++;
  978. }
  979. }
  980. out_resp->count_modes = mode_count;
  981. ret = drm_mode_object_get_properties(&connector->base, file_priv->atomic,
  982. (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
  983. (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
  984. &out_resp->count_props);
  985. if (ret)
  986. goto out;
  987. if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
  988. copied = 0;
  989. encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
  990. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  991. if (connector->encoder_ids[i] != 0) {
  992. if (put_user(connector->encoder_ids[i],
  993. encoder_ptr + copied)) {
  994. ret = -EFAULT;
  995. goto out;
  996. }
  997. copied++;
  998. }
  999. }
  1000. }
  1001. out_resp->count_encoders = encoders_count;
  1002. out:
  1003. drm_modeset_unlock(&dev->mode_config.connection_mutex);
  1004. drm_connector_unreference(connector);
  1005. out_unlock:
  1006. mutex_unlock(&dev->mode_config.mutex);
  1007. return ret;
  1008. }
  1009. /**
  1010. * DOC: Tile group
  1011. *
  1012. * Tile groups are used to represent tiled monitors with a unique integer
  1013. * identifier. Tiled monitors using DisplayID v1.3 have a unique 8-byte handle,
  1014. * we store this in a tile group, so we have a common identifier for all tiles
  1015. * in a monitor group. The property is called "TILE". Drivers can manage tile
  1016. * groups using drm_mode_create_tile_group(), drm_mode_put_tile_group() and
  1017. * drm_mode_get_tile_group(). But this is only needed for internal panels where
  1018. * the tile group information is exposed through a non-standard way.
  1019. */
  1020. static void drm_tile_group_free(struct kref *kref)
  1021. {
  1022. struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
  1023. struct drm_device *dev = tg->dev;
  1024. mutex_lock(&dev->mode_config.idr_mutex);
  1025. idr_remove(&dev->mode_config.tile_idr, tg->id);
  1026. mutex_unlock(&dev->mode_config.idr_mutex);
  1027. kfree(tg);
  1028. }
  1029. /**
  1030. * drm_mode_put_tile_group - drop a reference to a tile group.
  1031. * @dev: DRM device
  1032. * @tg: tile group to drop reference to.
  1033. *
  1034. * drop reference to tile group and free if 0.
  1035. */
  1036. void drm_mode_put_tile_group(struct drm_device *dev,
  1037. struct drm_tile_group *tg)
  1038. {
  1039. kref_put(&tg->refcount, drm_tile_group_free);
  1040. }
  1041. EXPORT_SYMBOL(drm_mode_put_tile_group);
  1042. /**
  1043. * drm_mode_get_tile_group - get a reference to an existing tile group
  1044. * @dev: DRM device
  1045. * @topology: 8-bytes unique per monitor.
  1046. *
  1047. * Use the unique bytes to get a reference to an existing tile group.
  1048. *
  1049. * RETURNS:
  1050. * tile group or NULL if not found.
  1051. */
  1052. struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
  1053. char topology[8])
  1054. {
  1055. struct drm_tile_group *tg;
  1056. int id;
  1057. mutex_lock(&dev->mode_config.idr_mutex);
  1058. idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
  1059. if (!memcmp(tg->group_data, topology, 8)) {
  1060. if (!kref_get_unless_zero(&tg->refcount))
  1061. tg = NULL;
  1062. mutex_unlock(&dev->mode_config.idr_mutex);
  1063. return tg;
  1064. }
  1065. }
  1066. mutex_unlock(&dev->mode_config.idr_mutex);
  1067. return NULL;
  1068. }
  1069. EXPORT_SYMBOL(drm_mode_get_tile_group);
  1070. /**
  1071. * drm_mode_create_tile_group - create a tile group from a displayid description
  1072. * @dev: DRM device
  1073. * @topology: 8-bytes unique per monitor.
  1074. *
  1075. * Create a tile group for the unique monitor, and get a unique
  1076. * identifier for the tile group.
  1077. *
  1078. * RETURNS:
  1079. * new tile group or error.
  1080. */
  1081. struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
  1082. char topology[8])
  1083. {
  1084. struct drm_tile_group *tg;
  1085. int ret;
  1086. tg = kzalloc(sizeof(*tg), GFP_KERNEL);
  1087. if (!tg)
  1088. return ERR_PTR(-ENOMEM);
  1089. kref_init(&tg->refcount);
  1090. memcpy(tg->group_data, topology, 8);
  1091. tg->dev = dev;
  1092. mutex_lock(&dev->mode_config.idr_mutex);
  1093. ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
  1094. if (ret >= 0) {
  1095. tg->id = ret;
  1096. } else {
  1097. kfree(tg);
  1098. tg = ERR_PTR(ret);
  1099. }
  1100. mutex_unlock(&dev->mode_config.idr_mutex);
  1101. return tg;
  1102. }
  1103. EXPORT_SYMBOL(drm_mode_create_tile_group);