vc4_drv.h 16 KB

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