vmwgfx_kms.h 16 KB

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