vc4_drv.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. /*
  2. * Copyright (C) 2015 Broadcom
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include "drmP.h"
  9. #include "drm_gem_cma_helper.h"
  10. #include <drm/drm_encoder.h>
  11. struct vc4_dev {
  12. struct drm_device *dev;
  13. struct vc4_hdmi *hdmi;
  14. struct vc4_hvs *hvs;
  15. struct vc4_v3d *v3d;
  16. struct vc4_dpi *dpi;
  17. struct vc4_dsi *dsi1;
  18. struct vc4_vec *vec;
  19. struct drm_fbdev_cma *fbdev;
  20. struct vc4_hang_state *hang_state;
  21. /* The kernel-space BO cache. Tracks buffers that have been
  22. * unreferenced by all other users (refcounts of 0!) but not
  23. * yet freed, so we can do cheap allocations.
  24. */
  25. struct vc4_bo_cache {
  26. /* Array of list heads for entries in the BO cache,
  27. * based on number of pages, so we can do O(1) lookups
  28. * in the cache when allocating.
  29. */
  30. struct list_head *size_list;
  31. uint32_t size_list_size;
  32. /* List of all BOs in the cache, ordered by age, so we
  33. * can do O(1) lookups when trying to free old
  34. * buffers.
  35. */
  36. struct list_head time_list;
  37. struct work_struct time_work;
  38. struct timer_list time_timer;
  39. } bo_cache;
  40. struct vc4_bo_stats {
  41. u32 num_allocated;
  42. u32 size_allocated;
  43. u32 num_cached;
  44. u32 size_cached;
  45. } bo_stats;
  46. /* Protects bo_cache and the BO stats. */
  47. struct mutex bo_lock;
  48. /* Sequence number for the last job queued in bin_job_list.
  49. * Starts at 0 (no jobs emitted).
  50. */
  51. uint64_t emit_seqno;
  52. /* Sequence number for the last completed job on the GPU.
  53. * Starts at 0 (no jobs completed).
  54. */
  55. uint64_t finished_seqno;
  56. /* List of all struct vc4_exec_info for jobs to be executed in
  57. * the binner. The first job in the list is the one currently
  58. * programmed into ct0ca for execution.
  59. */
  60. struct list_head bin_job_list;
  61. /* List of all struct vc4_exec_info for jobs that have
  62. * completed binning and are ready for rendering. The first
  63. * job in the list is the one currently programmed into ct1ca
  64. * for execution.
  65. */
  66. struct list_head render_job_list;
  67. /* List of the finished vc4_exec_infos waiting to be freed by
  68. * job_done_work.
  69. */
  70. struct list_head job_done_list;
  71. /* Spinlock used to synchronize the job_list and seqno
  72. * accesses between the IRQ handler and GEM ioctls.
  73. */
  74. spinlock_t job_lock;
  75. wait_queue_head_t job_wait_queue;
  76. struct work_struct job_done_work;
  77. /* List of struct vc4_seqno_cb for callbacks to be made from a
  78. * workqueue when the given seqno is passed.
  79. */
  80. struct list_head seqno_cb_list;
  81. /* The binner overflow memory that's currently set up in
  82. * BPOA/BPOS registers. When overflow occurs and a new one is
  83. * allocated, the previous one will be moved to
  84. * vc4->current_exec's free list.
  85. */
  86. struct vc4_bo *overflow_mem;
  87. struct work_struct overflow_mem_work;
  88. int power_refcount;
  89. /* Mutex controlling the power refcount. */
  90. struct mutex power_lock;
  91. struct {
  92. struct timer_list timer;
  93. struct work_struct reset_work;
  94. } hangcheck;
  95. struct semaphore async_modeset;
  96. };
  97. static inline struct vc4_dev *
  98. to_vc4_dev(struct drm_device *dev)
  99. {
  100. return (struct vc4_dev *)dev->dev_private;
  101. }
  102. struct vc4_bo {
  103. struct drm_gem_cma_object base;
  104. /* seqno of the last job to render using this BO. */
  105. uint64_t seqno;
  106. /* seqno of the last job to use the RCL to write to this BO.
  107. *
  108. * Note that this doesn't include binner overflow memory
  109. * writes.
  110. */
  111. uint64_t write_seqno;
  112. /* List entry for the BO's position in either
  113. * vc4_exec_info->unref_list or vc4_dev->bo_cache.time_list
  114. */
  115. struct list_head unref_head;
  116. /* Time in jiffies when the BO was put in vc4->bo_cache. */
  117. unsigned long free_time;
  118. /* List entry for the BO's position in vc4_dev->bo_cache.size_list */
  119. struct list_head size_head;
  120. /* Struct for shader validation state, if created by
  121. * DRM_IOCTL_VC4_CREATE_SHADER_BO.
  122. */
  123. struct vc4_validated_shader_info *validated_shader;
  124. };
  125. static inline struct vc4_bo *
  126. to_vc4_bo(struct drm_gem_object *bo)
  127. {
  128. return (struct vc4_bo *)bo;
  129. }
  130. struct vc4_seqno_cb {
  131. struct work_struct work;
  132. uint64_t seqno;
  133. void (*func)(struct vc4_seqno_cb *cb);
  134. };
  135. struct vc4_v3d {
  136. struct vc4_dev *vc4;
  137. struct platform_device *pdev;
  138. void __iomem *regs;
  139. };
  140. struct vc4_hvs {
  141. struct platform_device *pdev;
  142. void __iomem *regs;
  143. u32 __iomem *dlist;
  144. /* Memory manager for CRTCs to allocate space in the display
  145. * list. Units are dwords.
  146. */
  147. struct drm_mm dlist_mm;
  148. /* Memory manager for the LBM memory used by HVS scaling. */
  149. struct drm_mm lbm_mm;
  150. spinlock_t mm_lock;
  151. struct drm_mm_node mitchell_netravali_filter;
  152. };
  153. struct vc4_plane {
  154. struct drm_plane base;
  155. };
  156. static inline struct vc4_plane *
  157. to_vc4_plane(struct drm_plane *plane)
  158. {
  159. return (struct vc4_plane *)plane;
  160. }
  161. enum vc4_encoder_type {
  162. VC4_ENCODER_TYPE_NONE,
  163. VC4_ENCODER_TYPE_HDMI,
  164. VC4_ENCODER_TYPE_VEC,
  165. VC4_ENCODER_TYPE_DSI0,
  166. VC4_ENCODER_TYPE_DSI1,
  167. VC4_ENCODER_TYPE_SMI,
  168. VC4_ENCODER_TYPE_DPI,
  169. };
  170. struct vc4_encoder {
  171. struct drm_encoder base;
  172. enum vc4_encoder_type type;
  173. u32 clock_select;
  174. };
  175. static inline struct vc4_encoder *
  176. to_vc4_encoder(struct drm_encoder *encoder)
  177. {
  178. return container_of(encoder, struct vc4_encoder, base);
  179. }
  180. #define V3D_READ(offset) readl(vc4->v3d->regs + offset)
  181. #define V3D_WRITE(offset, val) writel(val, vc4->v3d->regs + offset)
  182. #define HVS_READ(offset) readl(vc4->hvs->regs + offset)
  183. #define HVS_WRITE(offset, val) writel(val, vc4->hvs->regs + offset)
  184. struct vc4_exec_info {
  185. /* Sequence number for this bin/render job. */
  186. uint64_t seqno;
  187. /* Latest write_seqno of any BO that binning depends on. */
  188. uint64_t bin_dep_seqno;
  189. /* Last current addresses the hardware was processing when the
  190. * hangcheck timer checked on us.
  191. */
  192. uint32_t last_ct0ca, last_ct1ca;
  193. /* Kernel-space copy of the ioctl arguments */
  194. struct drm_vc4_submit_cl *args;
  195. /* This is the array of BOs that were looked up at the start of exec.
  196. * Command validation will use indices into this array.
  197. */
  198. struct drm_gem_cma_object **bo;
  199. uint32_t bo_count;
  200. /* List of BOs that are being written by the RCL. Other than
  201. * the binner temporary storage, this is all the BOs written
  202. * by the job.
  203. */
  204. struct drm_gem_cma_object *rcl_write_bo[4];
  205. uint32_t rcl_write_bo_count;
  206. /* Pointers for our position in vc4->job_list */
  207. struct list_head head;
  208. /* List of other BOs used in the job that need to be released
  209. * once the job is complete.
  210. */
  211. struct list_head unref_list;
  212. /* Current unvalidated indices into @bo loaded by the non-hardware
  213. * VC4_PACKET_GEM_HANDLES.
  214. */
  215. uint32_t bo_index[2];
  216. /* This is the BO where we store the validated command lists, shader
  217. * records, and uniforms.
  218. */
  219. struct drm_gem_cma_object *exec_bo;
  220. /**
  221. * This tracks the per-shader-record state (packet 64) that
  222. * determines the length of the shader record and the offset
  223. * it's expected to be found at. It gets read in from the
  224. * command lists.
  225. */
  226. struct vc4_shader_state {
  227. uint32_t addr;
  228. /* Maximum vertex index referenced by any primitive using this
  229. * shader state.
  230. */
  231. uint32_t max_index;
  232. } *shader_state;
  233. /** How many shader states the user declared they were using. */
  234. uint32_t shader_state_size;
  235. /** How many shader state records the validator has seen. */
  236. uint32_t shader_state_count;
  237. bool found_tile_binning_mode_config_packet;
  238. bool found_start_tile_binning_packet;
  239. bool found_increment_semaphore_packet;
  240. bool found_flush;
  241. uint8_t bin_tiles_x, bin_tiles_y;
  242. struct drm_gem_cma_object *tile_bo;
  243. uint32_t tile_alloc_offset;
  244. /**
  245. * Computed addresses pointing into exec_bo where we start the
  246. * bin thread (ct0) and render thread (ct1).
  247. */
  248. uint32_t ct0ca, ct0ea;
  249. uint32_t ct1ca, ct1ea;
  250. /* Pointer to the unvalidated bin CL (if present). */
  251. void *bin_u;
  252. /* Pointers to the shader recs. These paddr gets incremented as CL
  253. * packets are relocated in validate_gl_shader_state, and the vaddrs
  254. * (u and v) get incremented and size decremented as the shader recs
  255. * themselves are validated.
  256. */
  257. void *shader_rec_u;
  258. void *shader_rec_v;
  259. uint32_t shader_rec_p;
  260. uint32_t shader_rec_size;
  261. /* Pointers to the uniform data. These pointers are incremented, and
  262. * size decremented, as each batch of uniforms is uploaded.
  263. */
  264. void *uniforms_u;
  265. void *uniforms_v;
  266. uint32_t uniforms_p;
  267. uint32_t uniforms_size;
  268. };
  269. static inline struct vc4_exec_info *
  270. vc4_first_bin_job(struct vc4_dev *vc4)
  271. {
  272. return list_first_entry_or_null(&vc4->bin_job_list,
  273. struct vc4_exec_info, head);
  274. }
  275. static inline struct vc4_exec_info *
  276. vc4_first_render_job(struct vc4_dev *vc4)
  277. {
  278. return list_first_entry_or_null(&vc4->render_job_list,
  279. struct vc4_exec_info, head);
  280. }
  281. static inline struct vc4_exec_info *
  282. vc4_last_render_job(struct vc4_dev *vc4)
  283. {
  284. if (list_empty(&vc4->render_job_list))
  285. return NULL;
  286. return list_last_entry(&vc4->render_job_list,
  287. struct vc4_exec_info, head);
  288. }
  289. /**
  290. * struct vc4_texture_sample_info - saves the offsets into the UBO for texture
  291. * setup parameters.
  292. *
  293. * This will be used at draw time to relocate the reference to the texture
  294. * contents in p0, and validate that the offset combined with
  295. * width/height/stride/etc. from p1 and p2/p3 doesn't sample outside the BO.
  296. * Note that the hardware treats unprovided config parameters as 0, so not all
  297. * of them need to be set up for every texure sample, and we'll store ~0 as
  298. * the offset to mark the unused ones.
  299. *
  300. * See the VC4 3D architecture guide page 41 ("Texture and Memory Lookup Unit
  301. * Setup") for definitions of the texture parameters.
  302. */
  303. struct vc4_texture_sample_info {
  304. bool is_direct;
  305. uint32_t p_offset[4];
  306. };
  307. /**
  308. * struct vc4_validated_shader_info - information about validated shaders that
  309. * needs to be used from command list validation.
  310. *
  311. * For a given shader, each time a shader state record references it, we need
  312. * to verify that the shader doesn't read more uniforms than the shader state
  313. * record's uniform BO pointer can provide, and we need to apply relocations
  314. * and validate the shader state record's uniforms that define the texture
  315. * samples.
  316. */
  317. struct vc4_validated_shader_info {
  318. uint32_t uniforms_size;
  319. uint32_t uniforms_src_size;
  320. uint32_t num_texture_samples;
  321. struct vc4_texture_sample_info *texture_samples;
  322. uint32_t num_uniform_addr_offsets;
  323. uint32_t *uniform_addr_offsets;
  324. bool is_threaded;
  325. };
  326. /**
  327. * _wait_for - magic (register) wait macro
  328. *
  329. * Does the right thing for modeset paths when run under kdgb or similar atomic
  330. * contexts. Note that it's important that we check the condition again after
  331. * having timed out, since the timeout could be due to preemption or similar and
  332. * we've never had a chance to check the condition before the timeout.
  333. */
  334. #define _wait_for(COND, MS, W) ({ \
  335. unsigned long timeout__ = jiffies + msecs_to_jiffies(MS) + 1; \
  336. int ret__ = 0; \
  337. while (!(COND)) { \
  338. if (time_after(jiffies, timeout__)) { \
  339. if (!(COND)) \
  340. ret__ = -ETIMEDOUT; \
  341. break; \
  342. } \
  343. if (W && drm_can_sleep()) { \
  344. msleep(W); \
  345. } else { \
  346. cpu_relax(); \
  347. } \
  348. } \
  349. ret__; \
  350. })
  351. #define wait_for(COND, MS) _wait_for(COND, MS, 1)
  352. /* vc4_bo.c */
  353. struct drm_gem_object *vc4_create_object(struct drm_device *dev, size_t size);
  354. void vc4_free_object(struct drm_gem_object *gem_obj);
  355. struct vc4_bo *vc4_bo_create(struct drm_device *dev, size_t size,
  356. bool from_cache);
  357. int vc4_dumb_create(struct drm_file *file_priv,
  358. struct drm_device *dev,
  359. struct drm_mode_create_dumb *args);
  360. struct dma_buf *vc4_prime_export(struct drm_device *dev,
  361. struct drm_gem_object *obj, int flags);
  362. int vc4_create_bo_ioctl(struct drm_device *dev, void *data,
  363. struct drm_file *file_priv);
  364. int vc4_create_shader_bo_ioctl(struct drm_device *dev, void *data,
  365. struct drm_file *file_priv);
  366. int vc4_mmap_bo_ioctl(struct drm_device *dev, void *data,
  367. struct drm_file *file_priv);
  368. int vc4_get_hang_state_ioctl(struct drm_device *dev, void *data,
  369. struct drm_file *file_priv);
  370. int vc4_mmap(struct file *filp, struct vm_area_struct *vma);
  371. int vc4_prime_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma);
  372. void *vc4_prime_vmap(struct drm_gem_object *obj);
  373. void vc4_bo_cache_init(struct drm_device *dev);
  374. void vc4_bo_cache_destroy(struct drm_device *dev);
  375. int vc4_bo_stats_debugfs(struct seq_file *m, void *arg);
  376. /* vc4_crtc.c */
  377. extern struct platform_driver vc4_crtc_driver;
  378. int vc4_enable_vblank(struct drm_device *dev, unsigned int crtc_id);
  379. void vc4_disable_vblank(struct drm_device *dev, unsigned int crtc_id);
  380. bool vc4_event_pending(struct drm_crtc *crtc);
  381. int vc4_crtc_debugfs_regs(struct seq_file *m, void *arg);
  382. int vc4_crtc_get_scanoutpos(struct drm_device *dev, unsigned int crtc_id,
  383. unsigned int flags, int *vpos, int *hpos,
  384. ktime_t *stime, ktime_t *etime,
  385. const struct drm_display_mode *mode);
  386. int vc4_crtc_get_vblank_timestamp(struct drm_device *dev, unsigned int crtc_id,
  387. int *max_error, struct timeval *vblank_time,
  388. unsigned flags);
  389. /* vc4_debugfs.c */
  390. int vc4_debugfs_init(struct drm_minor *minor);
  391. /* vc4_drv.c */
  392. void __iomem *vc4_ioremap_regs(struct platform_device *dev, int index);
  393. /* vc4_dpi.c */
  394. extern struct platform_driver vc4_dpi_driver;
  395. int vc4_dpi_debugfs_regs(struct seq_file *m, void *unused);
  396. /* vc4_dsi.c */
  397. extern struct platform_driver vc4_dsi_driver;
  398. int vc4_dsi_debugfs_regs(struct seq_file *m, void *unused);
  399. /* vc4_gem.c */
  400. void vc4_gem_init(struct drm_device *dev);
  401. void vc4_gem_destroy(struct drm_device *dev);
  402. int vc4_submit_cl_ioctl(struct drm_device *dev, void *data,
  403. struct drm_file *file_priv);
  404. int vc4_wait_seqno_ioctl(struct drm_device *dev, void *data,
  405. struct drm_file *file_priv);
  406. int vc4_wait_bo_ioctl(struct drm_device *dev, void *data,
  407. struct drm_file *file_priv);
  408. void vc4_submit_next_bin_job(struct drm_device *dev);
  409. void vc4_submit_next_render_job(struct drm_device *dev);
  410. void vc4_move_job_to_render(struct drm_device *dev, struct vc4_exec_info *exec);
  411. int vc4_wait_for_seqno(struct drm_device *dev, uint64_t seqno,
  412. uint64_t timeout_ns, bool interruptible);
  413. void vc4_job_handle_completed(struct vc4_dev *vc4);
  414. int vc4_queue_seqno_cb(struct drm_device *dev,
  415. struct vc4_seqno_cb *cb, uint64_t seqno,
  416. void (*func)(struct vc4_seqno_cb *cb));
  417. /* vc4_hdmi.c */
  418. extern struct platform_driver vc4_hdmi_driver;
  419. int vc4_hdmi_debugfs_regs(struct seq_file *m, void *unused);
  420. /* vc4_hdmi.c */
  421. extern struct platform_driver vc4_vec_driver;
  422. int vc4_vec_debugfs_regs(struct seq_file *m, void *unused);
  423. /* vc4_irq.c */
  424. irqreturn_t vc4_irq(int irq, void *arg);
  425. void vc4_irq_preinstall(struct drm_device *dev);
  426. int vc4_irq_postinstall(struct drm_device *dev);
  427. void vc4_irq_uninstall(struct drm_device *dev);
  428. void vc4_irq_reset(struct drm_device *dev);
  429. /* vc4_hvs.c */
  430. extern struct platform_driver vc4_hvs_driver;
  431. void vc4_hvs_dump_state(struct drm_device *dev);
  432. int vc4_hvs_debugfs_regs(struct seq_file *m, void *unused);
  433. /* vc4_kms.c */
  434. int vc4_kms_load(struct drm_device *dev);
  435. /* vc4_plane.c */
  436. struct drm_plane *vc4_plane_init(struct drm_device *dev,
  437. enum drm_plane_type type);
  438. u32 vc4_plane_write_dlist(struct drm_plane *plane, u32 __iomem *dlist);
  439. u32 vc4_plane_dlist_size(const struct drm_plane_state *state);
  440. void vc4_plane_async_set_fb(struct drm_plane *plane,
  441. struct drm_framebuffer *fb);
  442. /* vc4_v3d.c */
  443. extern struct platform_driver vc4_v3d_driver;
  444. int vc4_v3d_debugfs_ident(struct seq_file *m, void *unused);
  445. int vc4_v3d_debugfs_regs(struct seq_file *m, void *unused);
  446. /* vc4_validate.c */
  447. int
  448. vc4_validate_bin_cl(struct drm_device *dev,
  449. void *validated,
  450. void *unvalidated,
  451. struct vc4_exec_info *exec);
  452. int
  453. vc4_validate_shader_recs(struct drm_device *dev, struct vc4_exec_info *exec);
  454. struct drm_gem_cma_object *vc4_use_bo(struct vc4_exec_info *exec,
  455. uint32_t hindex);
  456. int vc4_get_rcl(struct drm_device *dev, struct vc4_exec_info *exec);
  457. bool vc4_check_tex_size(struct vc4_exec_info *exec,
  458. struct drm_gem_cma_object *fbo,
  459. uint32_t offset, uint8_t tiling_format,
  460. uint32_t width, uint32_t height, uint8_t cpp);
  461. /* vc4_validate_shader.c */
  462. struct vc4_validated_shader_info *
  463. vc4_validate_shader(struct drm_gem_cma_object *shader_obj);