drm_plane.c 30 KB

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