drm_plane.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091
  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. plane->modifier_count = format_modifier_count;
  178. plane->modifiers = kmalloc_array(format_modifier_count,
  179. sizeof(format_modifiers[0]),
  180. GFP_KERNEL);
  181. if (format_modifier_count && !plane->modifiers) {
  182. DRM_DEBUG_KMS("out of memory when allocating plane\n");
  183. kfree(plane->format_types);
  184. drm_mode_object_unregister(dev, &plane->base);
  185. return -ENOMEM;
  186. }
  187. if (name) {
  188. va_list ap;
  189. va_start(ap, name);
  190. plane->name = kvasprintf(GFP_KERNEL, name, ap);
  191. va_end(ap);
  192. } else {
  193. plane->name = kasprintf(GFP_KERNEL, "plane-%d",
  194. drm_num_planes(dev));
  195. }
  196. if (!plane->name) {
  197. kfree(plane->format_types);
  198. kfree(plane->modifiers);
  199. drm_mode_object_unregister(dev, &plane->base);
  200. return -ENOMEM;
  201. }
  202. memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
  203. plane->format_count = format_count;
  204. memcpy(plane->modifiers, format_modifiers,
  205. format_modifier_count * sizeof(format_modifiers[0]));
  206. plane->possible_crtcs = possible_crtcs;
  207. plane->type = type;
  208. list_add_tail(&plane->head, &config->plane_list);
  209. plane->index = config->num_total_plane++;
  210. if (plane->type == DRM_PLANE_TYPE_OVERLAY)
  211. config->num_overlay_plane++;
  212. drm_object_attach_property(&plane->base,
  213. config->plane_type_property,
  214. plane->type);
  215. if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
  216. drm_object_attach_property(&plane->base, config->prop_fb_id, 0);
  217. drm_object_attach_property(&plane->base, config->prop_in_fence_fd, -1);
  218. drm_object_attach_property(&plane->base, config->prop_crtc_id, 0);
  219. drm_object_attach_property(&plane->base, config->prop_crtc_x, 0);
  220. drm_object_attach_property(&plane->base, config->prop_crtc_y, 0);
  221. drm_object_attach_property(&plane->base, config->prop_crtc_w, 0);
  222. drm_object_attach_property(&plane->base, config->prop_crtc_h, 0);
  223. drm_object_attach_property(&plane->base, config->prop_src_x, 0);
  224. drm_object_attach_property(&plane->base, config->prop_src_y, 0);
  225. drm_object_attach_property(&plane->base, config->prop_src_w, 0);
  226. drm_object_attach_property(&plane->base, config->prop_src_h, 0);
  227. }
  228. if (config->allow_fb_modifiers)
  229. create_in_format_blob(dev, plane);
  230. return 0;
  231. }
  232. EXPORT_SYMBOL(drm_universal_plane_init);
  233. int drm_plane_register_all(struct drm_device *dev)
  234. {
  235. struct drm_plane *plane;
  236. int ret = 0;
  237. drm_for_each_plane(plane, dev) {
  238. if (plane->funcs->late_register)
  239. ret = plane->funcs->late_register(plane);
  240. if (ret)
  241. return ret;
  242. }
  243. return 0;
  244. }
  245. void drm_plane_unregister_all(struct drm_device *dev)
  246. {
  247. struct drm_plane *plane;
  248. drm_for_each_plane(plane, dev) {
  249. if (plane->funcs->early_unregister)
  250. plane->funcs->early_unregister(plane);
  251. }
  252. }
  253. /**
  254. * drm_plane_init - Initialize a legacy plane
  255. * @dev: DRM device
  256. * @plane: plane object to init
  257. * @possible_crtcs: bitmask of possible CRTCs
  258. * @funcs: callbacks for the new plane
  259. * @formats: array of supported formats (DRM_FORMAT\_\*)
  260. * @format_count: number of elements in @formats
  261. * @is_primary: plane type (primary vs overlay)
  262. *
  263. * Legacy API to initialize a DRM plane.
  264. *
  265. * New drivers should call drm_universal_plane_init() instead.
  266. *
  267. * Returns:
  268. * Zero on success, error code on failure.
  269. */
  270. int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
  271. uint32_t possible_crtcs,
  272. const struct drm_plane_funcs *funcs,
  273. const uint32_t *formats, unsigned int format_count,
  274. bool is_primary)
  275. {
  276. enum drm_plane_type type;
  277. type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
  278. return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
  279. formats, format_count,
  280. NULL, type, NULL);
  281. }
  282. EXPORT_SYMBOL(drm_plane_init);
  283. /**
  284. * drm_plane_cleanup - Clean up the core plane usage
  285. * @plane: plane to cleanup
  286. *
  287. * This function cleans up @plane and removes it from the DRM mode setting
  288. * core. Note that the function does *not* free the plane structure itself,
  289. * this is the responsibility of the caller.
  290. */
  291. void drm_plane_cleanup(struct drm_plane *plane)
  292. {
  293. struct drm_device *dev = plane->dev;
  294. drm_modeset_lock_fini(&plane->mutex);
  295. kfree(plane->format_types);
  296. kfree(plane->modifiers);
  297. drm_mode_object_unregister(dev, &plane->base);
  298. BUG_ON(list_empty(&plane->head));
  299. /* Note that the plane_list is considered to be static; should we
  300. * remove the drm_plane at runtime we would have to decrement all
  301. * the indices on the drm_plane after us in the plane_list.
  302. */
  303. list_del(&plane->head);
  304. dev->mode_config.num_total_plane--;
  305. if (plane->type == DRM_PLANE_TYPE_OVERLAY)
  306. dev->mode_config.num_overlay_plane--;
  307. WARN_ON(plane->state && !plane->funcs->atomic_destroy_state);
  308. if (plane->state && plane->funcs->atomic_destroy_state)
  309. plane->funcs->atomic_destroy_state(plane, plane->state);
  310. kfree(plane->name);
  311. memset(plane, 0, sizeof(*plane));
  312. }
  313. EXPORT_SYMBOL(drm_plane_cleanup);
  314. /**
  315. * drm_plane_from_index - find the registered plane at an index
  316. * @dev: DRM device
  317. * @idx: index of registered plane to find for
  318. *
  319. * Given a plane index, return the registered plane from DRM device's
  320. * list of planes with matching index. This is the inverse of drm_plane_index().
  321. */
  322. struct drm_plane *
  323. drm_plane_from_index(struct drm_device *dev, int idx)
  324. {
  325. struct drm_plane *plane;
  326. drm_for_each_plane(plane, dev)
  327. if (idx == plane->index)
  328. return plane;
  329. return NULL;
  330. }
  331. EXPORT_SYMBOL(drm_plane_from_index);
  332. /**
  333. * drm_plane_force_disable - Forcibly disable a plane
  334. * @plane: plane to disable
  335. *
  336. * Forces the plane to be disabled.
  337. *
  338. * Used when the plane's current framebuffer is destroyed,
  339. * and when restoring fbdev mode.
  340. *
  341. * Note that this function is not suitable for atomic drivers, since it doesn't
  342. * wire through the lock acquisition context properly and hence can't handle
  343. * retries or driver private locks. You probably want to use
  344. * drm_atomic_helper_disable_plane() or
  345. * drm_atomic_helper_disable_planes_on_crtc() instead.
  346. */
  347. void drm_plane_force_disable(struct drm_plane *plane)
  348. {
  349. int ret;
  350. if (!plane->fb)
  351. return;
  352. WARN_ON(drm_drv_uses_atomic_modeset(plane->dev));
  353. plane->old_fb = plane->fb;
  354. ret = plane->funcs->disable_plane(plane, NULL);
  355. if (ret) {
  356. DRM_ERROR("failed to disable plane with busy fb\n");
  357. plane->old_fb = NULL;
  358. return;
  359. }
  360. /* disconnect the plane from the fb and crtc: */
  361. drm_framebuffer_put(plane->old_fb);
  362. plane->old_fb = NULL;
  363. plane->fb = NULL;
  364. plane->crtc = NULL;
  365. }
  366. EXPORT_SYMBOL(drm_plane_force_disable);
  367. /**
  368. * drm_mode_plane_set_obj_prop - set the value of a property
  369. * @plane: drm plane object to set property value for
  370. * @property: property to set
  371. * @value: value the property should be set to
  372. *
  373. * This functions sets a given property on a given plane object. This function
  374. * calls the driver's ->set_property callback and changes the software state of
  375. * the property if the callback succeeds.
  376. *
  377. * Returns:
  378. * Zero on success, error code on failure.
  379. */
  380. int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
  381. struct drm_property *property,
  382. uint64_t value)
  383. {
  384. int ret = -EINVAL;
  385. struct drm_mode_object *obj = &plane->base;
  386. if (plane->funcs->set_property)
  387. ret = plane->funcs->set_property(plane, property, value);
  388. if (!ret)
  389. drm_object_property_set_value(obj, property, value);
  390. return ret;
  391. }
  392. EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
  393. int drm_mode_getplane_res(struct drm_device *dev, void *data,
  394. struct drm_file *file_priv)
  395. {
  396. struct drm_mode_get_plane_res *plane_resp = data;
  397. struct drm_mode_config *config;
  398. struct drm_plane *plane;
  399. uint32_t __user *plane_ptr;
  400. int copied = 0;
  401. unsigned num_planes;
  402. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  403. return -EINVAL;
  404. config = &dev->mode_config;
  405. if (file_priv->universal_planes)
  406. num_planes = config->num_total_plane;
  407. else
  408. num_planes = config->num_overlay_plane;
  409. /*
  410. * This ioctl is called twice, once to determine how much space is
  411. * needed, and the 2nd time to fill it.
  412. */
  413. if (num_planes &&
  414. (plane_resp->count_planes >= num_planes)) {
  415. plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
  416. /* Plane lists are invariant, no locking needed. */
  417. drm_for_each_plane(plane, dev) {
  418. /*
  419. * Unless userspace set the 'universal planes'
  420. * capability bit, only advertise overlays.
  421. */
  422. if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
  423. !file_priv->universal_planes)
  424. continue;
  425. if (put_user(plane->base.id, plane_ptr + copied))
  426. return -EFAULT;
  427. copied++;
  428. }
  429. }
  430. plane_resp->count_planes = num_planes;
  431. return 0;
  432. }
  433. int drm_mode_getplane(struct drm_device *dev, void *data,
  434. struct drm_file *file_priv)
  435. {
  436. struct drm_mode_get_plane *plane_resp = data;
  437. struct drm_plane *plane;
  438. uint32_t __user *format_ptr;
  439. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  440. return -EINVAL;
  441. plane = drm_plane_find(dev, file_priv, plane_resp->plane_id);
  442. if (!plane)
  443. return -ENOENT;
  444. drm_modeset_lock(&plane->mutex, NULL);
  445. if (plane->state && plane->state->crtc)
  446. plane_resp->crtc_id = plane->state->crtc->base.id;
  447. else if (!plane->state && plane->crtc)
  448. plane_resp->crtc_id = plane->crtc->base.id;
  449. else
  450. plane_resp->crtc_id = 0;
  451. if (plane->state && plane->state->fb)
  452. plane_resp->fb_id = plane->state->fb->base.id;
  453. else if (!plane->state && plane->fb)
  454. plane_resp->fb_id = plane->fb->base.id;
  455. else
  456. plane_resp->fb_id = 0;
  457. drm_modeset_unlock(&plane->mutex);
  458. plane_resp->plane_id = plane->base.id;
  459. plane_resp->possible_crtcs = plane->possible_crtcs;
  460. plane_resp->gamma_size = 0;
  461. /*
  462. * This ioctl is called twice, once to determine how much space is
  463. * needed, and the 2nd time to fill it.
  464. */
  465. if (plane->format_count &&
  466. (plane_resp->count_format_types >= plane->format_count)) {
  467. format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
  468. if (copy_to_user(format_ptr,
  469. plane->format_types,
  470. sizeof(uint32_t) * plane->format_count)) {
  471. return -EFAULT;
  472. }
  473. }
  474. plane_resp->count_format_types = plane->format_count;
  475. return 0;
  476. }
  477. int drm_plane_check_pixel_format(const struct drm_plane *plane, u32 format)
  478. {
  479. unsigned int i;
  480. for (i = 0; i < plane->format_count; i++) {
  481. if (format == plane->format_types[i])
  482. return 0;
  483. }
  484. return -EINVAL;
  485. }
  486. /*
  487. * setplane_internal - setplane handler for internal callers
  488. *
  489. * Note that we assume an extra reference has already been taken on fb. If the
  490. * update fails, this reference will be dropped before return; if it succeeds,
  491. * the previous framebuffer (if any) will be unreferenced instead.
  492. *
  493. * src_{x,y,w,h} are provided in 16.16 fixed point format
  494. */
  495. static int __setplane_internal(struct drm_plane *plane,
  496. struct drm_crtc *crtc,
  497. struct drm_framebuffer *fb,
  498. int32_t crtc_x, int32_t crtc_y,
  499. uint32_t crtc_w, uint32_t crtc_h,
  500. /* src_{x,y,w,h} values are 16.16 fixed point */
  501. uint32_t src_x, uint32_t src_y,
  502. uint32_t src_w, uint32_t src_h,
  503. struct drm_modeset_acquire_ctx *ctx)
  504. {
  505. int ret = 0;
  506. /* No fb means shut it down */
  507. if (!fb) {
  508. plane->old_fb = plane->fb;
  509. ret = plane->funcs->disable_plane(plane, ctx);
  510. if (!ret) {
  511. plane->crtc = NULL;
  512. plane->fb = NULL;
  513. } else {
  514. plane->old_fb = NULL;
  515. }
  516. goto out;
  517. }
  518. /* Check whether this plane is usable on this CRTC */
  519. if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
  520. DRM_DEBUG_KMS("Invalid crtc for plane\n");
  521. ret = -EINVAL;
  522. goto out;
  523. }
  524. /* Check whether this plane supports the fb pixel format. */
  525. ret = drm_plane_check_pixel_format(plane, fb->format->format);
  526. if (ret) {
  527. struct drm_format_name_buf format_name;
  528. DRM_DEBUG_KMS("Invalid pixel format %s\n",
  529. drm_get_format_name(fb->format->format,
  530. &format_name));
  531. goto out;
  532. }
  533. /* Give drivers some help against integer overflows */
  534. if (crtc_w > INT_MAX ||
  535. crtc_x > INT_MAX - (int32_t) crtc_w ||
  536. crtc_h > INT_MAX ||
  537. crtc_y > INT_MAX - (int32_t) crtc_h) {
  538. DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
  539. crtc_w, crtc_h, crtc_x, crtc_y);
  540. ret = -ERANGE;
  541. goto out;
  542. }
  543. ret = drm_framebuffer_check_src_coords(src_x, src_y, src_w, src_h, fb);
  544. if (ret)
  545. goto out;
  546. plane->old_fb = plane->fb;
  547. ret = plane->funcs->update_plane(plane, crtc, fb,
  548. crtc_x, crtc_y, crtc_w, crtc_h,
  549. src_x, src_y, src_w, src_h, ctx);
  550. if (!ret) {
  551. plane->crtc = crtc;
  552. plane->fb = fb;
  553. fb = NULL;
  554. } else {
  555. plane->old_fb = NULL;
  556. }
  557. out:
  558. if (fb)
  559. drm_framebuffer_put(fb);
  560. if (plane->old_fb)
  561. drm_framebuffer_put(plane->old_fb);
  562. plane->old_fb = NULL;
  563. return ret;
  564. }
  565. static int setplane_internal(struct drm_plane *plane,
  566. struct drm_crtc *crtc,
  567. struct drm_framebuffer *fb,
  568. int32_t crtc_x, int32_t crtc_y,
  569. uint32_t crtc_w, uint32_t crtc_h,
  570. /* src_{x,y,w,h} values are 16.16 fixed point */
  571. uint32_t src_x, uint32_t src_y,
  572. uint32_t src_w, uint32_t src_h)
  573. {
  574. struct drm_modeset_acquire_ctx ctx;
  575. int ret;
  576. drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE);
  577. retry:
  578. ret = drm_modeset_lock_all_ctx(plane->dev, &ctx);
  579. if (ret)
  580. goto fail;
  581. ret = __setplane_internal(plane, crtc, fb,
  582. crtc_x, crtc_y, crtc_w, crtc_h,
  583. src_x, src_y, src_w, src_h, &ctx);
  584. fail:
  585. if (ret == -EDEADLK) {
  586. ret = drm_modeset_backoff(&ctx);
  587. if (!ret)
  588. goto retry;
  589. }
  590. drm_modeset_drop_locks(&ctx);
  591. drm_modeset_acquire_fini(&ctx);
  592. return ret;
  593. }
  594. int drm_mode_setplane(struct drm_device *dev, void *data,
  595. struct drm_file *file_priv)
  596. {
  597. struct drm_mode_set_plane *plane_req = data;
  598. struct drm_plane *plane;
  599. struct drm_crtc *crtc = NULL;
  600. struct drm_framebuffer *fb = NULL;
  601. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  602. return -EINVAL;
  603. /*
  604. * First, find the plane, crtc, and fb objects. If not available,
  605. * we don't bother to call the driver.
  606. */
  607. plane = drm_plane_find(dev, file_priv, plane_req->plane_id);
  608. if (!plane) {
  609. DRM_DEBUG_KMS("Unknown plane ID %d\n",
  610. plane_req->plane_id);
  611. return -ENOENT;
  612. }
  613. if (plane_req->fb_id) {
  614. fb = drm_framebuffer_lookup(dev, file_priv, plane_req->fb_id);
  615. if (!fb) {
  616. DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
  617. plane_req->fb_id);
  618. return -ENOENT;
  619. }
  620. crtc = drm_crtc_find(dev, file_priv, plane_req->crtc_id);
  621. if (!crtc) {
  622. drm_framebuffer_put(fb);
  623. DRM_DEBUG_KMS("Unknown crtc ID %d\n",
  624. plane_req->crtc_id);
  625. return -ENOENT;
  626. }
  627. }
  628. /*
  629. * setplane_internal will take care of deref'ing either the old or new
  630. * framebuffer depending on success.
  631. */
  632. return setplane_internal(plane, crtc, fb,
  633. plane_req->crtc_x, plane_req->crtc_y,
  634. plane_req->crtc_w, plane_req->crtc_h,
  635. plane_req->src_x, plane_req->src_y,
  636. plane_req->src_w, plane_req->src_h);
  637. }
  638. static int drm_mode_cursor_universal(struct drm_crtc *crtc,
  639. struct drm_mode_cursor2 *req,
  640. struct drm_file *file_priv,
  641. struct drm_modeset_acquire_ctx *ctx)
  642. {
  643. struct drm_device *dev = crtc->dev;
  644. struct drm_framebuffer *fb = NULL;
  645. struct drm_mode_fb_cmd2 fbreq = {
  646. .width = req->width,
  647. .height = req->height,
  648. .pixel_format = DRM_FORMAT_ARGB8888,
  649. .pitches = { req->width * 4 },
  650. .handles = { req->handle },
  651. };
  652. int32_t crtc_x, crtc_y;
  653. uint32_t crtc_w = 0, crtc_h = 0;
  654. uint32_t src_w = 0, src_h = 0;
  655. int ret = 0;
  656. BUG_ON(!crtc->cursor);
  657. WARN_ON(crtc->cursor->crtc != crtc && crtc->cursor->crtc != NULL);
  658. /*
  659. * Obtain fb we'll be using (either new or existing) and take an extra
  660. * reference to it if fb != null. setplane will take care of dropping
  661. * the reference if the plane update fails.
  662. */
  663. if (req->flags & DRM_MODE_CURSOR_BO) {
  664. if (req->handle) {
  665. fb = drm_internal_framebuffer_create(dev, &fbreq, file_priv);
  666. if (IS_ERR(fb)) {
  667. DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
  668. return PTR_ERR(fb);
  669. }
  670. fb->hot_x = req->hot_x;
  671. fb->hot_y = req->hot_y;
  672. } else {
  673. fb = NULL;
  674. }
  675. } else {
  676. fb = crtc->cursor->fb;
  677. if (fb)
  678. drm_framebuffer_get(fb);
  679. }
  680. if (req->flags & DRM_MODE_CURSOR_MOVE) {
  681. crtc_x = req->x;
  682. crtc_y = req->y;
  683. } else {
  684. crtc_x = crtc->cursor_x;
  685. crtc_y = crtc->cursor_y;
  686. }
  687. if (fb) {
  688. crtc_w = fb->width;
  689. crtc_h = fb->height;
  690. src_w = fb->width << 16;
  691. src_h = fb->height << 16;
  692. }
  693. /*
  694. * setplane_internal will take care of deref'ing either the old or new
  695. * framebuffer depending on success.
  696. */
  697. ret = __setplane_internal(crtc->cursor, crtc, fb,
  698. crtc_x, crtc_y, crtc_w, crtc_h,
  699. 0, 0, src_w, src_h, ctx);
  700. /* Update successful; save new cursor position, if necessary */
  701. if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
  702. crtc->cursor_x = req->x;
  703. crtc->cursor_y = req->y;
  704. }
  705. return ret;
  706. }
  707. static int drm_mode_cursor_common(struct drm_device *dev,
  708. struct drm_mode_cursor2 *req,
  709. struct drm_file *file_priv)
  710. {
  711. struct drm_crtc *crtc;
  712. struct drm_modeset_acquire_ctx ctx;
  713. int ret = 0;
  714. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  715. return -EINVAL;
  716. if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
  717. return -EINVAL;
  718. crtc = drm_crtc_find(dev, file_priv, req->crtc_id);
  719. if (!crtc) {
  720. DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
  721. return -ENOENT;
  722. }
  723. drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE);
  724. retry:
  725. ret = drm_modeset_lock(&crtc->mutex, &ctx);
  726. if (ret)
  727. goto out;
  728. /*
  729. * If this crtc has a universal cursor plane, call that plane's update
  730. * handler rather than using legacy cursor handlers.
  731. */
  732. if (crtc->cursor) {
  733. ret = drm_modeset_lock(&crtc->cursor->mutex, &ctx);
  734. if (ret)
  735. goto out;
  736. ret = drm_mode_cursor_universal(crtc, req, file_priv, &ctx);
  737. goto out;
  738. }
  739. if (req->flags & DRM_MODE_CURSOR_BO) {
  740. if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
  741. ret = -ENXIO;
  742. goto out;
  743. }
  744. /* Turns off the cursor if handle is 0 */
  745. if (crtc->funcs->cursor_set2)
  746. ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
  747. req->width, req->height, req->hot_x, req->hot_y);
  748. else
  749. ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
  750. req->width, req->height);
  751. }
  752. if (req->flags & DRM_MODE_CURSOR_MOVE) {
  753. if (crtc->funcs->cursor_move) {
  754. ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
  755. } else {
  756. ret = -EFAULT;
  757. goto out;
  758. }
  759. }
  760. out:
  761. if (ret == -EDEADLK) {
  762. ret = drm_modeset_backoff(&ctx);
  763. if (!ret)
  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, DRM_MODESET_ACQUIRE_INTERRUPTIBLE);
  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. ret = drm_modeset_backoff(&ctx);
  932. if (!ret)
  933. goto retry;
  934. }
  935. drm_modeset_drop_locks(&ctx);
  936. drm_modeset_acquire_fini(&ctx);
  937. if (ret && crtc->funcs->page_flip_target)
  938. drm_crtc_vblank_put(crtc);
  939. return ret;
  940. }