drm_plane.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  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_plane.h>
  24. #include "drm_crtc_internal.h"
  25. /**
  26. * DOC: overview
  27. *
  28. * A plane represents an image source that can be blended with or overlayed on
  29. * top of a CRTC during the scanout process. Planes take their input data from a
  30. * &drm_framebuffer object. The plane itself specifies the cropping and scaling
  31. * of that image, and where it is placed on the visible are of a display
  32. * pipeline, represented by &drm_crtc. A plane can also have additional
  33. * properties that specify how the pixels are positioned and blended, like
  34. * rotation or Z-position. All these properties are stored in &drm_plane_state.
  35. *
  36. * To create a plane, a KMS drivers allocates and zeroes an instances of
  37. * &struct drm_plane (possibly as part of a larger structure) and registers it
  38. * with a call to drm_universal_plane_init().
  39. *
  40. * Cursor and overlay planes are optional. All drivers should provide one
  41. * primary plane per CRTC to avoid surprising userspace too much. See enum
  42. * drm_plane_type for a more in-depth discussion of these special uapi-relevant
  43. * plane types. Special planes are associated with their CRTC by calling
  44. * drm_crtc_init_with_planes().
  45. *
  46. * The type of a plane is exposed in the immutable "type" enumeration property,
  47. * which has one of the following values: "Overlay", "Primary", "Cursor".
  48. */
  49. static unsigned int drm_num_planes(struct drm_device *dev)
  50. {
  51. unsigned int num = 0;
  52. struct drm_plane *tmp;
  53. drm_for_each_plane(tmp, dev) {
  54. num++;
  55. }
  56. return num;
  57. }
  58. static inline u32 *
  59. formats_ptr(struct drm_format_modifier_blob *blob)
  60. {
  61. return (u32 *)(((char *)blob) + blob->formats_offset);
  62. }
  63. static inline struct drm_format_modifier *
  64. modifiers_ptr(struct drm_format_modifier_blob *blob)
  65. {
  66. return (struct drm_format_modifier *)(((char *)blob) + blob->modifiers_offset);
  67. }
  68. static int create_in_format_blob(struct drm_device *dev, struct drm_plane *plane)
  69. {
  70. const struct drm_mode_config *config = &dev->mode_config;
  71. struct drm_property_blob *blob;
  72. struct drm_format_modifier *mod;
  73. size_t blob_size, formats_size, modifiers_size;
  74. struct drm_format_modifier_blob *blob_data;
  75. unsigned int i, j;
  76. formats_size = sizeof(__u32) * plane->format_count;
  77. if (WARN_ON(!formats_size)) {
  78. /* 0 formats are never expected */
  79. return 0;
  80. }
  81. modifiers_size =
  82. sizeof(struct drm_format_modifier) * plane->modifier_count;
  83. blob_size = sizeof(struct drm_format_modifier_blob);
  84. /* Modifiers offset is a pointer to a struct with a 64 bit field so it
  85. * should be naturally aligned to 8B.
  86. */
  87. BUILD_BUG_ON(sizeof(struct drm_format_modifier_blob) % 8);
  88. blob_size += ALIGN(formats_size, 8);
  89. blob_size += modifiers_size;
  90. blob = drm_property_create_blob(dev, blob_size, NULL);
  91. if (IS_ERR(blob))
  92. return -1;
  93. blob_data = (struct drm_format_modifier_blob *)blob->data;
  94. blob_data->version = FORMAT_BLOB_CURRENT;
  95. blob_data->count_formats = plane->format_count;
  96. blob_data->formats_offset = sizeof(struct drm_format_modifier_blob);
  97. blob_data->count_modifiers = plane->modifier_count;
  98. blob_data->modifiers_offset =
  99. ALIGN(blob_data->formats_offset + formats_size, 8);
  100. memcpy(formats_ptr(blob_data), plane->format_types, formats_size);
  101. /* If we can't determine support, just bail */
  102. if (!plane->funcs->format_mod_supported)
  103. goto done;
  104. mod = modifiers_ptr(blob_data);
  105. for (i = 0; i < plane->modifier_count; i++) {
  106. for (j = 0; j < plane->format_count; j++) {
  107. if (plane->funcs->format_mod_supported(plane,
  108. plane->format_types[j],
  109. plane->modifiers[i])) {
  110. mod->formats |= 1ULL << j;
  111. }
  112. }
  113. mod->modifier = plane->modifiers[i];
  114. mod->offset = 0;
  115. mod->pad = 0;
  116. mod++;
  117. }
  118. done:
  119. drm_object_attach_property(&plane->base, config->modifiers_property,
  120. blob->base.id);
  121. return 0;
  122. }
  123. /**
  124. * drm_universal_plane_init - Initialize a new universal plane object
  125. * @dev: DRM device
  126. * @plane: plane object to init
  127. * @possible_crtcs: bitmask of possible CRTCs
  128. * @funcs: callbacks for the new plane
  129. * @formats: array of supported formats (DRM_FORMAT\_\*)
  130. * @format_count: number of elements in @formats
  131. * @format_modifiers: array of struct drm_format modifiers terminated by
  132. * DRM_FORMAT_MOD_INVALID
  133. * @type: type of plane (overlay, primary, cursor)
  134. * @name: printf style format string for the plane name, or NULL for default name
  135. *
  136. * Initializes a plane object of type @type.
  137. *
  138. * Returns:
  139. * Zero on success, error code on failure.
  140. */
  141. int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
  142. uint32_t possible_crtcs,
  143. const struct drm_plane_funcs *funcs,
  144. const uint32_t *formats, unsigned int format_count,
  145. const uint64_t *format_modifiers,
  146. enum drm_plane_type type,
  147. const char *name, ...)
  148. {
  149. struct drm_mode_config *config = &dev->mode_config;
  150. unsigned int format_modifier_count = 0;
  151. int ret;
  152. ret = drm_mode_object_add(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
  153. if (ret)
  154. return ret;
  155. drm_modeset_lock_init(&plane->mutex);
  156. plane->base.properties = &plane->properties;
  157. plane->dev = dev;
  158. plane->funcs = funcs;
  159. plane->format_types = kmalloc_array(format_count, sizeof(uint32_t),
  160. GFP_KERNEL);
  161. if (!plane->format_types) {
  162. DRM_DEBUG_KMS("out of memory when allocating plane\n");
  163. drm_mode_object_unregister(dev, &plane->base);
  164. return -ENOMEM;
  165. }
  166. /*
  167. * First driver to need more than 64 formats needs to fix this. Each
  168. * format is encoded as a bit and the current code only supports a u64.
  169. */
  170. if (WARN_ON(format_count > 64))
  171. return -EINVAL;
  172. if (format_modifiers) {
  173. const uint64_t *temp_modifiers = format_modifiers;
  174. while (*temp_modifiers++ != DRM_FORMAT_MOD_INVALID)
  175. format_modifier_count++;
  176. }
  177. if (format_modifier_count)
  178. config->allow_fb_modifiers = true;
  179. plane->modifier_count = format_modifier_count;
  180. plane->modifiers = kmalloc_array(format_modifier_count,
  181. sizeof(format_modifiers[0]),
  182. GFP_KERNEL);
  183. if (format_modifier_count && !plane->modifiers) {
  184. DRM_DEBUG_KMS("out of memory when allocating plane\n");
  185. kfree(plane->format_types);
  186. drm_mode_object_unregister(dev, &plane->base);
  187. return -ENOMEM;
  188. }
  189. if (name) {
  190. va_list ap;
  191. va_start(ap, name);
  192. plane->name = kvasprintf(GFP_KERNEL, name, ap);
  193. va_end(ap);
  194. } else {
  195. plane->name = kasprintf(GFP_KERNEL, "plane-%d",
  196. drm_num_planes(dev));
  197. }
  198. if (!plane->name) {
  199. kfree(plane->format_types);
  200. kfree(plane->modifiers);
  201. drm_mode_object_unregister(dev, &plane->base);
  202. return -ENOMEM;
  203. }
  204. memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
  205. plane->format_count = format_count;
  206. memcpy(plane->modifiers, format_modifiers,
  207. format_modifier_count * sizeof(format_modifiers[0]));
  208. plane->possible_crtcs = possible_crtcs;
  209. plane->type = type;
  210. list_add_tail(&plane->head, &config->plane_list);
  211. plane->index = config->num_total_plane++;
  212. if (plane->type == DRM_PLANE_TYPE_OVERLAY)
  213. config->num_overlay_plane++;
  214. drm_object_attach_property(&plane->base,
  215. config->plane_type_property,
  216. plane->type);
  217. if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
  218. drm_object_attach_property(&plane->base, config->prop_fb_id, 0);
  219. drm_object_attach_property(&plane->base, config->prop_in_fence_fd, -1);
  220. drm_object_attach_property(&plane->base, config->prop_crtc_id, 0);
  221. drm_object_attach_property(&plane->base, config->prop_crtc_x, 0);
  222. drm_object_attach_property(&plane->base, config->prop_crtc_y, 0);
  223. drm_object_attach_property(&plane->base, config->prop_crtc_w, 0);
  224. drm_object_attach_property(&plane->base, config->prop_crtc_h, 0);
  225. drm_object_attach_property(&plane->base, config->prop_src_x, 0);
  226. drm_object_attach_property(&plane->base, config->prop_src_y, 0);
  227. drm_object_attach_property(&plane->base, config->prop_src_w, 0);
  228. drm_object_attach_property(&plane->base, config->prop_src_h, 0);
  229. }
  230. if (config->allow_fb_modifiers)
  231. create_in_format_blob(dev, plane);
  232. return 0;
  233. }
  234. EXPORT_SYMBOL(drm_universal_plane_init);
  235. int drm_plane_register_all(struct drm_device *dev)
  236. {
  237. struct drm_plane *plane;
  238. int ret = 0;
  239. drm_for_each_plane(plane, dev) {
  240. if (plane->funcs->late_register)
  241. ret = plane->funcs->late_register(plane);
  242. if (ret)
  243. return ret;
  244. }
  245. return 0;
  246. }
  247. void drm_plane_unregister_all(struct drm_device *dev)
  248. {
  249. struct drm_plane *plane;
  250. drm_for_each_plane(plane, dev) {
  251. if (plane->funcs->early_unregister)
  252. plane->funcs->early_unregister(plane);
  253. }
  254. }
  255. /**
  256. * drm_plane_init - Initialize a legacy plane
  257. * @dev: DRM device
  258. * @plane: plane object to init
  259. * @possible_crtcs: bitmask of possible CRTCs
  260. * @funcs: callbacks for the new plane
  261. * @formats: array of supported formats (DRM_FORMAT\_\*)
  262. * @format_count: number of elements in @formats
  263. * @is_primary: plane type (primary vs overlay)
  264. *
  265. * Legacy API to initialize a DRM plane.
  266. *
  267. * New drivers should call drm_universal_plane_init() instead.
  268. *
  269. * Returns:
  270. * Zero on success, error code on failure.
  271. */
  272. int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
  273. uint32_t possible_crtcs,
  274. const struct drm_plane_funcs *funcs,
  275. const uint32_t *formats, unsigned int format_count,
  276. bool is_primary)
  277. {
  278. enum drm_plane_type type;
  279. type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
  280. return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
  281. formats, format_count,
  282. NULL, type, NULL);
  283. }
  284. EXPORT_SYMBOL(drm_plane_init);
  285. /**
  286. * drm_plane_cleanup - Clean up the core plane usage
  287. * @plane: plane to cleanup
  288. *
  289. * This function cleans up @plane and removes it from the DRM mode setting
  290. * core. Note that the function does *not* free the plane structure itself,
  291. * this is the responsibility of the caller.
  292. */
  293. void drm_plane_cleanup(struct drm_plane *plane)
  294. {
  295. struct drm_device *dev = plane->dev;
  296. drm_modeset_lock_fini(&plane->mutex);
  297. kfree(plane->format_types);
  298. kfree(plane->modifiers);
  299. drm_mode_object_unregister(dev, &plane->base);
  300. BUG_ON(list_empty(&plane->head));
  301. /* Note that the plane_list is considered to be static; should we
  302. * remove the drm_plane at runtime we would have to decrement all
  303. * the indices on the drm_plane after us in the plane_list.
  304. */
  305. list_del(&plane->head);
  306. dev->mode_config.num_total_plane--;
  307. if (plane->type == DRM_PLANE_TYPE_OVERLAY)
  308. dev->mode_config.num_overlay_plane--;
  309. WARN_ON(plane->state && !plane->funcs->atomic_destroy_state);
  310. if (plane->state && plane->funcs->atomic_destroy_state)
  311. plane->funcs->atomic_destroy_state(plane, plane->state);
  312. kfree(plane->name);
  313. memset(plane, 0, sizeof(*plane));
  314. }
  315. EXPORT_SYMBOL(drm_plane_cleanup);
  316. /**
  317. * drm_plane_from_index - find the registered plane at an index
  318. * @dev: DRM device
  319. * @idx: index of registered plane to find for
  320. *
  321. * Given a plane index, return the registered plane from DRM device's
  322. * list of planes with matching index. This is the inverse of drm_plane_index().
  323. */
  324. struct drm_plane *
  325. drm_plane_from_index(struct drm_device *dev, int idx)
  326. {
  327. struct drm_plane *plane;
  328. drm_for_each_plane(plane, dev)
  329. if (idx == plane->index)
  330. return plane;
  331. return NULL;
  332. }
  333. EXPORT_SYMBOL(drm_plane_from_index);
  334. /**
  335. * drm_plane_force_disable - Forcibly disable a plane
  336. * @plane: plane to disable
  337. *
  338. * Forces the plane to be disabled.
  339. *
  340. * Used when the plane's current framebuffer is destroyed,
  341. * and when restoring fbdev mode.
  342. *
  343. * Note that this function is not suitable for atomic drivers, since it doesn't
  344. * wire through the lock acquisition context properly and hence can't handle
  345. * retries or driver private locks. You probably want to use
  346. * drm_atomic_helper_disable_plane() or
  347. * drm_atomic_helper_disable_planes_on_crtc() instead.
  348. */
  349. void drm_plane_force_disable(struct drm_plane *plane)
  350. {
  351. int ret;
  352. if (!plane->fb)
  353. return;
  354. WARN_ON(drm_drv_uses_atomic_modeset(plane->dev));
  355. plane->old_fb = plane->fb;
  356. ret = plane->funcs->disable_plane(plane, NULL);
  357. if (ret) {
  358. DRM_ERROR("failed to disable plane with busy fb\n");
  359. plane->old_fb = NULL;
  360. return;
  361. }
  362. /* disconnect the plane from the fb and crtc: */
  363. drm_framebuffer_put(plane->old_fb);
  364. plane->old_fb = NULL;
  365. plane->fb = NULL;
  366. plane->crtc = NULL;
  367. }
  368. EXPORT_SYMBOL(drm_plane_force_disable);
  369. /**
  370. * drm_mode_plane_set_obj_prop - set the value of a property
  371. * @plane: drm plane object to set property value for
  372. * @property: property to set
  373. * @value: value the property should be set to
  374. *
  375. * This functions sets a given property on a given plane object. This function
  376. * calls the driver's ->set_property callback and changes the software state of
  377. * the property if the callback succeeds.
  378. *
  379. * Returns:
  380. * Zero on success, error code on failure.
  381. */
  382. int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
  383. struct drm_property *property,
  384. uint64_t value)
  385. {
  386. int ret = -EINVAL;
  387. struct drm_mode_object *obj = &plane->base;
  388. if (plane->funcs->set_property)
  389. ret = plane->funcs->set_property(plane, property, value);
  390. if (!ret)
  391. drm_object_property_set_value(obj, property, value);
  392. return ret;
  393. }
  394. EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
  395. int drm_mode_getplane_res(struct drm_device *dev, void *data,
  396. struct drm_file *file_priv)
  397. {
  398. struct drm_mode_get_plane_res *plane_resp = data;
  399. struct drm_mode_config *config;
  400. struct drm_plane *plane;
  401. uint32_t __user *plane_ptr;
  402. int copied = 0;
  403. unsigned num_planes;
  404. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  405. return -EINVAL;
  406. config = &dev->mode_config;
  407. if (file_priv->universal_planes)
  408. num_planes = config->num_total_plane;
  409. else
  410. num_planes = config->num_overlay_plane;
  411. /*
  412. * This ioctl is called twice, once to determine how much space is
  413. * needed, and the 2nd time to fill it.
  414. */
  415. if (num_planes &&
  416. (plane_resp->count_planes >= num_planes)) {
  417. plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
  418. /* Plane lists are invariant, no locking needed. */
  419. drm_for_each_plane(plane, dev) {
  420. /*
  421. * Unless userspace set the 'universal planes'
  422. * capability bit, only advertise overlays.
  423. */
  424. if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
  425. !file_priv->universal_planes)
  426. continue;
  427. if (put_user(plane->base.id, plane_ptr + copied))
  428. return -EFAULT;
  429. copied++;
  430. }
  431. }
  432. plane_resp->count_planes = num_planes;
  433. return 0;
  434. }
  435. int drm_mode_getplane(struct drm_device *dev, void *data,
  436. struct drm_file *file_priv)
  437. {
  438. struct drm_mode_get_plane *plane_resp = data;
  439. struct drm_plane *plane;
  440. uint32_t __user *format_ptr;
  441. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  442. return -EINVAL;
  443. plane = drm_plane_find(dev, file_priv, plane_resp->plane_id);
  444. if (!plane)
  445. return -ENOENT;
  446. drm_modeset_lock(&plane->mutex, NULL);
  447. if (plane->state && plane->state->crtc)
  448. plane_resp->crtc_id = plane->state->crtc->base.id;
  449. else if (!plane->state && plane->crtc)
  450. plane_resp->crtc_id = plane->crtc->base.id;
  451. else
  452. plane_resp->crtc_id = 0;
  453. if (plane->state && plane->state->fb)
  454. plane_resp->fb_id = plane->state->fb->base.id;
  455. else if (!plane->state && plane->fb)
  456. plane_resp->fb_id = plane->fb->base.id;
  457. else
  458. plane_resp->fb_id = 0;
  459. drm_modeset_unlock(&plane->mutex);
  460. plane_resp->plane_id = plane->base.id;
  461. plane_resp->possible_crtcs = plane->possible_crtcs;
  462. plane_resp->gamma_size = 0;
  463. /*
  464. * This ioctl is called twice, once to determine how much space is
  465. * needed, and the 2nd time to fill it.
  466. */
  467. if (plane->format_count &&
  468. (plane_resp->count_format_types >= plane->format_count)) {
  469. format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
  470. if (copy_to_user(format_ptr,
  471. plane->format_types,
  472. sizeof(uint32_t) * plane->format_count)) {
  473. return -EFAULT;
  474. }
  475. }
  476. plane_resp->count_format_types = plane->format_count;
  477. return 0;
  478. }
  479. int drm_plane_check_pixel_format(const struct drm_plane *plane, u32 format)
  480. {
  481. unsigned int i;
  482. for (i = 0; i < plane->format_count; i++) {
  483. if (format == plane->format_types[i])
  484. return 0;
  485. }
  486. return -EINVAL;
  487. }
  488. /*
  489. * setplane_internal - setplane handler for internal callers
  490. *
  491. * Note that we assume an extra reference has already been taken on fb. If the
  492. * update fails, this reference will be dropped before return; if it succeeds,
  493. * the previous framebuffer (if any) will be unreferenced instead.
  494. *
  495. * src_{x,y,w,h} are provided in 16.16 fixed point format
  496. */
  497. static int __setplane_internal(struct drm_plane *plane,
  498. struct drm_crtc *crtc,
  499. struct drm_framebuffer *fb,
  500. int32_t crtc_x, int32_t crtc_y,
  501. uint32_t crtc_w, uint32_t crtc_h,
  502. /* src_{x,y,w,h} values are 16.16 fixed point */
  503. uint32_t src_x, uint32_t src_y,
  504. uint32_t src_w, uint32_t src_h,
  505. struct drm_modeset_acquire_ctx *ctx)
  506. {
  507. int ret = 0;
  508. /* No fb means shut it down */
  509. if (!fb) {
  510. plane->old_fb = plane->fb;
  511. ret = plane->funcs->disable_plane(plane, ctx);
  512. if (!ret) {
  513. plane->crtc = NULL;
  514. plane->fb = NULL;
  515. } else {
  516. plane->old_fb = NULL;
  517. }
  518. goto out;
  519. }
  520. /* Check whether this plane is usable on this CRTC */
  521. if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
  522. DRM_DEBUG_KMS("Invalid crtc for plane\n");
  523. ret = -EINVAL;
  524. goto out;
  525. }
  526. /* Check whether this plane supports the fb pixel format. */
  527. ret = drm_plane_check_pixel_format(plane, fb->format->format);
  528. if (ret) {
  529. struct drm_format_name_buf format_name;
  530. DRM_DEBUG_KMS("Invalid pixel format %s\n",
  531. drm_get_format_name(fb->format->format,
  532. &format_name));
  533. goto out;
  534. }
  535. /* Give drivers some help against integer overflows */
  536. if (crtc_w > INT_MAX ||
  537. crtc_x > INT_MAX - (int32_t) crtc_w ||
  538. crtc_h > INT_MAX ||
  539. crtc_y > INT_MAX - (int32_t) crtc_h) {
  540. DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
  541. crtc_w, crtc_h, crtc_x, crtc_y);
  542. ret = -ERANGE;
  543. goto out;
  544. }
  545. ret = drm_framebuffer_check_src_coords(src_x, src_y, src_w, src_h, fb);
  546. if (ret)
  547. goto out;
  548. plane->old_fb = plane->fb;
  549. ret = plane->funcs->update_plane(plane, crtc, fb,
  550. crtc_x, crtc_y, crtc_w, crtc_h,
  551. src_x, src_y, src_w, src_h, ctx);
  552. if (!ret) {
  553. plane->crtc = crtc;
  554. plane->fb = fb;
  555. fb = NULL;
  556. } else {
  557. plane->old_fb = NULL;
  558. }
  559. out:
  560. if (fb)
  561. drm_framebuffer_put(fb);
  562. if (plane->old_fb)
  563. drm_framebuffer_put(plane->old_fb);
  564. plane->old_fb = NULL;
  565. return ret;
  566. }
  567. static int setplane_internal(struct drm_plane *plane,
  568. struct drm_crtc *crtc,
  569. struct drm_framebuffer *fb,
  570. int32_t crtc_x, int32_t crtc_y,
  571. uint32_t crtc_w, uint32_t crtc_h,
  572. /* src_{x,y,w,h} values are 16.16 fixed point */
  573. uint32_t src_x, uint32_t src_y,
  574. uint32_t src_w, uint32_t src_h)
  575. {
  576. struct drm_modeset_acquire_ctx ctx;
  577. int ret;
  578. drm_modeset_acquire_init(&ctx, 0);
  579. retry:
  580. ret = drm_modeset_lock_all_ctx(plane->dev, &ctx);
  581. if (ret)
  582. goto fail;
  583. ret = __setplane_internal(plane, crtc, fb,
  584. crtc_x, crtc_y, crtc_w, crtc_h,
  585. src_x, src_y, src_w, src_h, &ctx);
  586. fail:
  587. if (ret == -EDEADLK) {
  588. drm_modeset_backoff(&ctx);
  589. goto retry;
  590. }
  591. drm_modeset_drop_locks(&ctx);
  592. drm_modeset_acquire_fini(&ctx);
  593. return ret;
  594. }
  595. int drm_mode_setplane(struct drm_device *dev, void *data,
  596. struct drm_file *file_priv)
  597. {
  598. struct drm_mode_set_plane *plane_req = data;
  599. struct drm_plane *plane;
  600. struct drm_crtc *crtc = NULL;
  601. struct drm_framebuffer *fb = NULL;
  602. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  603. return -EINVAL;
  604. /*
  605. * First, find the plane, crtc, and fb objects. If not available,
  606. * we don't bother to call the driver.
  607. */
  608. plane = drm_plane_find(dev, file_priv, plane_req->plane_id);
  609. if (!plane) {
  610. DRM_DEBUG_KMS("Unknown plane ID %d\n",
  611. plane_req->plane_id);
  612. return -ENOENT;
  613. }
  614. if (plane_req->fb_id) {
  615. fb = drm_framebuffer_lookup(dev, file_priv, plane_req->fb_id);
  616. if (!fb) {
  617. DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
  618. plane_req->fb_id);
  619. return -ENOENT;
  620. }
  621. crtc = drm_crtc_find(dev, file_priv, plane_req->crtc_id);
  622. if (!crtc) {
  623. drm_framebuffer_put(fb);
  624. DRM_DEBUG_KMS("Unknown crtc ID %d\n",
  625. plane_req->crtc_id);
  626. return -ENOENT;
  627. }
  628. }
  629. /*
  630. * setplane_internal will take care of deref'ing either the old or new
  631. * framebuffer depending on success.
  632. */
  633. return setplane_internal(plane, crtc, fb,
  634. plane_req->crtc_x, plane_req->crtc_y,
  635. plane_req->crtc_w, plane_req->crtc_h,
  636. plane_req->src_x, plane_req->src_y,
  637. plane_req->src_w, plane_req->src_h);
  638. }
  639. static int drm_mode_cursor_universal(struct drm_crtc *crtc,
  640. struct drm_mode_cursor2 *req,
  641. struct drm_file *file_priv,
  642. struct drm_modeset_acquire_ctx *ctx)
  643. {
  644. struct drm_device *dev = crtc->dev;
  645. struct drm_framebuffer *fb = NULL;
  646. struct drm_mode_fb_cmd2 fbreq = {
  647. .width = req->width,
  648. .height = req->height,
  649. .pixel_format = DRM_FORMAT_ARGB8888,
  650. .pitches = { req->width * 4 },
  651. .handles = { req->handle },
  652. };
  653. int32_t crtc_x, crtc_y;
  654. uint32_t crtc_w = 0, crtc_h = 0;
  655. uint32_t src_w = 0, src_h = 0;
  656. int ret = 0;
  657. BUG_ON(!crtc->cursor);
  658. WARN_ON(crtc->cursor->crtc != crtc && crtc->cursor->crtc != NULL);
  659. /*
  660. * Obtain fb we'll be using (either new or existing) and take an extra
  661. * reference to it if fb != null. setplane will take care of dropping
  662. * the reference if the plane update fails.
  663. */
  664. if (req->flags & DRM_MODE_CURSOR_BO) {
  665. if (req->handle) {
  666. fb = drm_internal_framebuffer_create(dev, &fbreq, file_priv);
  667. if (IS_ERR(fb)) {
  668. DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
  669. return PTR_ERR(fb);
  670. }
  671. fb->hot_x = req->hot_x;
  672. fb->hot_y = req->hot_y;
  673. } else {
  674. fb = NULL;
  675. }
  676. } else {
  677. fb = crtc->cursor->fb;
  678. if (fb)
  679. drm_framebuffer_get(fb);
  680. }
  681. if (req->flags & DRM_MODE_CURSOR_MOVE) {
  682. crtc_x = req->x;
  683. crtc_y = req->y;
  684. } else {
  685. crtc_x = crtc->cursor_x;
  686. crtc_y = crtc->cursor_y;
  687. }
  688. if (fb) {
  689. crtc_w = fb->width;
  690. crtc_h = fb->height;
  691. src_w = fb->width << 16;
  692. src_h = fb->height << 16;
  693. }
  694. /*
  695. * setplane_internal will take care of deref'ing either the old or new
  696. * framebuffer depending on success.
  697. */
  698. ret = __setplane_internal(crtc->cursor, crtc, fb,
  699. crtc_x, crtc_y, crtc_w, crtc_h,
  700. 0, 0, src_w, src_h, ctx);
  701. /* Update successful; save new cursor position, if necessary */
  702. if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
  703. crtc->cursor_x = req->x;
  704. crtc->cursor_y = req->y;
  705. }
  706. return ret;
  707. }
  708. static int drm_mode_cursor_common(struct drm_device *dev,
  709. struct drm_mode_cursor2 *req,
  710. struct drm_file *file_priv)
  711. {
  712. struct drm_crtc *crtc;
  713. struct drm_modeset_acquire_ctx ctx;
  714. int ret = 0;
  715. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  716. return -EINVAL;
  717. if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
  718. return -EINVAL;
  719. crtc = drm_crtc_find(dev, file_priv, req->crtc_id);
  720. if (!crtc) {
  721. DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
  722. return -ENOENT;
  723. }
  724. drm_modeset_acquire_init(&ctx, 0);
  725. retry:
  726. ret = drm_modeset_lock(&crtc->mutex, &ctx);
  727. if (ret)
  728. goto out;
  729. /*
  730. * If this crtc has a universal cursor plane, call that plane's update
  731. * handler rather than using legacy cursor handlers.
  732. */
  733. if (crtc->cursor) {
  734. ret = drm_modeset_lock(&crtc->cursor->mutex, &ctx);
  735. if (ret)
  736. goto out;
  737. ret = drm_mode_cursor_universal(crtc, req, file_priv, &ctx);
  738. goto out;
  739. }
  740. if (req->flags & DRM_MODE_CURSOR_BO) {
  741. if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
  742. ret = -ENXIO;
  743. goto out;
  744. }
  745. /* Turns off the cursor if handle is 0 */
  746. if (crtc->funcs->cursor_set2)
  747. ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
  748. req->width, req->height, req->hot_x, req->hot_y);
  749. else
  750. ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
  751. req->width, req->height);
  752. }
  753. if (req->flags & DRM_MODE_CURSOR_MOVE) {
  754. if (crtc->funcs->cursor_move) {
  755. ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
  756. } else {
  757. ret = -EFAULT;
  758. goto out;
  759. }
  760. }
  761. out:
  762. if (ret == -EDEADLK) {
  763. drm_modeset_backoff(&ctx);
  764. goto retry;
  765. }
  766. drm_modeset_drop_locks(&ctx);
  767. drm_modeset_acquire_fini(&ctx);
  768. return ret;
  769. }
  770. int drm_mode_cursor_ioctl(struct drm_device *dev,
  771. void *data, struct drm_file *file_priv)
  772. {
  773. struct drm_mode_cursor *req = data;
  774. struct drm_mode_cursor2 new_req;
  775. memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
  776. new_req.hot_x = new_req.hot_y = 0;
  777. return drm_mode_cursor_common(dev, &new_req, file_priv);
  778. }
  779. /*
  780. * Set the cursor configuration based on user request. This implements the 2nd
  781. * version of the cursor ioctl, which allows userspace to additionally specify
  782. * the hotspot of the pointer.
  783. */
  784. int drm_mode_cursor2_ioctl(struct drm_device *dev,
  785. void *data, struct drm_file *file_priv)
  786. {
  787. struct drm_mode_cursor2 *req = data;
  788. return drm_mode_cursor_common(dev, req, file_priv);
  789. }
  790. int drm_mode_page_flip_ioctl(struct drm_device *dev,
  791. void *data, struct drm_file *file_priv)
  792. {
  793. struct drm_mode_crtc_page_flip_target *page_flip = data;
  794. struct drm_crtc *crtc;
  795. struct drm_framebuffer *fb = NULL;
  796. struct drm_pending_vblank_event *e = NULL;
  797. u32 target_vblank = page_flip->sequence;
  798. struct drm_modeset_acquire_ctx ctx;
  799. int ret = -EINVAL;
  800. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  801. return -EINVAL;
  802. if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS)
  803. return -EINVAL;
  804. if (page_flip->sequence != 0 && !(page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET))
  805. return -EINVAL;
  806. /* Only one of the DRM_MODE_PAGE_FLIP_TARGET_ABSOLUTE/RELATIVE flags
  807. * can be specified
  808. */
  809. if ((page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET) == DRM_MODE_PAGE_FLIP_TARGET)
  810. return -EINVAL;
  811. if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
  812. return -EINVAL;
  813. crtc = drm_crtc_find(dev, file_priv, page_flip->crtc_id);
  814. if (!crtc)
  815. return -ENOENT;
  816. if (crtc->funcs->page_flip_target) {
  817. u32 current_vblank;
  818. int r;
  819. r = drm_crtc_vblank_get(crtc);
  820. if (r)
  821. return r;
  822. current_vblank = drm_crtc_vblank_count(crtc);
  823. switch (page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET) {
  824. case DRM_MODE_PAGE_FLIP_TARGET_ABSOLUTE:
  825. if ((int)(target_vblank - current_vblank) > 1) {
  826. DRM_DEBUG("Invalid absolute flip target %u, "
  827. "must be <= %u\n", target_vblank,
  828. current_vblank + 1);
  829. drm_crtc_vblank_put(crtc);
  830. return -EINVAL;
  831. }
  832. break;
  833. case DRM_MODE_PAGE_FLIP_TARGET_RELATIVE:
  834. if (target_vblank != 0 && target_vblank != 1) {
  835. DRM_DEBUG("Invalid relative flip target %u, "
  836. "must be 0 or 1\n", target_vblank);
  837. drm_crtc_vblank_put(crtc);
  838. return -EINVAL;
  839. }
  840. target_vblank += current_vblank;
  841. break;
  842. default:
  843. target_vblank = current_vblank +
  844. !(page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC);
  845. break;
  846. }
  847. } else if (crtc->funcs->page_flip == NULL ||
  848. (page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET)) {
  849. return -EINVAL;
  850. }
  851. drm_modeset_acquire_init(&ctx, 0);
  852. retry:
  853. ret = drm_modeset_lock(&crtc->mutex, &ctx);
  854. if (ret)
  855. goto out;
  856. ret = drm_modeset_lock(&crtc->primary->mutex, &ctx);
  857. if (ret)
  858. goto out;
  859. if (crtc->primary->fb == NULL) {
  860. /* The framebuffer is currently unbound, presumably
  861. * due to a hotplug event, that userspace has not
  862. * yet discovered.
  863. */
  864. ret = -EBUSY;
  865. goto out;
  866. }
  867. fb = drm_framebuffer_lookup(dev, file_priv, page_flip->fb_id);
  868. if (!fb) {
  869. ret = -ENOENT;
  870. goto out;
  871. }
  872. if (crtc->state) {
  873. const struct drm_plane_state *state = crtc->primary->state;
  874. ret = drm_framebuffer_check_src_coords(state->src_x,
  875. state->src_y,
  876. state->src_w,
  877. state->src_h,
  878. fb);
  879. } else {
  880. ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
  881. }
  882. if (ret)
  883. goto out;
  884. if (crtc->primary->fb->format != fb->format) {
  885. DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
  886. ret = -EINVAL;
  887. goto out;
  888. }
  889. if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
  890. e = kzalloc(sizeof *e, GFP_KERNEL);
  891. if (!e) {
  892. ret = -ENOMEM;
  893. goto out;
  894. }
  895. e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
  896. e->event.base.length = sizeof(e->event);
  897. e->event.user_data = page_flip->user_data;
  898. ret = drm_event_reserve_init(dev, file_priv, &e->base, &e->event.base);
  899. if (ret) {
  900. kfree(e);
  901. e = NULL;
  902. goto out;
  903. }
  904. }
  905. crtc->primary->old_fb = crtc->primary->fb;
  906. if (crtc->funcs->page_flip_target)
  907. ret = crtc->funcs->page_flip_target(crtc, fb, e,
  908. page_flip->flags,
  909. target_vblank,
  910. &ctx);
  911. else
  912. ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags,
  913. &ctx);
  914. if (ret) {
  915. if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT)
  916. drm_event_cancel_free(dev, &e->base);
  917. /* Keep the old fb, don't unref it. */
  918. crtc->primary->old_fb = NULL;
  919. } else {
  920. crtc->primary->fb = fb;
  921. /* Unref only the old framebuffer. */
  922. fb = NULL;
  923. }
  924. out:
  925. if (fb)
  926. drm_framebuffer_put(fb);
  927. if (crtc->primary->old_fb)
  928. drm_framebuffer_put(crtc->primary->old_fb);
  929. crtc->primary->old_fb = NULL;
  930. if (ret == -EDEADLK) {
  931. drm_modeset_backoff(&ctx);
  932. goto retry;
  933. }
  934. drm_modeset_drop_locks(&ctx);
  935. drm_modeset_acquire_fini(&ctx);
  936. if (ret && crtc->funcs->page_flip_target)
  937. drm_crtc_vblank_put(crtc);
  938. return ret;
  939. }