vmwgfx_kms.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. /* SPDX-License-Identifier: GPL-2.0 OR MIT */
  2. /**************************************************************************
  3. *
  4. * Copyright (c) 2009-2024 Broadcom. All Rights Reserved. The term
  5. * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the
  9. * "Software"), to deal in the Software without restriction, including
  10. * without limitation the rights to use, copy, modify, merge, publish,
  11. * distribute, sub license, and/or sell copies of the Software, and to
  12. * permit persons to whom the Software is furnished to do so, subject to
  13. * the following conditions:
  14. *
  15. * The above copyright notice and this permission notice (including the
  16. * next paragraph) shall be included in all copies or substantial portions
  17. * of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  22. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  23. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  24. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  25. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. *
  27. **************************************************************************/
  28. #ifndef VMWGFX_KMS_H_
  29. #define VMWGFX_KMS_H_
  30. #include <drm/drm_encoder.h>
  31. #include <drm/drm_framebuffer.h>
  32. #include <drm/drm_probe_helper.h>
  33. #include "vmwgfx_drv.h"
  34. /**
  35. * struct vmw_du_update_plane - Closure structure for vmw_du_helper_plane_update
  36. * @plane: Plane which is being updated.
  37. * @old_state: Old state of plane.
  38. * @dev_priv: Device private.
  39. * @du: Display unit on which to update the plane.
  40. * @vfb: Framebuffer which is blitted to display unit.
  41. * @out_fence: Out fence for resource finish.
  42. * @mutex: The mutex used to protect resource reservation.
  43. * @cpu_blit: True if need cpu blit.
  44. * @intr: Whether to perform waits interruptible if possible.
  45. *
  46. * This structure loosely represent the set of operations needed to perform a
  47. * plane update on a display unit. Implementer will define that functionality
  48. * according to the function callbacks for this structure. In brief it involves
  49. * surface/buffer object validation, populate FIFO commands and command
  50. * submission to the device.
  51. */
  52. struct vmw_du_update_plane {
  53. /**
  54. * @calc_fifo_size: Calculate fifo size.
  55. *
  56. * Determine fifo size for the commands needed for update. The number of
  57. * damage clips on display unit @num_hits will be passed to allocate
  58. * sufficient fifo space.
  59. *
  60. * Return: Fifo size needed
  61. */
  62. uint32_t (*calc_fifo_size)(struct vmw_du_update_plane *update,
  63. uint32_t num_hits);
  64. /**
  65. * @post_prepare: Populate fifo for resource preparation.
  66. *
  67. * Some surface resource or buffer object need some extra cmd submission
  68. * like update GB image for proxy surface and define a GMRFB for screen
  69. * object. That should be done here as this callback will be
  70. * called after FIFO allocation with the address of command buufer.
  71. *
  72. * This callback is optional.
  73. *
  74. * Return: Size of commands populated to command buffer.
  75. */
  76. uint32_t (*post_prepare)(struct vmw_du_update_plane *update, void *cmd);
  77. /**
  78. * @pre_clip: Populate fifo before clip.
  79. *
  80. * This is where pre clip related command should be populated like
  81. * surface copy/DMA, etc.
  82. *
  83. * This callback is optional.
  84. *
  85. * Return: Size of commands populated to command buffer.
  86. */
  87. uint32_t (*pre_clip)(struct vmw_du_update_plane *update, void *cmd,
  88. uint32_t num_hits);
  89. /**
  90. * @clip: Populate fifo for clip.
  91. *
  92. * This is where to populate clips for surface copy/dma or blit commands
  93. * if needed. This will be called times have damage in display unit,
  94. * which is one if doing full update. @clip is the damage in destination
  95. * coordinates which is crtc/DU and @src_x, @src_y is damage clip src in
  96. * framebuffer coordinate.
  97. *
  98. * This callback is optional.
  99. *
  100. * Return: Size of commands populated to command buffer.
  101. */
  102. uint32_t (*clip)(struct vmw_du_update_plane *update, void *cmd,
  103. struct drm_rect *clip, uint32_t src_x, uint32_t src_y);
  104. /**
  105. * @post_clip: Populate fifo after clip.
  106. *
  107. * This is where to populate display unit update commands or blit
  108. * commands.
  109. *
  110. * Return: Size of commands populated to command buffer.
  111. */
  112. uint32_t (*post_clip)(struct vmw_du_update_plane *update, void *cmd,
  113. struct drm_rect *bb);
  114. struct drm_plane *plane;
  115. struct drm_plane_state *old_state;
  116. struct vmw_private *dev_priv;
  117. struct vmw_display_unit *du;
  118. struct vmw_framebuffer *vfb;
  119. struct vmw_fence_obj **out_fence;
  120. struct mutex *mutex;
  121. bool intr;
  122. };
  123. /**
  124. * struct vmw_du_update_plane_surface - closure structure for surface
  125. * @base: base closure structure.
  126. * @cmd_start: FIFO command start address (used by SOU only).
  127. */
  128. struct vmw_du_update_plane_surface {
  129. struct vmw_du_update_plane base;
  130. /* This member is to handle special case SOU surface update */
  131. void *cmd_start;
  132. };
  133. /**
  134. * struct vmw_du_update_plane_buffer - Closure structure for buffer object
  135. * @base: Base closure structure.
  136. * @fb_left: x1 for fb damage bounding box.
  137. * @fb_top: y1 for fb damage bounding box.
  138. */
  139. struct vmw_du_update_plane_buffer {
  140. struct vmw_du_update_plane base;
  141. int fb_left, fb_top;
  142. };
  143. /**
  144. * struct vmw_kms_dirty - closure structure for the vmw_kms_helper_dirty
  145. * function.
  146. *
  147. * @fifo_commit: Callback that is called once for each display unit after
  148. * all clip rects. This function must commit the fifo space reserved by the
  149. * helper. Set up by the caller.
  150. * @clip: Callback that is called for each cliprect on each display unit.
  151. * Set up by the caller.
  152. * @fifo_reserve_size: Fifo size that the helper should try to allocat for
  153. * each display unit. Set up by the caller.
  154. * @dev_priv: Pointer to the device private. Set up by the helper.
  155. * @unit: The current display unit. Set up by the helper before a call to @clip.
  156. * @cmd: The allocated fifo space. Set up by the helper before the first @clip
  157. * call.
  158. * @crtc: The crtc for which to build dirty commands.
  159. * @num_hits: Number of clip rect commands for this display unit.
  160. * Cleared by the helper before the first @clip call. Updated by the @clip
  161. * callback.
  162. * @fb_x: Clip rect left side in framebuffer coordinates.
  163. * @fb_y: Clip rect right side in framebuffer coordinates.
  164. * @unit_x1: Clip rect left side in crtc coordinates.
  165. * @unit_y1: Clip rect top side in crtc coordinates.
  166. * @unit_x2: Clip rect right side in crtc coordinates.
  167. * @unit_y2: Clip rect bottom side in crtc coordinates.
  168. *
  169. * The clip rect coordinates are updated by the helper for each @clip call.
  170. * Note that this may be derived from if more info needs to be passed between
  171. * helper caller and helper callbacks.
  172. */
  173. struct vmw_kms_dirty {
  174. void (*fifo_commit)(struct vmw_kms_dirty *);
  175. void (*clip)(struct vmw_kms_dirty *);
  176. size_t fifo_reserve_size;
  177. struct vmw_private *dev_priv;
  178. struct vmw_display_unit *unit;
  179. void *cmd;
  180. struct drm_crtc *crtc;
  181. u32 num_hits;
  182. s32 fb_x;
  183. s32 fb_y;
  184. s32 unit_x1;
  185. s32 unit_y1;
  186. s32 unit_x2;
  187. s32 unit_y2;
  188. };
  189. #define vmw_framebuffer_to_vfb(x) \
  190. container_of(x, struct vmw_framebuffer, base)
  191. #define vmw_framebuffer_to_vfbs(x) \
  192. container_of(x, struct vmw_framebuffer_surface, base.base)
  193. #define vmw_framebuffer_to_vfbd(x) \
  194. container_of(x, struct vmw_framebuffer_bo, base.base)
  195. /**
  196. * Base class for framebuffers
  197. *
  198. * @pin is called the when ever a crtc uses this framebuffer
  199. * @unpin is called
  200. */
  201. struct vmw_framebuffer {
  202. struct drm_framebuffer base;
  203. bool bo;
  204. };
  205. struct vmw_framebuffer_surface {
  206. struct vmw_framebuffer base;
  207. struct vmw_user_object uo;
  208. };
  209. struct vmw_framebuffer_bo {
  210. struct vmw_framebuffer base;
  211. struct vmw_bo *buffer;
  212. };
  213. static const uint32_t __maybe_unused vmw_primary_plane_formats[] = {
  214. DRM_FORMAT_XRGB8888,
  215. DRM_FORMAT_ARGB8888,
  216. DRM_FORMAT_RGB565,
  217. DRM_FORMAT_XRGB1555,
  218. };
  219. static const uint32_t __maybe_unused vmw_cursor_plane_formats[] = {
  220. DRM_FORMAT_ARGB8888,
  221. };
  222. #define vmw_crtc_state_to_vcs(x) container_of(x, struct vmw_crtc_state, base)
  223. #define vmw_plane_state_to_vps(x) container_of(x, struct vmw_plane_state, base)
  224. #define vmw_connector_state_to_vcs(x) \
  225. container_of(x, struct vmw_connector_state, base)
  226. #define vmw_plane_to_vcp(x) container_of(x, struct vmw_cursor_plane, base)
  227. /**
  228. * Derived class for crtc state object
  229. *
  230. * @base DRM crtc object
  231. */
  232. struct vmw_crtc_state {
  233. struct drm_crtc_state base;
  234. };
  235. struct vmw_cursor_plane_state {
  236. struct vmw_bo *bo;
  237. s32 hotspot_x;
  238. s32 hotspot_y;
  239. };
  240. /**
  241. * Derived class for plane state object
  242. *
  243. * @base DRM plane object
  244. * @surf Display surface for STDU
  245. * @bo display bo for SOU
  246. * @content_fb_type Used by STDU.
  247. * @bo_size Size of the bo, used by Screen Object Display Unit
  248. * @pinned pin count for STDU display surface
  249. */
  250. struct vmw_plane_state {
  251. struct drm_plane_state base;
  252. struct vmw_user_object uo;
  253. int content_fb_type;
  254. unsigned long bo_size;
  255. int pinned;
  256. /* For CPU Blit */
  257. unsigned int cpp;
  258. bool surf_mapped;
  259. struct vmw_cursor_plane_state cursor;
  260. };
  261. /**
  262. * Derived class for connector state object
  263. *
  264. * @base DRM connector object
  265. * @is_implicit connector property
  266. *
  267. */
  268. struct vmw_connector_state {
  269. struct drm_connector_state base;
  270. /**
  271. * @gui_x:
  272. *
  273. * vmwgfx connector property representing the x position of this display
  274. * unit (connector is synonymous to display unit) in overall topology.
  275. * This is what the device expect as xRoot while creating screen.
  276. */
  277. int gui_x;
  278. /**
  279. * @gui_y:
  280. *
  281. * vmwgfx connector property representing the y position of this display
  282. * unit (connector is synonymous to display unit) in overall topology.
  283. * This is what the device expect as yRoot while creating screen.
  284. */
  285. int gui_y;
  286. };
  287. /**
  288. * Derived class for cursor plane object
  289. *
  290. * @base DRM plane object
  291. * @cursor.cursor_mobs Cursor mobs available for re-use
  292. */
  293. struct vmw_cursor_plane {
  294. struct drm_plane base;
  295. struct vmw_bo *cursor_mobs[3];
  296. };
  297. /**
  298. * Base class display unit.
  299. *
  300. * Since the SVGA hw doesn't have a concept of a crtc, encoder or connector
  301. * so the display unit is all of them at the same time. This is true for both
  302. * legacy multimon and screen objects.
  303. */
  304. struct vmw_display_unit {
  305. struct drm_crtc crtc;
  306. struct drm_encoder encoder;
  307. struct drm_connector connector;
  308. struct drm_plane primary;
  309. struct vmw_cursor_plane cursor;
  310. struct vmw_surface *cursor_surface;
  311. size_t cursor_age;
  312. int cursor_x;
  313. int cursor_y;
  314. int hotspot_x;
  315. int hotspot_y;
  316. s32 core_hotspot_x;
  317. s32 core_hotspot_y;
  318. unsigned unit;
  319. /*
  320. * Prefered mode tracking.
  321. */
  322. unsigned pref_width;
  323. unsigned pref_height;
  324. bool pref_active;
  325. /*
  326. * Gui positioning
  327. */
  328. int gui_x;
  329. int gui_y;
  330. bool is_implicit;
  331. int set_gui_x;
  332. int set_gui_y;
  333. struct {
  334. struct work_struct crc_generator_work;
  335. struct hrtimer timer;
  336. ktime_t period_ns;
  337. /* protects concurrent access to the vblank handler */
  338. atomic_t atomic_lock;
  339. /* protected by @atomic_lock */
  340. bool crc_enabled;
  341. struct vmw_surface *surface;
  342. /* protects concurrent access to the crc worker */
  343. spinlock_t crc_state_lock;
  344. /* protected by @crc_state_lock */
  345. bool crc_pending;
  346. u64 frame_start;
  347. u64 frame_end;
  348. } vkms;
  349. };
  350. #define vmw_crtc_to_du(x) \
  351. container_of(x, struct vmw_display_unit, crtc)
  352. #define vmw_connector_to_du(x) \
  353. container_of(x, struct vmw_display_unit, connector)
  354. /*
  355. * Shared display unit functions - vmwgfx_kms.c
  356. */
  357. void vmw_du_init(struct vmw_display_unit *du);
  358. void vmw_du_cleanup(struct vmw_display_unit *du);
  359. void vmw_du_crtc_save(struct drm_crtc *crtc);
  360. void vmw_du_crtc_restore(struct drm_crtc *crtc);
  361. int vmw_du_crtc_gamma_set(struct drm_crtc *crtc,
  362. u16 *r, u16 *g, u16 *b,
  363. uint32_t size,
  364. struct drm_modeset_acquire_ctx *ctx);
  365. int vmw_du_connector_set_property(struct drm_connector *connector,
  366. struct drm_property *property,
  367. uint64_t val);
  368. int vmw_du_connector_atomic_set_property(struct drm_connector *connector,
  369. struct drm_connector_state *state,
  370. struct drm_property *property,
  371. uint64_t val);
  372. int
  373. vmw_du_connector_atomic_get_property(struct drm_connector *connector,
  374. const struct drm_connector_state *state,
  375. struct drm_property *property,
  376. uint64_t *val);
  377. int vmw_du_connector_dpms(struct drm_connector *connector, int mode);
  378. void vmw_du_connector_save(struct drm_connector *connector);
  379. void vmw_du_connector_restore(struct drm_connector *connector);
  380. enum drm_connector_status
  381. vmw_du_connector_detect(struct drm_connector *connector, bool force);
  382. int vmw_kms_helper_dirty(struct vmw_private *dev_priv,
  383. struct vmw_framebuffer *framebuffer,
  384. const struct drm_clip_rect *clips,
  385. const struct drm_vmw_rect *vclips,
  386. s32 dest_x, s32 dest_y,
  387. int num_clips,
  388. int increment,
  389. struct vmw_kms_dirty *dirty);
  390. enum drm_mode_status vmw_connector_mode_valid(struct drm_connector *connector,
  391. struct drm_display_mode *mode);
  392. int vmw_connector_get_modes(struct drm_connector *connector);
  393. void vmw_kms_helper_validation_finish(struct vmw_private *dev_priv,
  394. struct drm_file *file_priv,
  395. struct vmw_validation_context *ctx,
  396. struct vmw_fence_obj **out_fence,
  397. struct drm_vmw_fence_rep __user *
  398. user_fence_rep);
  399. int vmw_kms_readback(struct vmw_private *dev_priv,
  400. struct drm_file *file_priv,
  401. struct vmw_framebuffer *vfb,
  402. struct drm_vmw_fence_rep __user *user_fence_rep,
  403. struct drm_vmw_rect *vclips,
  404. uint32_t num_clips);
  405. struct vmw_framebuffer *
  406. vmw_kms_new_framebuffer(struct vmw_private *dev_priv,
  407. struct vmw_user_object *uo,
  408. const struct drm_mode_fb_cmd2 *mode_cmd);
  409. void vmw_guess_mode_timing(struct drm_display_mode *mode);
  410. void vmw_kms_update_implicit_fb(struct vmw_private *dev_priv);
  411. void vmw_kms_create_implicit_placement_property(struct vmw_private *dev_priv);
  412. /* Universal Plane Helpers */
  413. void vmw_du_primary_plane_destroy(struct drm_plane *plane);
  414. void vmw_du_cursor_plane_destroy(struct drm_plane *plane);
  415. /* Atomic Helpers */
  416. int vmw_du_primary_plane_atomic_check(struct drm_plane *plane,
  417. struct drm_atomic_state *state);
  418. int vmw_du_cursor_plane_atomic_check(struct drm_plane *plane,
  419. struct drm_atomic_state *state);
  420. void vmw_du_cursor_plane_atomic_update(struct drm_plane *plane,
  421. struct drm_atomic_state *state);
  422. int vmw_du_cursor_plane_prepare_fb(struct drm_plane *plane,
  423. struct drm_plane_state *new_state);
  424. void vmw_du_cursor_plane_cleanup_fb(struct drm_plane *plane,
  425. struct drm_plane_state *old_state);
  426. void vmw_du_plane_cleanup_fb(struct drm_plane *plane,
  427. struct drm_plane_state *old_state);
  428. void vmw_du_plane_reset(struct drm_plane *plane);
  429. struct drm_plane_state *vmw_du_plane_duplicate_state(struct drm_plane *plane);
  430. void vmw_du_plane_destroy_state(struct drm_plane *plane,
  431. struct drm_plane_state *state);
  432. void vmw_du_plane_unpin_surf(struct vmw_plane_state *vps);
  433. int vmw_du_crtc_atomic_check(struct drm_crtc *crtc,
  434. struct drm_atomic_state *state);
  435. void vmw_du_crtc_atomic_begin(struct drm_crtc *crtc,
  436. struct drm_atomic_state *state);
  437. void vmw_du_crtc_reset(struct drm_crtc *crtc);
  438. struct drm_crtc_state *vmw_du_crtc_duplicate_state(struct drm_crtc *crtc);
  439. void vmw_du_crtc_destroy_state(struct drm_crtc *crtc,
  440. struct drm_crtc_state *state);
  441. void vmw_du_connector_reset(struct drm_connector *connector);
  442. struct drm_connector_state *
  443. vmw_du_connector_duplicate_state(struct drm_connector *connector);
  444. void vmw_du_connector_destroy_state(struct drm_connector *connector,
  445. struct drm_connector_state *state);
  446. /*
  447. * Legacy display unit functions - vmwgfx_ldu.c
  448. */
  449. int vmw_kms_ldu_init_display(struct vmw_private *dev_priv);
  450. int vmw_kms_ldu_close_display(struct vmw_private *dev_priv);
  451. int vmw_kms_update_proxy(struct vmw_resource *res,
  452. const struct drm_clip_rect *clips,
  453. unsigned num_clips,
  454. int increment);
  455. /*
  456. * Screen Objects display functions - vmwgfx_scrn.c
  457. */
  458. int vmw_kms_sou_init_display(struct vmw_private *dev_priv);
  459. int vmw_kms_sou_do_surface_dirty(struct vmw_private *dev_priv,
  460. struct vmw_framebuffer *framebuffer,
  461. struct drm_clip_rect *clips,
  462. struct drm_vmw_rect *vclips,
  463. struct vmw_resource *srf,
  464. s32 dest_x,
  465. s32 dest_y,
  466. unsigned num_clips, int inc,
  467. struct vmw_fence_obj **out_fence,
  468. struct drm_crtc *crtc);
  469. int vmw_kms_sou_do_bo_dirty(struct vmw_private *dev_priv,
  470. struct vmw_framebuffer *framebuffer,
  471. struct drm_clip_rect *clips,
  472. struct drm_vmw_rect *vclips,
  473. unsigned int num_clips, int increment,
  474. bool interruptible,
  475. struct vmw_fence_obj **out_fence,
  476. struct drm_crtc *crtc);
  477. int vmw_kms_sou_readback(struct vmw_private *dev_priv,
  478. struct drm_file *file_priv,
  479. struct vmw_framebuffer *vfb,
  480. struct drm_vmw_fence_rep __user *user_fence_rep,
  481. struct drm_vmw_rect *vclips,
  482. uint32_t num_clips,
  483. struct drm_crtc *crtc);
  484. /*
  485. * Screen Target Display Unit functions - vmwgfx_stdu.c
  486. */
  487. int vmw_kms_stdu_init_display(struct vmw_private *dev_priv);
  488. int vmw_kms_stdu_surface_dirty(struct vmw_private *dev_priv,
  489. struct vmw_framebuffer *framebuffer,
  490. struct drm_clip_rect *clips,
  491. struct drm_vmw_rect *vclips,
  492. struct vmw_resource *srf,
  493. s32 dest_x,
  494. s32 dest_y,
  495. unsigned num_clips, int inc,
  496. struct vmw_fence_obj **out_fence,
  497. struct drm_crtc *crtc);
  498. int vmw_kms_stdu_readback(struct vmw_private *dev_priv,
  499. struct drm_file *file_priv,
  500. struct vmw_framebuffer *vfb,
  501. struct drm_vmw_fence_rep __user *user_fence_rep,
  502. struct drm_clip_rect *clips,
  503. struct drm_vmw_rect *vclips,
  504. uint32_t num_clips,
  505. int increment,
  506. struct drm_crtc *crtc);
  507. int vmw_du_helper_plane_update(struct vmw_du_update_plane *update);
  508. /**
  509. * vmw_du_translate_to_crtc - Translate a rect from framebuffer to crtc
  510. * @state: Plane state.
  511. * @r: Rectangle to translate.
  512. */
  513. static inline void vmw_du_translate_to_crtc(struct drm_plane_state *state,
  514. struct drm_rect *r)
  515. {
  516. int translate_crtc_x = -((state->src_x >> 16) - state->crtc_x);
  517. int translate_crtc_y = -((state->src_y >> 16) - state->crtc_y);
  518. drm_rect_translate(r, translate_crtc_x, translate_crtc_y);
  519. }
  520. #endif