drm_crtc.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264
  1. /*
  2. * Copyright (c) 2006-2008 Intel Corporation
  3. * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
  4. * Copyright (c) 2008 Red Hat Inc.
  5. *
  6. * DRM core CRTC related functions
  7. *
  8. * Permission to use, copy, modify, distribute, and sell this software and its
  9. * documentation for any purpose is hereby granted without fee, provided that
  10. * the above copyright notice appear in all copies and that both that copyright
  11. * notice and this permission notice appear in supporting documentation, and
  12. * that the name of the copyright holders not be used in advertising or
  13. * publicity pertaining to distribution of the software without specific,
  14. * written prior permission. The copyright holders make no representations
  15. * about the suitability of this software for any purpose. It is provided "as
  16. * is" without express or implied warranty.
  17. *
  18. * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  19. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  20. * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  21. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  22. * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  23. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  24. * OF THIS SOFTWARE.
  25. *
  26. * Authors:
  27. * Keith Packard
  28. * Eric Anholt <eric@anholt.net>
  29. * Dave Airlie <airlied@linux.ie>
  30. * Jesse Barnes <jesse.barnes@intel.com>
  31. */
  32. #include <linux/ctype.h>
  33. #include <linux/list.h>
  34. #include <linux/slab.h>
  35. #include <linux/export.h>
  36. #include <drm/drmP.h>
  37. #include <drm/drm_crtc.h>
  38. #include <drm/drm_edid.h>
  39. #include <drm/drm_fourcc.h>
  40. #include <drm/drm_modeset_lock.h>
  41. #include <drm/drm_atomic.h>
  42. #include <drm/drm_auth.h>
  43. #include <drm/drm_framebuffer.h>
  44. #include "drm_crtc_internal.h"
  45. #include "drm_internal.h"
  46. /*
  47. * Global properties
  48. */
  49. static const struct drm_prop_enum_list drm_plane_type_enum_list[] = {
  50. { DRM_PLANE_TYPE_OVERLAY, "Overlay" },
  51. { DRM_PLANE_TYPE_PRIMARY, "Primary" },
  52. { DRM_PLANE_TYPE_CURSOR, "Cursor" },
  53. };
  54. /*
  55. * Optional properties
  56. */
  57. /**
  58. * drm_crtc_force_disable - Forcibly turn off a CRTC
  59. * @crtc: CRTC to turn off
  60. *
  61. * Returns:
  62. * Zero on success, error code on failure.
  63. */
  64. int drm_crtc_force_disable(struct drm_crtc *crtc)
  65. {
  66. struct drm_mode_set set = {
  67. .crtc = crtc,
  68. };
  69. return drm_mode_set_config_internal(&set);
  70. }
  71. EXPORT_SYMBOL(drm_crtc_force_disable);
  72. /**
  73. * drm_crtc_force_disable_all - Forcibly turn off all enabled CRTCs
  74. * @dev: DRM device whose CRTCs to turn off
  75. *
  76. * Drivers may want to call this on unload to ensure that all displays are
  77. * unlit and the GPU is in a consistent, low power state. Takes modeset locks.
  78. *
  79. * Returns:
  80. * Zero on success, error code on failure.
  81. */
  82. int drm_crtc_force_disable_all(struct drm_device *dev)
  83. {
  84. struct drm_crtc *crtc;
  85. int ret = 0;
  86. drm_modeset_lock_all(dev);
  87. drm_for_each_crtc(crtc, dev)
  88. if (crtc->enabled) {
  89. ret = drm_crtc_force_disable(crtc);
  90. if (ret)
  91. goto out;
  92. }
  93. out:
  94. drm_modeset_unlock_all(dev);
  95. return ret;
  96. }
  97. EXPORT_SYMBOL(drm_crtc_force_disable_all);
  98. DEFINE_WW_CLASS(crtc_ww_class);
  99. static unsigned int drm_num_crtcs(struct drm_device *dev)
  100. {
  101. unsigned int num = 0;
  102. struct drm_crtc *tmp;
  103. drm_for_each_crtc(tmp, dev) {
  104. num++;
  105. }
  106. return num;
  107. }
  108. static int drm_crtc_register_all(struct drm_device *dev)
  109. {
  110. struct drm_crtc *crtc;
  111. int ret = 0;
  112. drm_for_each_crtc(crtc, dev) {
  113. if (crtc->funcs->late_register)
  114. ret = crtc->funcs->late_register(crtc);
  115. if (ret)
  116. return ret;
  117. }
  118. return 0;
  119. }
  120. static void drm_crtc_unregister_all(struct drm_device *dev)
  121. {
  122. struct drm_crtc *crtc;
  123. drm_for_each_crtc(crtc, dev) {
  124. if (crtc->funcs->early_unregister)
  125. crtc->funcs->early_unregister(crtc);
  126. }
  127. }
  128. /**
  129. * drm_crtc_init_with_planes - Initialise a new CRTC object with
  130. * specified primary and cursor planes.
  131. * @dev: DRM device
  132. * @crtc: CRTC object to init
  133. * @primary: Primary plane for CRTC
  134. * @cursor: Cursor plane for CRTC
  135. * @funcs: callbacks for the new CRTC
  136. * @name: printf style format string for the CRTC name, or NULL for default name
  137. *
  138. * Inits a new object created as base part of a driver crtc object. Drivers
  139. * should use this function instead of drm_crtc_init(), which is only provided
  140. * for backwards compatibility with drivers which do not yet support universal
  141. * planes). For really simple hardware which has only 1 plane look at
  142. * drm_simple_display_pipe_init() instead.
  143. *
  144. * Returns:
  145. * Zero on success, error code on failure.
  146. */
  147. int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
  148. struct drm_plane *primary,
  149. struct drm_plane *cursor,
  150. const struct drm_crtc_funcs *funcs,
  151. const char *name, ...)
  152. {
  153. struct drm_mode_config *config = &dev->mode_config;
  154. int ret;
  155. WARN_ON(primary && primary->type != DRM_PLANE_TYPE_PRIMARY);
  156. WARN_ON(cursor && cursor->type != DRM_PLANE_TYPE_CURSOR);
  157. crtc->dev = dev;
  158. crtc->funcs = funcs;
  159. INIT_LIST_HEAD(&crtc->commit_list);
  160. spin_lock_init(&crtc->commit_lock);
  161. drm_modeset_lock_init(&crtc->mutex);
  162. ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
  163. if (ret)
  164. return ret;
  165. if (name) {
  166. va_list ap;
  167. va_start(ap, name);
  168. crtc->name = kvasprintf(GFP_KERNEL, name, ap);
  169. va_end(ap);
  170. } else {
  171. crtc->name = kasprintf(GFP_KERNEL, "crtc-%d",
  172. drm_num_crtcs(dev));
  173. }
  174. if (!crtc->name) {
  175. drm_mode_object_unregister(dev, &crtc->base);
  176. return -ENOMEM;
  177. }
  178. crtc->base.properties = &crtc->properties;
  179. list_add_tail(&crtc->head, &config->crtc_list);
  180. crtc->index = config->num_crtc++;
  181. crtc->primary = primary;
  182. crtc->cursor = cursor;
  183. if (primary)
  184. primary->possible_crtcs = 1 << drm_crtc_index(crtc);
  185. if (cursor)
  186. cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
  187. if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
  188. drm_object_attach_property(&crtc->base, config->prop_active, 0);
  189. drm_object_attach_property(&crtc->base, config->prop_mode_id, 0);
  190. }
  191. return 0;
  192. }
  193. EXPORT_SYMBOL(drm_crtc_init_with_planes);
  194. /**
  195. * drm_crtc_cleanup - Clean up the core crtc usage
  196. * @crtc: CRTC to cleanup
  197. *
  198. * This function cleans up @crtc and removes it from the DRM mode setting
  199. * core. Note that the function does *not* free the crtc structure itself,
  200. * this is the responsibility of the caller.
  201. */
  202. void drm_crtc_cleanup(struct drm_crtc *crtc)
  203. {
  204. struct drm_device *dev = crtc->dev;
  205. /* Note that the crtc_list is considered to be static; should we
  206. * remove the drm_crtc at runtime we would have to decrement all
  207. * the indices on the drm_crtc after us in the crtc_list.
  208. */
  209. kfree(crtc->gamma_store);
  210. crtc->gamma_store = NULL;
  211. drm_modeset_lock_fini(&crtc->mutex);
  212. drm_mode_object_unregister(dev, &crtc->base);
  213. list_del(&crtc->head);
  214. dev->mode_config.num_crtc--;
  215. WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
  216. if (crtc->state && crtc->funcs->atomic_destroy_state)
  217. crtc->funcs->atomic_destroy_state(crtc, crtc->state);
  218. kfree(crtc->name);
  219. memset(crtc, 0, sizeof(*crtc));
  220. }
  221. EXPORT_SYMBOL(drm_crtc_cleanup);
  222. int drm_modeset_register_all(struct drm_device *dev)
  223. {
  224. int ret;
  225. ret = drm_plane_register_all(dev);
  226. if (ret)
  227. goto err_plane;
  228. ret = drm_crtc_register_all(dev);
  229. if (ret)
  230. goto err_crtc;
  231. ret = drm_encoder_register_all(dev);
  232. if (ret)
  233. goto err_encoder;
  234. ret = drm_connector_register_all(dev);
  235. if (ret)
  236. goto err_connector;
  237. return 0;
  238. err_connector:
  239. drm_encoder_unregister_all(dev);
  240. err_encoder:
  241. drm_crtc_unregister_all(dev);
  242. err_crtc:
  243. drm_plane_unregister_all(dev);
  244. err_plane:
  245. return ret;
  246. }
  247. void drm_modeset_unregister_all(struct drm_device *dev)
  248. {
  249. drm_connector_unregister_all(dev);
  250. drm_encoder_unregister_all(dev);
  251. drm_crtc_unregister_all(dev);
  252. drm_plane_unregister_all(dev);
  253. }
  254. static int drm_mode_create_standard_properties(struct drm_device *dev)
  255. {
  256. struct drm_property *prop;
  257. int ret;
  258. ret = drm_connector_create_standard_properties(dev);
  259. if (ret)
  260. return ret;
  261. prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  262. "type", drm_plane_type_enum_list,
  263. ARRAY_SIZE(drm_plane_type_enum_list));
  264. if (!prop)
  265. return -ENOMEM;
  266. dev->mode_config.plane_type_property = prop;
  267. prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  268. "SRC_X", 0, UINT_MAX);
  269. if (!prop)
  270. return -ENOMEM;
  271. dev->mode_config.prop_src_x = prop;
  272. prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  273. "SRC_Y", 0, UINT_MAX);
  274. if (!prop)
  275. return -ENOMEM;
  276. dev->mode_config.prop_src_y = prop;
  277. prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  278. "SRC_W", 0, UINT_MAX);
  279. if (!prop)
  280. return -ENOMEM;
  281. dev->mode_config.prop_src_w = prop;
  282. prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  283. "SRC_H", 0, UINT_MAX);
  284. if (!prop)
  285. return -ENOMEM;
  286. dev->mode_config.prop_src_h = prop;
  287. prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
  288. "CRTC_X", INT_MIN, INT_MAX);
  289. if (!prop)
  290. return -ENOMEM;
  291. dev->mode_config.prop_crtc_x = prop;
  292. prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
  293. "CRTC_Y", INT_MIN, INT_MAX);
  294. if (!prop)
  295. return -ENOMEM;
  296. dev->mode_config.prop_crtc_y = prop;
  297. prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  298. "CRTC_W", 0, INT_MAX);
  299. if (!prop)
  300. return -ENOMEM;
  301. dev->mode_config.prop_crtc_w = prop;
  302. prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  303. "CRTC_H", 0, INT_MAX);
  304. if (!prop)
  305. return -ENOMEM;
  306. dev->mode_config.prop_crtc_h = prop;
  307. prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
  308. "FB_ID", DRM_MODE_OBJECT_FB);
  309. if (!prop)
  310. return -ENOMEM;
  311. dev->mode_config.prop_fb_id = prop;
  312. prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
  313. "CRTC_ID", DRM_MODE_OBJECT_CRTC);
  314. if (!prop)
  315. return -ENOMEM;
  316. dev->mode_config.prop_crtc_id = prop;
  317. prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
  318. "ACTIVE");
  319. if (!prop)
  320. return -ENOMEM;
  321. dev->mode_config.prop_active = prop;
  322. prop = drm_property_create(dev,
  323. DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
  324. "MODE_ID", 0);
  325. if (!prop)
  326. return -ENOMEM;
  327. dev->mode_config.prop_mode_id = prop;
  328. prop = drm_property_create(dev,
  329. DRM_MODE_PROP_BLOB,
  330. "DEGAMMA_LUT", 0);
  331. if (!prop)
  332. return -ENOMEM;
  333. dev->mode_config.degamma_lut_property = prop;
  334. prop = drm_property_create_range(dev,
  335. DRM_MODE_PROP_IMMUTABLE,
  336. "DEGAMMA_LUT_SIZE", 0, UINT_MAX);
  337. if (!prop)
  338. return -ENOMEM;
  339. dev->mode_config.degamma_lut_size_property = prop;
  340. prop = drm_property_create(dev,
  341. DRM_MODE_PROP_BLOB,
  342. "CTM", 0);
  343. if (!prop)
  344. return -ENOMEM;
  345. dev->mode_config.ctm_property = prop;
  346. prop = drm_property_create(dev,
  347. DRM_MODE_PROP_BLOB,
  348. "GAMMA_LUT", 0);
  349. if (!prop)
  350. return -ENOMEM;
  351. dev->mode_config.gamma_lut_property = prop;
  352. prop = drm_property_create_range(dev,
  353. DRM_MODE_PROP_IMMUTABLE,
  354. "GAMMA_LUT_SIZE", 0, UINT_MAX);
  355. if (!prop)
  356. return -ENOMEM;
  357. dev->mode_config.gamma_lut_size_property = prop;
  358. return 0;
  359. }
  360. /**
  361. * drm_mode_getresources - get graphics configuration
  362. * @dev: drm device for the ioctl
  363. * @data: data pointer for the ioctl
  364. * @file_priv: drm file for the ioctl call
  365. *
  366. * Construct a set of configuration description structures and return
  367. * them to the user, including CRTC, connector and framebuffer configuration.
  368. *
  369. * Called by the user via ioctl.
  370. *
  371. * Returns:
  372. * Zero on success, negative errno on failure.
  373. */
  374. int drm_mode_getresources(struct drm_device *dev, void *data,
  375. struct drm_file *file_priv)
  376. {
  377. struct drm_mode_card_res *card_res = data;
  378. struct list_head *lh;
  379. struct drm_framebuffer *fb;
  380. struct drm_connector *connector;
  381. struct drm_crtc *crtc;
  382. struct drm_encoder *encoder;
  383. int ret = 0;
  384. int connector_count = 0;
  385. int crtc_count = 0;
  386. int fb_count = 0;
  387. int encoder_count = 0;
  388. int copied = 0;
  389. uint32_t __user *fb_id;
  390. uint32_t __user *crtc_id;
  391. uint32_t __user *connector_id;
  392. uint32_t __user *encoder_id;
  393. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  394. return -EINVAL;
  395. mutex_lock(&file_priv->fbs_lock);
  396. /*
  397. * For the non-control nodes we need to limit the list of resources
  398. * by IDs in the group list for this node
  399. */
  400. list_for_each(lh, &file_priv->fbs)
  401. fb_count++;
  402. /* handle this in 4 parts */
  403. /* FBs */
  404. if (card_res->count_fbs >= fb_count) {
  405. copied = 0;
  406. fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
  407. list_for_each_entry(fb, &file_priv->fbs, filp_head) {
  408. if (put_user(fb->base.id, fb_id + copied)) {
  409. mutex_unlock(&file_priv->fbs_lock);
  410. return -EFAULT;
  411. }
  412. copied++;
  413. }
  414. }
  415. card_res->count_fbs = fb_count;
  416. mutex_unlock(&file_priv->fbs_lock);
  417. /* mode_config.mutex protects the connector list against e.g. DP MST
  418. * connector hot-adding. CRTC/Plane lists are invariant. */
  419. mutex_lock(&dev->mode_config.mutex);
  420. drm_for_each_crtc(crtc, dev)
  421. crtc_count++;
  422. drm_for_each_connector(connector, dev)
  423. connector_count++;
  424. drm_for_each_encoder(encoder, dev)
  425. encoder_count++;
  426. card_res->max_height = dev->mode_config.max_height;
  427. card_res->min_height = dev->mode_config.min_height;
  428. card_res->max_width = dev->mode_config.max_width;
  429. card_res->min_width = dev->mode_config.min_width;
  430. /* CRTCs */
  431. if (card_res->count_crtcs >= crtc_count) {
  432. copied = 0;
  433. crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
  434. drm_for_each_crtc(crtc, dev) {
  435. if (put_user(crtc->base.id, crtc_id + copied)) {
  436. ret = -EFAULT;
  437. goto out;
  438. }
  439. copied++;
  440. }
  441. }
  442. card_res->count_crtcs = crtc_count;
  443. /* Encoders */
  444. if (card_res->count_encoders >= encoder_count) {
  445. copied = 0;
  446. encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
  447. drm_for_each_encoder(encoder, dev) {
  448. if (put_user(encoder->base.id, encoder_id +
  449. copied)) {
  450. ret = -EFAULT;
  451. goto out;
  452. }
  453. copied++;
  454. }
  455. }
  456. card_res->count_encoders = encoder_count;
  457. /* Connectors */
  458. if (card_res->count_connectors >= connector_count) {
  459. copied = 0;
  460. connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
  461. drm_for_each_connector(connector, dev) {
  462. if (put_user(connector->base.id,
  463. connector_id + copied)) {
  464. ret = -EFAULT;
  465. goto out;
  466. }
  467. copied++;
  468. }
  469. }
  470. card_res->count_connectors = connector_count;
  471. out:
  472. mutex_unlock(&dev->mode_config.mutex);
  473. return ret;
  474. }
  475. /**
  476. * drm_mode_getcrtc - get CRTC configuration
  477. * @dev: drm device for the ioctl
  478. * @data: data pointer for the ioctl
  479. * @file_priv: drm file for the ioctl call
  480. *
  481. * Construct a CRTC configuration structure to return to the user.
  482. *
  483. * Called by the user via ioctl.
  484. *
  485. * Returns:
  486. * Zero on success, negative errno on failure.
  487. */
  488. int drm_mode_getcrtc(struct drm_device *dev,
  489. void *data, struct drm_file *file_priv)
  490. {
  491. struct drm_mode_crtc *crtc_resp = data;
  492. struct drm_crtc *crtc;
  493. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  494. return -EINVAL;
  495. crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
  496. if (!crtc)
  497. return -ENOENT;
  498. drm_modeset_lock_crtc(crtc, crtc->primary);
  499. crtc_resp->gamma_size = crtc->gamma_size;
  500. if (crtc->primary->fb)
  501. crtc_resp->fb_id = crtc->primary->fb->base.id;
  502. else
  503. crtc_resp->fb_id = 0;
  504. if (crtc->state) {
  505. crtc_resp->x = crtc->primary->state->src_x >> 16;
  506. crtc_resp->y = crtc->primary->state->src_y >> 16;
  507. if (crtc->state->enable) {
  508. drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode);
  509. crtc_resp->mode_valid = 1;
  510. } else {
  511. crtc_resp->mode_valid = 0;
  512. }
  513. } else {
  514. crtc_resp->x = crtc->x;
  515. crtc_resp->y = crtc->y;
  516. if (crtc->enabled) {
  517. drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode);
  518. crtc_resp->mode_valid = 1;
  519. } else {
  520. crtc_resp->mode_valid = 0;
  521. }
  522. }
  523. drm_modeset_unlock_crtc(crtc);
  524. return 0;
  525. }
  526. /**
  527. * drm_mode_set_config_internal - helper to call ->set_config
  528. * @set: modeset config to set
  529. *
  530. * This is a little helper to wrap internal calls to the ->set_config driver
  531. * interface. The only thing it adds is correct refcounting dance.
  532. *
  533. * Returns:
  534. * Zero on success, negative errno on failure.
  535. */
  536. int drm_mode_set_config_internal(struct drm_mode_set *set)
  537. {
  538. struct drm_crtc *crtc = set->crtc;
  539. struct drm_framebuffer *fb;
  540. struct drm_crtc *tmp;
  541. int ret;
  542. /*
  543. * NOTE: ->set_config can also disable other crtcs (if we steal all
  544. * connectors from it), hence we need to refcount the fbs across all
  545. * crtcs. Atomic modeset will have saner semantics ...
  546. */
  547. drm_for_each_crtc(tmp, crtc->dev)
  548. tmp->primary->old_fb = tmp->primary->fb;
  549. fb = set->fb;
  550. ret = crtc->funcs->set_config(set);
  551. if (ret == 0) {
  552. crtc->primary->crtc = crtc;
  553. crtc->primary->fb = fb;
  554. }
  555. drm_for_each_crtc(tmp, crtc->dev) {
  556. if (tmp->primary->fb)
  557. drm_framebuffer_reference(tmp->primary->fb);
  558. if (tmp->primary->old_fb)
  559. drm_framebuffer_unreference(tmp->primary->old_fb);
  560. tmp->primary->old_fb = NULL;
  561. }
  562. return ret;
  563. }
  564. EXPORT_SYMBOL(drm_mode_set_config_internal);
  565. /**
  566. * drm_crtc_get_hv_timing - Fetches hdisplay/vdisplay for given mode
  567. * @mode: mode to query
  568. * @hdisplay: hdisplay value to fill in
  569. * @vdisplay: vdisplay value to fill in
  570. *
  571. * The vdisplay value will be doubled if the specified mode is a stereo mode of
  572. * the appropriate layout.
  573. */
  574. void drm_crtc_get_hv_timing(const struct drm_display_mode *mode,
  575. int *hdisplay, int *vdisplay)
  576. {
  577. struct drm_display_mode adjusted;
  578. drm_mode_copy(&adjusted, mode);
  579. drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
  580. *hdisplay = adjusted.crtc_hdisplay;
  581. *vdisplay = adjusted.crtc_vdisplay;
  582. }
  583. EXPORT_SYMBOL(drm_crtc_get_hv_timing);
  584. /**
  585. * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
  586. * CRTC viewport
  587. * @crtc: CRTC that framebuffer will be displayed on
  588. * @x: x panning
  589. * @y: y panning
  590. * @mode: mode that framebuffer will be displayed under
  591. * @fb: framebuffer to check size of
  592. */
  593. int drm_crtc_check_viewport(const struct drm_crtc *crtc,
  594. int x, int y,
  595. const struct drm_display_mode *mode,
  596. const struct drm_framebuffer *fb)
  597. {
  598. int hdisplay, vdisplay;
  599. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  600. if (crtc->state &&
  601. crtc->primary->state->rotation & (DRM_ROTATE_90 |
  602. DRM_ROTATE_270))
  603. swap(hdisplay, vdisplay);
  604. return drm_framebuffer_check_src_coords(x << 16, y << 16,
  605. hdisplay << 16, vdisplay << 16,
  606. fb);
  607. }
  608. EXPORT_SYMBOL(drm_crtc_check_viewport);
  609. /**
  610. * drm_mode_setcrtc - set CRTC configuration
  611. * @dev: drm device for the ioctl
  612. * @data: data pointer for the ioctl
  613. * @file_priv: drm file for the ioctl call
  614. *
  615. * Build a new CRTC configuration based on user request.
  616. *
  617. * Called by the user via ioctl.
  618. *
  619. * Returns:
  620. * Zero on success, negative errno on failure.
  621. */
  622. int drm_mode_setcrtc(struct drm_device *dev, void *data,
  623. struct drm_file *file_priv)
  624. {
  625. struct drm_mode_config *config = &dev->mode_config;
  626. struct drm_mode_crtc *crtc_req = data;
  627. struct drm_crtc *crtc;
  628. struct drm_connector **connector_set = NULL, *connector;
  629. struct drm_framebuffer *fb = NULL;
  630. struct drm_display_mode *mode = NULL;
  631. struct drm_mode_set set;
  632. uint32_t __user *set_connectors_ptr;
  633. int ret;
  634. int i;
  635. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  636. return -EINVAL;
  637. /*
  638. * Universal plane src offsets are only 16.16, prevent havoc for
  639. * drivers using universal plane code internally.
  640. */
  641. if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000)
  642. return -ERANGE;
  643. drm_modeset_lock_all(dev);
  644. crtc = drm_crtc_find(dev, crtc_req->crtc_id);
  645. if (!crtc) {
  646. DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
  647. ret = -ENOENT;
  648. goto out;
  649. }
  650. DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
  651. if (crtc_req->mode_valid) {
  652. /* If we have a mode we need a framebuffer. */
  653. /* If we pass -1, set the mode with the currently bound fb */
  654. if (crtc_req->fb_id == -1) {
  655. if (!crtc->primary->fb) {
  656. DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
  657. ret = -EINVAL;
  658. goto out;
  659. }
  660. fb = crtc->primary->fb;
  661. /* Make refcounting symmetric with the lookup path. */
  662. drm_framebuffer_reference(fb);
  663. } else {
  664. fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
  665. if (!fb) {
  666. DRM_DEBUG_KMS("Unknown FB ID%d\n",
  667. crtc_req->fb_id);
  668. ret = -ENOENT;
  669. goto out;
  670. }
  671. }
  672. mode = drm_mode_create(dev);
  673. if (!mode) {
  674. ret = -ENOMEM;
  675. goto out;
  676. }
  677. ret = drm_mode_convert_umode(mode, &crtc_req->mode);
  678. if (ret) {
  679. DRM_DEBUG_KMS("Invalid mode\n");
  680. goto out;
  681. }
  682. /*
  683. * Check whether the primary plane supports the fb pixel format.
  684. * Drivers not implementing the universal planes API use a
  685. * default formats list provided by the DRM core which doesn't
  686. * match real hardware capabilities. Skip the check in that
  687. * case.
  688. */
  689. if (!crtc->primary->format_default) {
  690. ret = drm_plane_check_pixel_format(crtc->primary,
  691. fb->pixel_format);
  692. if (ret) {
  693. char *format_name = drm_get_format_name(fb->pixel_format);
  694. DRM_DEBUG_KMS("Invalid pixel format %s\n", format_name);
  695. kfree(format_name);
  696. goto out;
  697. }
  698. }
  699. ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
  700. mode, fb);
  701. if (ret)
  702. goto out;
  703. }
  704. if (crtc_req->count_connectors == 0 && mode) {
  705. DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
  706. ret = -EINVAL;
  707. goto out;
  708. }
  709. if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
  710. DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
  711. crtc_req->count_connectors);
  712. ret = -EINVAL;
  713. goto out;
  714. }
  715. if (crtc_req->count_connectors > 0) {
  716. u32 out_id;
  717. /* Avoid unbounded kernel memory allocation */
  718. if (crtc_req->count_connectors > config->num_connector) {
  719. ret = -EINVAL;
  720. goto out;
  721. }
  722. connector_set = kmalloc_array(crtc_req->count_connectors,
  723. sizeof(struct drm_connector *),
  724. GFP_KERNEL);
  725. if (!connector_set) {
  726. ret = -ENOMEM;
  727. goto out;
  728. }
  729. for (i = 0; i < crtc_req->count_connectors; i++) {
  730. connector_set[i] = NULL;
  731. set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
  732. if (get_user(out_id, &set_connectors_ptr[i])) {
  733. ret = -EFAULT;
  734. goto out;
  735. }
  736. connector = drm_connector_lookup(dev, out_id);
  737. if (!connector) {
  738. DRM_DEBUG_KMS("Connector id %d unknown\n",
  739. out_id);
  740. ret = -ENOENT;
  741. goto out;
  742. }
  743. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  744. connector->base.id,
  745. connector->name);
  746. connector_set[i] = connector;
  747. }
  748. }
  749. set.crtc = crtc;
  750. set.x = crtc_req->x;
  751. set.y = crtc_req->y;
  752. set.mode = mode;
  753. set.connectors = connector_set;
  754. set.num_connectors = crtc_req->count_connectors;
  755. set.fb = fb;
  756. ret = drm_mode_set_config_internal(&set);
  757. out:
  758. if (fb)
  759. drm_framebuffer_unreference(fb);
  760. if (connector_set) {
  761. for (i = 0; i < crtc_req->count_connectors; i++) {
  762. if (connector_set[i])
  763. drm_connector_unreference(connector_set[i]);
  764. }
  765. }
  766. kfree(connector_set);
  767. drm_mode_destroy(dev, mode);
  768. drm_modeset_unlock_all(dev);
  769. return ret;
  770. }
  771. int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
  772. struct drm_property *property,
  773. uint64_t value)
  774. {
  775. int ret = -EINVAL;
  776. struct drm_crtc *crtc = obj_to_crtc(obj);
  777. if (crtc->funcs->set_property)
  778. ret = crtc->funcs->set_property(crtc, property, value);
  779. if (!ret)
  780. drm_object_property_set_value(obj, property, value);
  781. return ret;
  782. }
  783. /**
  784. * drm_mode_config_reset - call ->reset callbacks
  785. * @dev: drm device
  786. *
  787. * This functions calls all the crtc's, encoder's and connector's ->reset
  788. * callback. Drivers can use this in e.g. their driver load or resume code to
  789. * reset hardware and software state.
  790. */
  791. void drm_mode_config_reset(struct drm_device *dev)
  792. {
  793. struct drm_crtc *crtc;
  794. struct drm_plane *plane;
  795. struct drm_encoder *encoder;
  796. struct drm_connector *connector;
  797. drm_for_each_plane(plane, dev)
  798. if (plane->funcs->reset)
  799. plane->funcs->reset(plane);
  800. drm_for_each_crtc(crtc, dev)
  801. if (crtc->funcs->reset)
  802. crtc->funcs->reset(crtc);
  803. drm_for_each_encoder(encoder, dev)
  804. if (encoder->funcs->reset)
  805. encoder->funcs->reset(encoder);
  806. mutex_lock(&dev->mode_config.mutex);
  807. drm_for_each_connector(connector, dev)
  808. if (connector->funcs->reset)
  809. connector->funcs->reset(connector);
  810. mutex_unlock(&dev->mode_config.mutex);
  811. }
  812. EXPORT_SYMBOL(drm_mode_config_reset);
  813. /**
  814. * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
  815. * @dev: DRM device
  816. * @data: ioctl data
  817. * @file_priv: DRM file info
  818. *
  819. * This creates a new dumb buffer in the driver's backing storage manager (GEM,
  820. * TTM or something else entirely) and returns the resulting buffer handle. This
  821. * handle can then be wrapped up into a framebuffer modeset object.
  822. *
  823. * Note that userspace is not allowed to use such objects for render
  824. * acceleration - drivers must create their own private ioctls for such a use
  825. * case.
  826. *
  827. * Called by the user via ioctl.
  828. *
  829. * Returns:
  830. * Zero on success, negative errno on failure.
  831. */
  832. int drm_mode_create_dumb_ioctl(struct drm_device *dev,
  833. void *data, struct drm_file *file_priv)
  834. {
  835. struct drm_mode_create_dumb *args = data;
  836. u32 cpp, stride, size;
  837. if (!dev->driver->dumb_create)
  838. return -ENOSYS;
  839. if (!args->width || !args->height || !args->bpp)
  840. return -EINVAL;
  841. /* overflow checks for 32bit size calculations */
  842. /* NOTE: DIV_ROUND_UP() can overflow */
  843. cpp = DIV_ROUND_UP(args->bpp, 8);
  844. if (!cpp || cpp > 0xffffffffU / args->width)
  845. return -EINVAL;
  846. stride = cpp * args->width;
  847. if (args->height > 0xffffffffU / stride)
  848. return -EINVAL;
  849. /* test for wrap-around */
  850. size = args->height * stride;
  851. if (PAGE_ALIGN(size) == 0)
  852. return -EINVAL;
  853. /*
  854. * handle, pitch and size are output parameters. Zero them out to
  855. * prevent drivers from accidentally using uninitialized data. Since
  856. * not all existing userspace is clearing these fields properly we
  857. * cannot reject IOCTL with garbage in them.
  858. */
  859. args->handle = 0;
  860. args->pitch = 0;
  861. args->size = 0;
  862. return dev->driver->dumb_create(file_priv, dev, args);
  863. }
  864. /**
  865. * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
  866. * @dev: DRM device
  867. * @data: ioctl data
  868. * @file_priv: DRM file info
  869. *
  870. * Allocate an offset in the drm device node's address space to be able to
  871. * memory map a dumb buffer.
  872. *
  873. * Called by the user via ioctl.
  874. *
  875. * Returns:
  876. * Zero on success, negative errno on failure.
  877. */
  878. int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
  879. void *data, struct drm_file *file_priv)
  880. {
  881. struct drm_mode_map_dumb *args = data;
  882. /* call driver ioctl to get mmap offset */
  883. if (!dev->driver->dumb_map_offset)
  884. return -ENOSYS;
  885. return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
  886. }
  887. /**
  888. * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
  889. * @dev: DRM device
  890. * @data: ioctl data
  891. * @file_priv: DRM file info
  892. *
  893. * This destroys the userspace handle for the given dumb backing storage buffer.
  894. * Since buffer objects must be reference counted in the kernel a buffer object
  895. * won't be immediately freed if a framebuffer modeset object still uses it.
  896. *
  897. * Called by the user via ioctl.
  898. *
  899. * Returns:
  900. * Zero on success, negative errno on failure.
  901. */
  902. int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
  903. void *data, struct drm_file *file_priv)
  904. {
  905. struct drm_mode_destroy_dumb *args = data;
  906. if (!dev->driver->dumb_destroy)
  907. return -ENOSYS;
  908. return dev->driver->dumb_destroy(file_priv, dev, args->handle);
  909. }
  910. /**
  911. * drm_mode_config_init - initialize DRM mode_configuration structure
  912. * @dev: DRM device
  913. *
  914. * Initialize @dev's mode_config structure, used for tracking the graphics
  915. * configuration of @dev.
  916. *
  917. * Since this initializes the modeset locks, no locking is possible. Which is no
  918. * problem, since this should happen single threaded at init time. It is the
  919. * driver's problem to ensure this guarantee.
  920. *
  921. */
  922. void drm_mode_config_init(struct drm_device *dev)
  923. {
  924. mutex_init(&dev->mode_config.mutex);
  925. drm_modeset_lock_init(&dev->mode_config.connection_mutex);
  926. mutex_init(&dev->mode_config.idr_mutex);
  927. mutex_init(&dev->mode_config.fb_lock);
  928. mutex_init(&dev->mode_config.blob_lock);
  929. INIT_LIST_HEAD(&dev->mode_config.fb_list);
  930. INIT_LIST_HEAD(&dev->mode_config.crtc_list);
  931. INIT_LIST_HEAD(&dev->mode_config.connector_list);
  932. INIT_LIST_HEAD(&dev->mode_config.encoder_list);
  933. INIT_LIST_HEAD(&dev->mode_config.property_list);
  934. INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
  935. INIT_LIST_HEAD(&dev->mode_config.plane_list);
  936. idr_init(&dev->mode_config.crtc_idr);
  937. idr_init(&dev->mode_config.tile_idr);
  938. ida_init(&dev->mode_config.connector_ida);
  939. drm_modeset_lock_all(dev);
  940. drm_mode_create_standard_properties(dev);
  941. drm_modeset_unlock_all(dev);
  942. /* Just to be sure */
  943. dev->mode_config.num_fb = 0;
  944. dev->mode_config.num_connector = 0;
  945. dev->mode_config.num_crtc = 0;
  946. dev->mode_config.num_encoder = 0;
  947. dev->mode_config.num_overlay_plane = 0;
  948. dev->mode_config.num_total_plane = 0;
  949. }
  950. EXPORT_SYMBOL(drm_mode_config_init);
  951. /**
  952. * drm_mode_config_cleanup - free up DRM mode_config info
  953. * @dev: DRM device
  954. *
  955. * Free up all the connectors and CRTCs associated with this DRM device, then
  956. * free up the framebuffers and associated buffer objects.
  957. *
  958. * Note that since this /should/ happen single-threaded at driver/device
  959. * teardown time, no locking is required. It's the driver's job to ensure that
  960. * this guarantee actually holds true.
  961. *
  962. * FIXME: cleanup any dangling user buffer objects too
  963. */
  964. void drm_mode_config_cleanup(struct drm_device *dev)
  965. {
  966. struct drm_connector *connector, *ot;
  967. struct drm_crtc *crtc, *ct;
  968. struct drm_encoder *encoder, *enct;
  969. struct drm_framebuffer *fb, *fbt;
  970. struct drm_property *property, *pt;
  971. struct drm_property_blob *blob, *bt;
  972. struct drm_plane *plane, *plt;
  973. list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
  974. head) {
  975. encoder->funcs->destroy(encoder);
  976. }
  977. list_for_each_entry_safe(connector, ot,
  978. &dev->mode_config.connector_list, head) {
  979. connector->funcs->destroy(connector);
  980. }
  981. list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
  982. head) {
  983. drm_property_destroy(dev, property);
  984. }
  985. list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
  986. head) {
  987. plane->funcs->destroy(plane);
  988. }
  989. list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
  990. crtc->funcs->destroy(crtc);
  991. }
  992. list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
  993. head_global) {
  994. drm_property_unreference_blob(blob);
  995. }
  996. /*
  997. * Single-threaded teardown context, so it's not required to grab the
  998. * fb_lock to protect against concurrent fb_list access. Contrary, it
  999. * would actually deadlock with the drm_framebuffer_cleanup function.
  1000. *
  1001. * Also, if there are any framebuffers left, that's a driver leak now,
  1002. * so politely WARN about this.
  1003. */
  1004. WARN_ON(!list_empty(&dev->mode_config.fb_list));
  1005. list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
  1006. drm_framebuffer_free(&fb->base.refcount);
  1007. }
  1008. ida_destroy(&dev->mode_config.connector_ida);
  1009. idr_destroy(&dev->mode_config.tile_idr);
  1010. idr_destroy(&dev->mode_config.crtc_idr);
  1011. drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
  1012. }
  1013. EXPORT_SYMBOL(drm_mode_config_cleanup);
  1014. /**
  1015. * DOC: Tile group
  1016. *
  1017. * Tile groups are used to represent tiled monitors with a unique
  1018. * integer identifier. Tiled monitors using DisplayID v1.3 have
  1019. * a unique 8-byte handle, we store this in a tile group, so we
  1020. * have a common identifier for all tiles in a monitor group.
  1021. */
  1022. static void drm_tile_group_free(struct kref *kref)
  1023. {
  1024. struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
  1025. struct drm_device *dev = tg->dev;
  1026. mutex_lock(&dev->mode_config.idr_mutex);
  1027. idr_remove(&dev->mode_config.tile_idr, tg->id);
  1028. mutex_unlock(&dev->mode_config.idr_mutex);
  1029. kfree(tg);
  1030. }
  1031. /**
  1032. * drm_mode_put_tile_group - drop a reference to a tile group.
  1033. * @dev: DRM device
  1034. * @tg: tile group to drop reference to.
  1035. *
  1036. * drop reference to tile group and free if 0.
  1037. */
  1038. void drm_mode_put_tile_group(struct drm_device *dev,
  1039. struct drm_tile_group *tg)
  1040. {
  1041. kref_put(&tg->refcount, drm_tile_group_free);
  1042. }
  1043. /**
  1044. * drm_mode_get_tile_group - get a reference to an existing tile group
  1045. * @dev: DRM device
  1046. * @topology: 8-bytes unique per monitor.
  1047. *
  1048. * Use the unique bytes to get a reference to an existing tile group.
  1049. *
  1050. * RETURNS:
  1051. * tile group or NULL if not found.
  1052. */
  1053. struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
  1054. char topology[8])
  1055. {
  1056. struct drm_tile_group *tg;
  1057. int id;
  1058. mutex_lock(&dev->mode_config.idr_mutex);
  1059. idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
  1060. if (!memcmp(tg->group_data, topology, 8)) {
  1061. if (!kref_get_unless_zero(&tg->refcount))
  1062. tg = NULL;
  1063. mutex_unlock(&dev->mode_config.idr_mutex);
  1064. return tg;
  1065. }
  1066. }
  1067. mutex_unlock(&dev->mode_config.idr_mutex);
  1068. return NULL;
  1069. }
  1070. EXPORT_SYMBOL(drm_mode_get_tile_group);
  1071. /**
  1072. * drm_mode_create_tile_group - create a tile group from a displayid description
  1073. * @dev: DRM device
  1074. * @topology: 8-bytes unique per monitor.
  1075. *
  1076. * Create a tile group for the unique monitor, and get a unique
  1077. * identifier for the tile group.
  1078. *
  1079. * RETURNS:
  1080. * new tile group or error.
  1081. */
  1082. struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
  1083. char topology[8])
  1084. {
  1085. struct drm_tile_group *tg;
  1086. int ret;
  1087. tg = kzalloc(sizeof(*tg), GFP_KERNEL);
  1088. if (!tg)
  1089. return ERR_PTR(-ENOMEM);
  1090. kref_init(&tg->refcount);
  1091. memcpy(tg->group_data, topology, 8);
  1092. tg->dev = dev;
  1093. mutex_lock(&dev->mode_config.idr_mutex);
  1094. ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
  1095. if (ret >= 0) {
  1096. tg->id = ret;
  1097. } else {
  1098. kfree(tg);
  1099. tg = ERR_PTR(ret);
  1100. }
  1101. mutex_unlock(&dev->mode_config.idr_mutex);
  1102. return tg;
  1103. }
  1104. EXPORT_SYMBOL(drm_mode_create_tile_group);