vc4_drv.h 14 KB

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