drm_plane.c 30 KB

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