vc4_drv.h 17 KB

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