drm_crtc.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262
  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 && !primary->possible_crtcs)
  184. primary->possible_crtcs = 1 << drm_crtc_index(crtc);
  185. if (cursor && !cursor->possible_crtcs)
  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. drm_rotation_90_or_270(crtc->primary->state->rotation))
  602. swap(hdisplay, vdisplay);
  603. return drm_framebuffer_check_src_coords(x << 16, y << 16,
  604. hdisplay << 16, vdisplay << 16,
  605. fb);
  606. }
  607. EXPORT_SYMBOL(drm_crtc_check_viewport);
  608. /**
  609. * drm_mode_setcrtc - set CRTC configuration
  610. * @dev: drm device for the ioctl
  611. * @data: data pointer for the ioctl
  612. * @file_priv: drm file for the ioctl call
  613. *
  614. * Build a new CRTC configuration based on user request.
  615. *
  616. * Called by the user via ioctl.
  617. *
  618. * Returns:
  619. * Zero on success, negative errno on failure.
  620. */
  621. int drm_mode_setcrtc(struct drm_device *dev, void *data,
  622. struct drm_file *file_priv)
  623. {
  624. struct drm_mode_config *config = &dev->mode_config;
  625. struct drm_mode_crtc *crtc_req = data;
  626. struct drm_crtc *crtc;
  627. struct drm_connector **connector_set = NULL, *connector;
  628. struct drm_framebuffer *fb = NULL;
  629. struct drm_display_mode *mode = NULL;
  630. struct drm_mode_set set;
  631. uint32_t __user *set_connectors_ptr;
  632. int ret;
  633. int i;
  634. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  635. return -EINVAL;
  636. /*
  637. * Universal plane src offsets are only 16.16, prevent havoc for
  638. * drivers using universal plane code internally.
  639. */
  640. if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000)
  641. return -ERANGE;
  642. drm_modeset_lock_all(dev);
  643. crtc = drm_crtc_find(dev, crtc_req->crtc_id);
  644. if (!crtc) {
  645. DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
  646. ret = -ENOENT;
  647. goto out;
  648. }
  649. DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
  650. if (crtc_req->mode_valid) {
  651. /* If we have a mode we need a framebuffer. */
  652. /* If we pass -1, set the mode with the currently bound fb */
  653. if (crtc_req->fb_id == -1) {
  654. if (!crtc->primary->fb) {
  655. DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
  656. ret = -EINVAL;
  657. goto out;
  658. }
  659. fb = crtc->primary->fb;
  660. /* Make refcounting symmetric with the lookup path. */
  661. drm_framebuffer_reference(fb);
  662. } else {
  663. fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
  664. if (!fb) {
  665. DRM_DEBUG_KMS("Unknown FB ID%d\n",
  666. crtc_req->fb_id);
  667. ret = -ENOENT;
  668. goto out;
  669. }
  670. }
  671. mode = drm_mode_create(dev);
  672. if (!mode) {
  673. ret = -ENOMEM;
  674. goto out;
  675. }
  676. ret = drm_mode_convert_umode(mode, &crtc_req->mode);
  677. if (ret) {
  678. DRM_DEBUG_KMS("Invalid mode\n");
  679. goto out;
  680. }
  681. /*
  682. * Check whether the primary plane supports the fb pixel format.
  683. * Drivers not implementing the universal planes API use a
  684. * default formats list provided by the DRM core which doesn't
  685. * match real hardware capabilities. Skip the check in that
  686. * case.
  687. */
  688. if (!crtc->primary->format_default) {
  689. ret = drm_plane_check_pixel_format(crtc->primary,
  690. fb->pixel_format);
  691. if (ret) {
  692. char *format_name = drm_get_format_name(fb->pixel_format);
  693. DRM_DEBUG_KMS("Invalid pixel format %s\n", format_name);
  694. kfree(format_name);
  695. goto out;
  696. }
  697. }
  698. ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
  699. mode, fb);
  700. if (ret)
  701. goto out;
  702. }
  703. if (crtc_req->count_connectors == 0 && mode) {
  704. DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
  705. ret = -EINVAL;
  706. goto out;
  707. }
  708. if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
  709. DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
  710. crtc_req->count_connectors);
  711. ret = -EINVAL;
  712. goto out;
  713. }
  714. if (crtc_req->count_connectors > 0) {
  715. u32 out_id;
  716. /* Avoid unbounded kernel memory allocation */
  717. if (crtc_req->count_connectors > config->num_connector) {
  718. ret = -EINVAL;
  719. goto out;
  720. }
  721. connector_set = kmalloc_array(crtc_req->count_connectors,
  722. sizeof(struct drm_connector *),
  723. GFP_KERNEL);
  724. if (!connector_set) {
  725. ret = -ENOMEM;
  726. goto out;
  727. }
  728. for (i = 0; i < crtc_req->count_connectors; i++) {
  729. connector_set[i] = NULL;
  730. set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
  731. if (get_user(out_id, &set_connectors_ptr[i])) {
  732. ret = -EFAULT;
  733. goto out;
  734. }
  735. connector = drm_connector_lookup(dev, out_id);
  736. if (!connector) {
  737. DRM_DEBUG_KMS("Connector id %d unknown\n",
  738. out_id);
  739. ret = -ENOENT;
  740. goto out;
  741. }
  742. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  743. connector->base.id,
  744. connector->name);
  745. connector_set[i] = connector;
  746. }
  747. }
  748. set.crtc = crtc;
  749. set.x = crtc_req->x;
  750. set.y = crtc_req->y;
  751. set.mode = mode;
  752. set.connectors = connector_set;
  753. set.num_connectors = crtc_req->count_connectors;
  754. set.fb = fb;
  755. ret = drm_mode_set_config_internal(&set);
  756. out:
  757. if (fb)
  758. drm_framebuffer_unreference(fb);
  759. if (connector_set) {
  760. for (i = 0; i < crtc_req->count_connectors; i++) {
  761. if (connector_set[i])
  762. drm_connector_unreference(connector_set[i]);
  763. }
  764. }
  765. kfree(connector_set);
  766. drm_mode_destroy(dev, mode);
  767. drm_modeset_unlock_all(dev);
  768. return ret;
  769. }
  770. int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
  771. struct drm_property *property,
  772. uint64_t value)
  773. {
  774. int ret = -EINVAL;
  775. struct drm_crtc *crtc = obj_to_crtc(obj);
  776. if (crtc->funcs->set_property)
  777. ret = crtc->funcs->set_property(crtc, property, value);
  778. if (!ret)
  779. drm_object_property_set_value(obj, property, value);
  780. return ret;
  781. }
  782. /**
  783. * drm_mode_config_reset - call ->reset callbacks
  784. * @dev: drm device
  785. *
  786. * This functions calls all the crtc's, encoder's and connector's ->reset
  787. * callback. Drivers can use this in e.g. their driver load or resume code to
  788. * reset hardware and software state.
  789. */
  790. void drm_mode_config_reset(struct drm_device *dev)
  791. {
  792. struct drm_crtc *crtc;
  793. struct drm_plane *plane;
  794. struct drm_encoder *encoder;
  795. struct drm_connector *connector;
  796. drm_for_each_plane(plane, dev)
  797. if (plane->funcs->reset)
  798. plane->funcs->reset(plane);
  799. drm_for_each_crtc(crtc, dev)
  800. if (crtc->funcs->reset)
  801. crtc->funcs->reset(crtc);
  802. drm_for_each_encoder(encoder, dev)
  803. if (encoder->funcs->reset)
  804. encoder->funcs->reset(encoder);
  805. mutex_lock(&dev->mode_config.mutex);
  806. drm_for_each_connector(connector, dev)
  807. if (connector->funcs->reset)
  808. connector->funcs->reset(connector);
  809. mutex_unlock(&dev->mode_config.mutex);
  810. }
  811. EXPORT_SYMBOL(drm_mode_config_reset);
  812. /**
  813. * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
  814. * @dev: DRM device
  815. * @data: ioctl data
  816. * @file_priv: DRM file info
  817. *
  818. * This creates a new dumb buffer in the driver's backing storage manager (GEM,
  819. * TTM or something else entirely) and returns the resulting buffer handle. This
  820. * handle can then be wrapped up into a framebuffer modeset object.
  821. *
  822. * Note that userspace is not allowed to use such objects for render
  823. * acceleration - drivers must create their own private ioctls for such a use
  824. * case.
  825. *
  826. * Called by the user via ioctl.
  827. *
  828. * Returns:
  829. * Zero on success, negative errno on failure.
  830. */
  831. int drm_mode_create_dumb_ioctl(struct drm_device *dev,
  832. void *data, struct drm_file *file_priv)
  833. {
  834. struct drm_mode_create_dumb *args = data;
  835. u32 cpp, stride, size;
  836. if (!dev->driver->dumb_create)
  837. return -ENOSYS;
  838. if (!args->width || !args->height || !args->bpp)
  839. return -EINVAL;
  840. /* overflow checks for 32bit size calculations */
  841. /* NOTE: DIV_ROUND_UP() can overflow */
  842. cpp = DIV_ROUND_UP(args->bpp, 8);
  843. if (!cpp || cpp > 0xffffffffU / args->width)
  844. return -EINVAL;
  845. stride = cpp * args->width;
  846. if (args->height > 0xffffffffU / stride)
  847. return -EINVAL;
  848. /* test for wrap-around */
  849. size = args->height * stride;
  850. if (PAGE_ALIGN(size) == 0)
  851. return -EINVAL;
  852. /*
  853. * handle, pitch and size are output parameters. Zero them out to
  854. * prevent drivers from accidentally using uninitialized data. Since
  855. * not all existing userspace is clearing these fields properly we
  856. * cannot reject IOCTL with garbage in them.
  857. */
  858. args->handle = 0;
  859. args->pitch = 0;
  860. args->size = 0;
  861. return dev->driver->dumb_create(file_priv, dev, args);
  862. }
  863. /**
  864. * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
  865. * @dev: DRM device
  866. * @data: ioctl data
  867. * @file_priv: DRM file info
  868. *
  869. * Allocate an offset in the drm device node's address space to be able to
  870. * memory map a dumb buffer.
  871. *
  872. * Called by the user via ioctl.
  873. *
  874. * Returns:
  875. * Zero on success, negative errno on failure.
  876. */
  877. int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
  878. void *data, struct drm_file *file_priv)
  879. {
  880. struct drm_mode_map_dumb *args = data;
  881. /* call driver ioctl to get mmap offset */
  882. if (!dev->driver->dumb_map_offset)
  883. return -ENOSYS;
  884. return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
  885. }
  886. /**
  887. * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
  888. * @dev: DRM device
  889. * @data: ioctl data
  890. * @file_priv: DRM file info
  891. *
  892. * This destroys the userspace handle for the given dumb backing storage buffer.
  893. * Since buffer objects must be reference counted in the kernel a buffer object
  894. * won't be immediately freed if a framebuffer modeset object still uses it.
  895. *
  896. * Called by the user via ioctl.
  897. *
  898. * Returns:
  899. * Zero on success, negative errno on failure.
  900. */
  901. int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
  902. void *data, struct drm_file *file_priv)
  903. {
  904. struct drm_mode_destroy_dumb *args = data;
  905. if (!dev->driver->dumb_destroy)
  906. return -ENOSYS;
  907. return dev->driver->dumb_destroy(file_priv, dev, args->handle);
  908. }
  909. /**
  910. * drm_mode_config_init - initialize DRM mode_configuration structure
  911. * @dev: DRM device
  912. *
  913. * Initialize @dev's mode_config structure, used for tracking the graphics
  914. * configuration of @dev.
  915. *
  916. * Since this initializes the modeset locks, no locking is possible. Which is no
  917. * problem, since this should happen single threaded at init time. It is the
  918. * driver's problem to ensure this guarantee.
  919. *
  920. */
  921. void drm_mode_config_init(struct drm_device *dev)
  922. {
  923. mutex_init(&dev->mode_config.mutex);
  924. drm_modeset_lock_init(&dev->mode_config.connection_mutex);
  925. mutex_init(&dev->mode_config.idr_mutex);
  926. mutex_init(&dev->mode_config.fb_lock);
  927. mutex_init(&dev->mode_config.blob_lock);
  928. INIT_LIST_HEAD(&dev->mode_config.fb_list);
  929. INIT_LIST_HEAD(&dev->mode_config.crtc_list);
  930. INIT_LIST_HEAD(&dev->mode_config.connector_list);
  931. INIT_LIST_HEAD(&dev->mode_config.encoder_list);
  932. INIT_LIST_HEAD(&dev->mode_config.property_list);
  933. INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
  934. INIT_LIST_HEAD(&dev->mode_config.plane_list);
  935. idr_init(&dev->mode_config.crtc_idr);
  936. idr_init(&dev->mode_config.tile_idr);
  937. ida_init(&dev->mode_config.connector_ida);
  938. drm_modeset_lock_all(dev);
  939. drm_mode_create_standard_properties(dev);
  940. drm_modeset_unlock_all(dev);
  941. /* Just to be sure */
  942. dev->mode_config.num_fb = 0;
  943. dev->mode_config.num_connector = 0;
  944. dev->mode_config.num_crtc = 0;
  945. dev->mode_config.num_encoder = 0;
  946. dev->mode_config.num_overlay_plane = 0;
  947. dev->mode_config.num_total_plane = 0;
  948. }
  949. EXPORT_SYMBOL(drm_mode_config_init);
  950. /**
  951. * drm_mode_config_cleanup - free up DRM mode_config info
  952. * @dev: DRM device
  953. *
  954. * Free up all the connectors and CRTCs associated with this DRM device, then
  955. * free up the framebuffers and associated buffer objects.
  956. *
  957. * Note that since this /should/ happen single-threaded at driver/device
  958. * teardown time, no locking is required. It's the driver's job to ensure that
  959. * this guarantee actually holds true.
  960. *
  961. * FIXME: cleanup any dangling user buffer objects too
  962. */
  963. void drm_mode_config_cleanup(struct drm_device *dev)
  964. {
  965. struct drm_connector *connector, *ot;
  966. struct drm_crtc *crtc, *ct;
  967. struct drm_encoder *encoder, *enct;
  968. struct drm_framebuffer *fb, *fbt;
  969. struct drm_property *property, *pt;
  970. struct drm_property_blob *blob, *bt;
  971. struct drm_plane *plane, *plt;
  972. list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
  973. head) {
  974. encoder->funcs->destroy(encoder);
  975. }
  976. list_for_each_entry_safe(connector, ot,
  977. &dev->mode_config.connector_list, head) {
  978. connector->funcs->destroy(connector);
  979. }
  980. list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
  981. head) {
  982. drm_property_destroy(dev, property);
  983. }
  984. list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
  985. head) {
  986. plane->funcs->destroy(plane);
  987. }
  988. list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
  989. crtc->funcs->destroy(crtc);
  990. }
  991. list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
  992. head_global) {
  993. drm_property_unreference_blob(blob);
  994. }
  995. /*
  996. * Single-threaded teardown context, so it's not required to grab the
  997. * fb_lock to protect against concurrent fb_list access. Contrary, it
  998. * would actually deadlock with the drm_framebuffer_cleanup function.
  999. *
  1000. * Also, if there are any framebuffers left, that's a driver leak now,
  1001. * so politely WARN about this.
  1002. */
  1003. WARN_ON(!list_empty(&dev->mode_config.fb_list));
  1004. list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
  1005. drm_framebuffer_free(&fb->base.refcount);
  1006. }
  1007. ida_destroy(&dev->mode_config.connector_ida);
  1008. idr_destroy(&dev->mode_config.tile_idr);
  1009. idr_destroy(&dev->mode_config.crtc_idr);
  1010. drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
  1011. }
  1012. EXPORT_SYMBOL(drm_mode_config_cleanup);
  1013. /**
  1014. * DOC: Tile group
  1015. *
  1016. * Tile groups are used to represent tiled monitors with a unique
  1017. * integer identifier. Tiled monitors using DisplayID v1.3 have
  1018. * a unique 8-byte handle, we store this in a tile group, so we
  1019. * have a common identifier for all tiles in a monitor group.
  1020. */
  1021. static void drm_tile_group_free(struct kref *kref)
  1022. {
  1023. struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
  1024. struct drm_device *dev = tg->dev;
  1025. mutex_lock(&dev->mode_config.idr_mutex);
  1026. idr_remove(&dev->mode_config.tile_idr, tg->id);
  1027. mutex_unlock(&dev->mode_config.idr_mutex);
  1028. kfree(tg);
  1029. }
  1030. /**
  1031. * drm_mode_put_tile_group - drop a reference to a tile group.
  1032. * @dev: DRM device
  1033. * @tg: tile group to drop reference to.
  1034. *
  1035. * drop reference to tile group and free if 0.
  1036. */
  1037. void drm_mode_put_tile_group(struct drm_device *dev,
  1038. struct drm_tile_group *tg)
  1039. {
  1040. kref_put(&tg->refcount, drm_tile_group_free);
  1041. }
  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);