vc4_drv.h 16 KB

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