vc4_drv.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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 to this BO. */
  103. uint64_t seqno;
  104. /* List entry for the BO's position in either
  105. * vc4_exec_info->unref_list or vc4_dev->bo_cache.time_list
  106. */
  107. struct list_head unref_head;
  108. /* Time in jiffies when the BO was put in vc4->bo_cache. */
  109. unsigned long free_time;
  110. /* List entry for the BO's position in vc4_dev->bo_cache.size_list */
  111. struct list_head size_head;
  112. /* Struct for shader validation state, if created by
  113. * DRM_IOCTL_VC4_CREATE_SHADER_BO.
  114. */
  115. struct vc4_validated_shader_info *validated_shader;
  116. };
  117. static inline struct vc4_bo *
  118. to_vc4_bo(struct drm_gem_object *bo)
  119. {
  120. return (struct vc4_bo *)bo;
  121. }
  122. struct vc4_seqno_cb {
  123. struct work_struct work;
  124. uint64_t seqno;
  125. void (*func)(struct vc4_seqno_cb *cb);
  126. };
  127. struct vc4_v3d {
  128. struct vc4_dev *vc4;
  129. struct platform_device *pdev;
  130. void __iomem *regs;
  131. };
  132. struct vc4_hvs {
  133. struct platform_device *pdev;
  134. void __iomem *regs;
  135. u32 __iomem *dlist;
  136. /* Memory manager for CRTCs to allocate space in the display
  137. * list. Units are dwords.
  138. */
  139. struct drm_mm dlist_mm;
  140. /* Memory manager for the LBM memory used by HVS scaling. */
  141. struct drm_mm lbm_mm;
  142. spinlock_t mm_lock;
  143. struct drm_mm_node mitchell_netravali_filter;
  144. };
  145. struct vc4_plane {
  146. struct drm_plane base;
  147. };
  148. static inline struct vc4_plane *
  149. to_vc4_plane(struct drm_plane *plane)
  150. {
  151. return (struct vc4_plane *)plane;
  152. }
  153. enum vc4_encoder_type {
  154. VC4_ENCODER_TYPE_HDMI,
  155. VC4_ENCODER_TYPE_VEC,
  156. VC4_ENCODER_TYPE_DSI0,
  157. VC4_ENCODER_TYPE_DSI1,
  158. VC4_ENCODER_TYPE_SMI,
  159. VC4_ENCODER_TYPE_DPI,
  160. };
  161. struct vc4_encoder {
  162. struct drm_encoder base;
  163. enum vc4_encoder_type type;
  164. u32 clock_select;
  165. };
  166. static inline struct vc4_encoder *
  167. to_vc4_encoder(struct drm_encoder *encoder)
  168. {
  169. return container_of(encoder, struct vc4_encoder, base);
  170. }
  171. #define V3D_READ(offset) readl(vc4->v3d->regs + offset)
  172. #define V3D_WRITE(offset, val) writel(val, vc4->v3d->regs + offset)
  173. #define HVS_READ(offset) readl(vc4->hvs->regs + offset)
  174. #define HVS_WRITE(offset, val) writel(val, vc4->hvs->regs + offset)
  175. struct vc4_exec_info {
  176. /* Sequence number for this bin/render job. */
  177. uint64_t seqno;
  178. /* Last current addresses the hardware was processing when the
  179. * hangcheck timer checked on us.
  180. */
  181. uint32_t last_ct0ca, last_ct1ca;
  182. /* Kernel-space copy of the ioctl arguments */
  183. struct drm_vc4_submit_cl *args;
  184. /* This is the array of BOs that were looked up at the start of exec.
  185. * Command validation will use indices into this array.
  186. */
  187. struct drm_gem_cma_object **bo;
  188. uint32_t bo_count;
  189. /* Pointers for our position in vc4->job_list */
  190. struct list_head head;
  191. /* List of other BOs used in the job that need to be released
  192. * once the job is complete.
  193. */
  194. struct list_head unref_list;
  195. /* Current unvalidated indices into @bo loaded by the non-hardware
  196. * VC4_PACKET_GEM_HANDLES.
  197. */
  198. uint32_t bo_index[2];
  199. /* This is the BO where we store the validated command lists, shader
  200. * records, and uniforms.
  201. */
  202. struct drm_gem_cma_object *exec_bo;
  203. /**
  204. * This tracks the per-shader-record state (packet 64) that
  205. * determines the length of the shader record and the offset
  206. * it's expected to be found at. It gets read in from the
  207. * command lists.
  208. */
  209. struct vc4_shader_state {
  210. uint32_t addr;
  211. /* Maximum vertex index referenced by any primitive using this
  212. * shader state.
  213. */
  214. uint32_t max_index;
  215. } *shader_state;
  216. /** How many shader states the user declared they were using. */
  217. uint32_t shader_state_size;
  218. /** How many shader state records the validator has seen. */
  219. uint32_t shader_state_count;
  220. bool found_tile_binning_mode_config_packet;
  221. bool found_start_tile_binning_packet;
  222. bool found_increment_semaphore_packet;
  223. bool found_flush;
  224. uint8_t bin_tiles_x, bin_tiles_y;
  225. struct drm_gem_cma_object *tile_bo;
  226. uint32_t tile_alloc_offset;
  227. /**
  228. * Computed addresses pointing into exec_bo where we start the
  229. * bin thread (ct0) and render thread (ct1).
  230. */
  231. uint32_t ct0ca, ct0ea;
  232. uint32_t ct1ca, ct1ea;
  233. /* Pointer to the unvalidated bin CL (if present). */
  234. void *bin_u;
  235. /* Pointers to the shader recs. These paddr gets incremented as CL
  236. * packets are relocated in validate_gl_shader_state, and the vaddrs
  237. * (u and v) get incremented and size decremented as the shader recs
  238. * themselves are validated.
  239. */
  240. void *shader_rec_u;
  241. void *shader_rec_v;
  242. uint32_t shader_rec_p;
  243. uint32_t shader_rec_size;
  244. /* Pointers to the uniform data. These pointers are incremented, and
  245. * size decremented, as each batch of uniforms is uploaded.
  246. */
  247. void *uniforms_u;
  248. void *uniforms_v;
  249. uint32_t uniforms_p;
  250. uint32_t uniforms_size;
  251. };
  252. static inline struct vc4_exec_info *
  253. vc4_first_bin_job(struct vc4_dev *vc4)
  254. {
  255. if (list_empty(&vc4->bin_job_list))
  256. return NULL;
  257. return list_first_entry(&vc4->bin_job_list, struct vc4_exec_info, head);
  258. }
  259. static inline struct vc4_exec_info *
  260. vc4_first_render_job(struct vc4_dev *vc4)
  261. {
  262. if (list_empty(&vc4->render_job_list))
  263. return NULL;
  264. return list_first_entry(&vc4->render_job_list,
  265. struct vc4_exec_info, head);
  266. }
  267. /**
  268. * struct vc4_texture_sample_info - saves the offsets into the UBO for texture
  269. * setup parameters.
  270. *
  271. * This will be used at draw time to relocate the reference to the texture
  272. * contents in p0, and validate that the offset combined with
  273. * width/height/stride/etc. from p1 and p2/p3 doesn't sample outside the BO.
  274. * Note that the hardware treats unprovided config parameters as 0, so not all
  275. * of them need to be set up for every texure sample, and we'll store ~0 as
  276. * the offset to mark the unused ones.
  277. *
  278. * See the VC4 3D architecture guide page 41 ("Texture and Memory Lookup Unit
  279. * Setup") for definitions of the texture parameters.
  280. */
  281. struct vc4_texture_sample_info {
  282. bool is_direct;
  283. uint32_t p_offset[4];
  284. };
  285. /**
  286. * struct vc4_validated_shader_info - information about validated shaders that
  287. * needs to be used from command list validation.
  288. *
  289. * For a given shader, each time a shader state record references it, we need
  290. * to verify that the shader doesn't read more uniforms than the shader state
  291. * record's uniform BO pointer can provide, and we need to apply relocations
  292. * and validate the shader state record's uniforms that define the texture
  293. * samples.
  294. */
  295. struct vc4_validated_shader_info {
  296. uint32_t uniforms_size;
  297. uint32_t uniforms_src_size;
  298. uint32_t num_texture_samples;
  299. struct vc4_texture_sample_info *texture_samples;
  300. };
  301. /**
  302. * _wait_for - magic (register) wait macro
  303. *
  304. * Does the right thing for modeset paths when run under kdgb or similar atomic
  305. * contexts. Note that it's important that we check the condition again after
  306. * having timed out, since the timeout could be due to preemption or similar and
  307. * we've never had a chance to check the condition before the timeout.
  308. */
  309. #define _wait_for(COND, MS, W) ({ \
  310. unsigned long timeout__ = jiffies + msecs_to_jiffies(MS) + 1; \
  311. int ret__ = 0; \
  312. while (!(COND)) { \
  313. if (time_after(jiffies, timeout__)) { \
  314. if (!(COND)) \
  315. ret__ = -ETIMEDOUT; \
  316. break; \
  317. } \
  318. if (W && drm_can_sleep()) { \
  319. msleep(W); \
  320. } else { \
  321. cpu_relax(); \
  322. } \
  323. } \
  324. ret__; \
  325. })
  326. #define wait_for(COND, MS) _wait_for(COND, MS, 1)
  327. /* vc4_bo.c */
  328. struct drm_gem_object *vc4_create_object(struct drm_device *dev, size_t size);
  329. void vc4_free_object(struct drm_gem_object *gem_obj);
  330. struct vc4_bo *vc4_bo_create(struct drm_device *dev, size_t size,
  331. bool from_cache);
  332. int vc4_dumb_create(struct drm_file *file_priv,
  333. struct drm_device *dev,
  334. struct drm_mode_create_dumb *args);
  335. struct dma_buf *vc4_prime_export(struct drm_device *dev,
  336. struct drm_gem_object *obj, int flags);
  337. int vc4_create_bo_ioctl(struct drm_device *dev, void *data,
  338. struct drm_file *file_priv);
  339. int vc4_create_shader_bo_ioctl(struct drm_device *dev, void *data,
  340. struct drm_file *file_priv);
  341. int vc4_mmap_bo_ioctl(struct drm_device *dev, void *data,
  342. struct drm_file *file_priv);
  343. int vc4_get_hang_state_ioctl(struct drm_device *dev, void *data,
  344. struct drm_file *file_priv);
  345. int vc4_mmap(struct file *filp, struct vm_area_struct *vma);
  346. int vc4_prime_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma);
  347. void *vc4_prime_vmap(struct drm_gem_object *obj);
  348. void vc4_bo_cache_init(struct drm_device *dev);
  349. void vc4_bo_cache_destroy(struct drm_device *dev);
  350. int vc4_bo_stats_debugfs(struct seq_file *m, void *arg);
  351. /* vc4_crtc.c */
  352. extern struct platform_driver vc4_crtc_driver;
  353. int vc4_enable_vblank(struct drm_device *dev, unsigned int crtc_id);
  354. void vc4_disable_vblank(struct drm_device *dev, unsigned int crtc_id);
  355. int vc4_crtc_debugfs_regs(struct seq_file *m, void *arg);
  356. /* vc4_debugfs.c */
  357. int vc4_debugfs_init(struct drm_minor *minor);
  358. void vc4_debugfs_cleanup(struct drm_minor *minor);
  359. /* vc4_drv.c */
  360. void __iomem *vc4_ioremap_regs(struct platform_device *dev, int index);
  361. /* vc4_dpi.c */
  362. extern struct platform_driver vc4_dpi_driver;
  363. int vc4_dpi_debugfs_regs(struct seq_file *m, void *unused);
  364. /* vc4_gem.c */
  365. void vc4_gem_init(struct drm_device *dev);
  366. void vc4_gem_destroy(struct drm_device *dev);
  367. int vc4_submit_cl_ioctl(struct drm_device *dev, void *data,
  368. struct drm_file *file_priv);
  369. int vc4_wait_seqno_ioctl(struct drm_device *dev, void *data,
  370. struct drm_file *file_priv);
  371. int vc4_wait_bo_ioctl(struct drm_device *dev, void *data,
  372. struct drm_file *file_priv);
  373. void vc4_submit_next_bin_job(struct drm_device *dev);
  374. void vc4_submit_next_render_job(struct drm_device *dev);
  375. void vc4_move_job_to_render(struct drm_device *dev, struct vc4_exec_info *exec);
  376. int vc4_wait_for_seqno(struct drm_device *dev, uint64_t seqno,
  377. uint64_t timeout_ns, bool interruptible);
  378. void vc4_job_handle_completed(struct vc4_dev *vc4);
  379. int vc4_queue_seqno_cb(struct drm_device *dev,
  380. struct vc4_seqno_cb *cb, uint64_t seqno,
  381. void (*func)(struct vc4_seqno_cb *cb));
  382. /* vc4_hdmi.c */
  383. extern struct platform_driver vc4_hdmi_driver;
  384. int vc4_hdmi_debugfs_regs(struct seq_file *m, void *unused);
  385. /* vc4_irq.c */
  386. irqreturn_t vc4_irq(int irq, void *arg);
  387. void vc4_irq_preinstall(struct drm_device *dev);
  388. int vc4_irq_postinstall(struct drm_device *dev);
  389. void vc4_irq_uninstall(struct drm_device *dev);
  390. void vc4_irq_reset(struct drm_device *dev);
  391. /* vc4_hvs.c */
  392. extern struct platform_driver vc4_hvs_driver;
  393. void vc4_hvs_dump_state(struct drm_device *dev);
  394. int vc4_hvs_debugfs_regs(struct seq_file *m, void *unused);
  395. /* vc4_kms.c */
  396. int vc4_kms_load(struct drm_device *dev);
  397. /* vc4_plane.c */
  398. struct drm_plane *vc4_plane_init(struct drm_device *dev,
  399. enum drm_plane_type type);
  400. u32 vc4_plane_write_dlist(struct drm_plane *plane, u32 __iomem *dlist);
  401. u32 vc4_plane_dlist_size(const struct drm_plane_state *state);
  402. void vc4_plane_async_set_fb(struct drm_plane *plane,
  403. struct drm_framebuffer *fb);
  404. /* vc4_v3d.c */
  405. extern struct platform_driver vc4_v3d_driver;
  406. int vc4_v3d_debugfs_ident(struct seq_file *m, void *unused);
  407. int vc4_v3d_debugfs_regs(struct seq_file *m, void *unused);
  408. /* vc4_validate.c */
  409. int
  410. vc4_validate_bin_cl(struct drm_device *dev,
  411. void *validated,
  412. void *unvalidated,
  413. struct vc4_exec_info *exec);
  414. int
  415. vc4_validate_shader_recs(struct drm_device *dev, struct vc4_exec_info *exec);
  416. struct drm_gem_cma_object *vc4_use_bo(struct vc4_exec_info *exec,
  417. uint32_t hindex);
  418. int vc4_get_rcl(struct drm_device *dev, struct vc4_exec_info *exec);
  419. bool vc4_check_tex_size(struct vc4_exec_info *exec,
  420. struct drm_gem_cma_object *fbo,
  421. uint32_t offset, uint8_t tiling_format,
  422. uint32_t width, uint32_t height, uint8_t cpp);
  423. /* vc4_validate_shader.c */
  424. struct vc4_validated_shader_info *
  425. vc4_validate_shader(struct drm_gem_cma_object *shader_obj);