vc4_drv.h 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2015 Broadcom
  4. */
  5. #ifndef _VC4_DRV_H_
  6. #define _VC4_DRV_H_
  7. #include <linux/debugfs.h>
  8. #include <linux/delay.h>
  9. #include <linux/of.h>
  10. #include <linux/refcount.h>
  11. #include <linux/uaccess.h>
  12. #include <drm/drm_atomic.h>
  13. #include <drm/drm_debugfs.h>
  14. #include <drm/drm_device.h>
  15. #include <drm/drm_encoder.h>
  16. #include <drm/drm_gem_dma_helper.h>
  17. #include <drm/drm_managed.h>
  18. #include <drm/drm_mm.h>
  19. #include <drm/drm_modeset_lock.h>
  20. #include <kunit/test-bug.h>
  21. #include "uapi/drm/vc4_drm.h"
  22. struct drm_device;
  23. struct drm_gem_object;
  24. extern const struct drm_driver vc4_drm_driver;
  25. extern const struct drm_driver vc5_drm_driver;
  26. /* Don't forget to update vc4_bo.c: bo_type_names[] when adding to
  27. * this.
  28. */
  29. enum vc4_kernel_bo_type {
  30. /* Any kernel allocation (gem_create_object hook) before it
  31. * gets another type set.
  32. */
  33. VC4_BO_TYPE_KERNEL,
  34. VC4_BO_TYPE_V3D,
  35. VC4_BO_TYPE_V3D_SHADER,
  36. VC4_BO_TYPE_DUMB,
  37. VC4_BO_TYPE_BIN,
  38. VC4_BO_TYPE_RCL,
  39. VC4_BO_TYPE_BCL,
  40. VC4_BO_TYPE_KERNEL_CACHE,
  41. VC4_BO_TYPE_COUNT
  42. };
  43. /* Performance monitor object. The perform lifetime is controlled by userspace
  44. * using perfmon related ioctls. A perfmon can be attached to a submit_cl
  45. * request, and when this is the case, HW perf counters will be activated just
  46. * before the submit_cl is submitted to the GPU and disabled when the job is
  47. * done. This way, only events related to a specific job will be counted.
  48. */
  49. struct vc4_perfmon {
  50. struct vc4_dev *dev;
  51. /* Tracks the number of users of the perfmon, when this counter reaches
  52. * zero the perfmon is destroyed.
  53. */
  54. refcount_t refcnt;
  55. /* Number of counters activated in this perfmon instance
  56. * (should be less than DRM_VC4_MAX_PERF_COUNTERS).
  57. */
  58. u8 ncounters;
  59. /* Events counted by the HW perf counters. */
  60. u8 events[DRM_VC4_MAX_PERF_COUNTERS];
  61. /* Storage for counter values. Counters are incremented by the HW
  62. * perf counter values every time the perfmon is attached to a GPU job.
  63. * This way, perfmon users don't have to retrieve the results after
  64. * each job if they want to track events covering several submissions.
  65. * Note that counter values can't be reset, but you can fake a reset by
  66. * destroying the perfmon and creating a new one.
  67. */
  68. u64 counters[] __counted_by(ncounters);
  69. };
  70. enum vc4_gen {
  71. VC4_GEN_4,
  72. VC4_GEN_5,
  73. };
  74. struct vc4_dev {
  75. struct drm_device base;
  76. struct device *dev;
  77. enum vc4_gen gen;
  78. unsigned int irq;
  79. struct vc4_hvs *hvs;
  80. struct vc4_v3d *v3d;
  81. struct vc4_hang_state *hang_state;
  82. /* The kernel-space BO cache. Tracks buffers that have been
  83. * unreferenced by all other users (refcounts of 0!) but not
  84. * yet freed, so we can do cheap allocations.
  85. */
  86. struct vc4_bo_cache {
  87. /* Array of list heads for entries in the BO cache,
  88. * based on number of pages, so we can do O(1) lookups
  89. * in the cache when allocating.
  90. */
  91. struct list_head *size_list;
  92. uint32_t size_list_size;
  93. /* List of all BOs in the cache, ordered by age, so we
  94. * can do O(1) lookups when trying to free old
  95. * buffers.
  96. */
  97. struct list_head time_list;
  98. struct work_struct time_work;
  99. struct timer_list time_timer;
  100. } bo_cache;
  101. u32 num_labels;
  102. struct vc4_label {
  103. const char *name;
  104. u32 num_allocated;
  105. u32 size_allocated;
  106. } *bo_labels;
  107. /* Protects bo_cache and bo_labels. */
  108. struct mutex bo_lock;
  109. /* Purgeable BO pool. All BOs in this pool can have their memory
  110. * reclaimed if the driver is unable to allocate new BOs. We also
  111. * keep stats related to the purge mechanism here.
  112. */
  113. struct {
  114. struct list_head list;
  115. unsigned int num;
  116. size_t size;
  117. unsigned int purged_num;
  118. size_t purged_size;
  119. struct mutex lock;
  120. } purgeable;
  121. uint64_t dma_fence_context;
  122. /* Sequence number for the last job queued in bin_job_list.
  123. * Starts at 0 (no jobs emitted).
  124. */
  125. uint64_t emit_seqno;
  126. /* Sequence number for the last completed job on the GPU.
  127. * Starts at 0 (no jobs completed).
  128. */
  129. uint64_t finished_seqno;
  130. /* List of all struct vc4_exec_info for jobs to be executed in
  131. * the binner. The first job in the list is the one currently
  132. * programmed into ct0ca for execution.
  133. */
  134. struct list_head bin_job_list;
  135. /* List of all struct vc4_exec_info for jobs that have
  136. * completed binning and are ready for rendering. The first
  137. * job in the list is the one currently programmed into ct1ca
  138. * for execution.
  139. */
  140. struct list_head render_job_list;
  141. /* List of the finished vc4_exec_infos waiting to be freed by
  142. * job_done_work.
  143. */
  144. struct list_head job_done_list;
  145. /* Spinlock used to synchronize the job_list and seqno
  146. * accesses between the IRQ handler and GEM ioctls.
  147. */
  148. spinlock_t job_lock;
  149. wait_queue_head_t job_wait_queue;
  150. struct work_struct job_done_work;
  151. /* Used to track the active perfmon if any. Access to this field is
  152. * protected by job_lock.
  153. */
  154. struct vc4_perfmon *active_perfmon;
  155. /* List of struct vc4_seqno_cb for callbacks to be made from a
  156. * workqueue when the given seqno is passed.
  157. */
  158. struct list_head seqno_cb_list;
  159. /* The memory used for storing binner tile alloc, tile state,
  160. * and overflow memory allocations. This is freed when V3D
  161. * powers down.
  162. */
  163. struct vc4_bo *bin_bo;
  164. /* Size of blocks allocated within bin_bo. */
  165. uint32_t bin_alloc_size;
  166. /* Bitmask of the bin_alloc_size chunks in bin_bo that are
  167. * used.
  168. */
  169. uint32_t bin_alloc_used;
  170. /* Bitmask of the current bin_alloc used for overflow memory. */
  171. uint32_t bin_alloc_overflow;
  172. /* Incremented when an underrun error happened after an atomic commit.
  173. * This is particularly useful to detect when a specific modeset is too
  174. * demanding in term of memory or HVS bandwidth which is hard to guess
  175. * at atomic check time.
  176. */
  177. atomic_t underrun;
  178. struct work_struct overflow_mem_work;
  179. int power_refcount;
  180. /* Set to true when the load tracker is active. */
  181. bool load_tracker_enabled;
  182. /* Mutex controlling the power refcount. */
  183. struct mutex power_lock;
  184. struct {
  185. struct timer_list timer;
  186. struct work_struct reset_work;
  187. } hangcheck;
  188. struct drm_modeset_lock ctm_state_lock;
  189. struct drm_private_obj ctm_manager;
  190. struct drm_private_obj hvs_channels;
  191. struct drm_private_obj load_tracker;
  192. /* Mutex for binner bo allocation. */
  193. struct mutex bin_bo_lock;
  194. /* Reference count for our binner bo. */
  195. struct kref bin_bo_kref;
  196. };
  197. #define to_vc4_dev(_dev) \
  198. container_of_const(_dev, struct vc4_dev, base)
  199. struct vc4_bo {
  200. struct drm_gem_dma_object base;
  201. /* seqno of the last job to render using this BO. */
  202. uint64_t seqno;
  203. /* seqno of the last job to use the RCL to write to this BO.
  204. *
  205. * Note that this doesn't include binner overflow memory
  206. * writes.
  207. */
  208. uint64_t write_seqno;
  209. bool t_format;
  210. /* List entry for the BO's position in either
  211. * vc4_exec_info->unref_list or vc4_dev->bo_cache.time_list
  212. */
  213. struct list_head unref_head;
  214. /* Time in jiffies when the BO was put in vc4->bo_cache. */
  215. unsigned long free_time;
  216. /* List entry for the BO's position in vc4_dev->bo_cache.size_list */
  217. struct list_head size_head;
  218. /* Struct for shader validation state, if created by
  219. * DRM_IOCTL_VC4_CREATE_SHADER_BO.
  220. */
  221. struct vc4_validated_shader_info *validated_shader;
  222. /* One of enum vc4_kernel_bo_type, or VC4_BO_TYPE_COUNT + i
  223. * for user-allocated labels.
  224. */
  225. int label;
  226. /* Count the number of active users. This is needed to determine
  227. * whether we can move the BO to the purgeable list or not (when the BO
  228. * is used by the GPU or the display engine we can't purge it).
  229. */
  230. refcount_t usecnt;
  231. /* Store purgeable/purged state here */
  232. u32 madv;
  233. struct mutex madv_lock;
  234. };
  235. #define to_vc4_bo(_bo) \
  236. container_of_const(to_drm_gem_dma_obj(_bo), struct vc4_bo, base)
  237. struct vc4_fence {
  238. struct dma_fence base;
  239. struct drm_device *dev;
  240. /* vc4 seqno for signaled() test */
  241. uint64_t seqno;
  242. };
  243. #define to_vc4_fence(_fence) \
  244. container_of_const(_fence, struct vc4_fence, base)
  245. struct vc4_seqno_cb {
  246. struct work_struct work;
  247. uint64_t seqno;
  248. void (*func)(struct vc4_seqno_cb *cb);
  249. };
  250. struct vc4_v3d {
  251. struct vc4_dev *vc4;
  252. struct platform_device *pdev;
  253. void __iomem *regs;
  254. struct clk *clk;
  255. struct debugfs_regset32 regset;
  256. };
  257. struct vc4_hvs {
  258. struct vc4_dev *vc4;
  259. struct platform_device *pdev;
  260. void __iomem *regs;
  261. u32 __iomem *dlist;
  262. unsigned int dlist_mem_size;
  263. struct clk *core_clk;
  264. unsigned long max_core_rate;
  265. /* Memory manager for CRTCs to allocate space in the display
  266. * list. Units are dwords.
  267. */
  268. struct drm_mm dlist_mm;
  269. /* Memory manager for the LBM memory used by HVS scaling. */
  270. struct drm_mm lbm_mm;
  271. spinlock_t mm_lock;
  272. struct drm_mm_node mitchell_netravali_filter;
  273. struct debugfs_regset32 regset;
  274. /*
  275. * Even if HDMI0 on the RPi4 can output modes requiring a pixel
  276. * rate higher than 297MHz, it needs some adjustments in the
  277. * config.txt file to be able to do so and thus won't always be
  278. * available.
  279. */
  280. bool vc5_hdmi_enable_hdmi_20;
  281. /*
  282. * 4096x2160@60 requires a core overclock to work, so register
  283. * whether that is sufficient.
  284. */
  285. bool vc5_hdmi_enable_4096by2160;
  286. };
  287. #define HVS_NUM_CHANNELS 3
  288. struct vc4_hvs_state {
  289. struct drm_private_state base;
  290. unsigned long core_clock_rate;
  291. struct {
  292. unsigned in_use: 1;
  293. unsigned long fifo_load;
  294. struct drm_crtc_commit *pending_commit;
  295. } fifo_state[HVS_NUM_CHANNELS];
  296. };
  297. #define to_vc4_hvs_state(_state) \
  298. container_of_const(_state, struct vc4_hvs_state, base)
  299. struct vc4_hvs_state *vc4_hvs_get_global_state(struct drm_atomic_state *state);
  300. struct vc4_hvs_state *vc4_hvs_get_old_global_state(const struct drm_atomic_state *state);
  301. struct vc4_hvs_state *vc4_hvs_get_new_global_state(const struct drm_atomic_state *state);
  302. struct vc4_plane {
  303. struct drm_plane base;
  304. };
  305. #define to_vc4_plane(_plane) \
  306. container_of_const(_plane, struct vc4_plane, base)
  307. enum vc4_scaling_mode {
  308. VC4_SCALING_NONE,
  309. VC4_SCALING_TPZ,
  310. VC4_SCALING_PPF,
  311. };
  312. struct vc4_plane_state {
  313. struct drm_plane_state base;
  314. /* System memory copy of the display list for this element, computed
  315. * at atomic_check time.
  316. */
  317. u32 *dlist;
  318. u32 dlist_size; /* Number of dwords allocated for the display list */
  319. u32 dlist_count; /* Number of used dwords in the display list. */
  320. /* Offset in the dlist to various words, for pageflip or
  321. * cursor updates.
  322. */
  323. u32 pos0_offset;
  324. u32 pos2_offset;
  325. u32 ptr0_offset;
  326. u32 lbm_offset;
  327. /* Offset where the plane's dlist was last stored in the
  328. * hardware at vc4_crtc_atomic_flush() time.
  329. */
  330. u32 __iomem *hw_dlist;
  331. /* Clipped coordinates of the plane on the display. */
  332. int crtc_x, crtc_y, crtc_w, crtc_h;
  333. /* Clipped area being scanned from in the FB. */
  334. u32 src_x, src_y;
  335. u32 src_w[2], src_h[2];
  336. /* Scaling selection for the RGB/Y plane and the Cb/Cr planes. */
  337. enum vc4_scaling_mode x_scaling[2], y_scaling[2];
  338. bool is_unity;
  339. bool is_yuv;
  340. /* Offset to start scanning out from the start of the plane's
  341. * BO.
  342. */
  343. u32 offsets[3];
  344. /* Our allocation in LBM for temporary storage during scaling. */
  345. struct drm_mm_node lbm;
  346. /* Set when the plane has per-pixel alpha content or does not cover
  347. * the entire screen. This is a hint to the CRTC that it might need
  348. * to enable background color fill.
  349. */
  350. bool needs_bg_fill;
  351. /* Mark the dlist as initialized. Useful to avoid initializing it twice
  352. * when async update is not possible.
  353. */
  354. bool dlist_initialized;
  355. /* Load of this plane on the HVS block. The load is expressed in HVS
  356. * cycles/sec.
  357. */
  358. u64 hvs_load;
  359. /* Memory bandwidth needed for this plane. This is expressed in
  360. * bytes/sec.
  361. */
  362. u64 membus_load;
  363. };
  364. #define to_vc4_plane_state(_state) \
  365. container_of_const(_state, struct vc4_plane_state, base)
  366. enum vc4_encoder_type {
  367. VC4_ENCODER_TYPE_NONE,
  368. VC4_ENCODER_TYPE_HDMI0,
  369. VC4_ENCODER_TYPE_HDMI1,
  370. VC4_ENCODER_TYPE_VEC,
  371. VC4_ENCODER_TYPE_DSI0,
  372. VC4_ENCODER_TYPE_DSI1,
  373. VC4_ENCODER_TYPE_SMI,
  374. VC4_ENCODER_TYPE_DPI,
  375. VC4_ENCODER_TYPE_TXP,
  376. };
  377. struct vc4_encoder {
  378. struct drm_encoder base;
  379. enum vc4_encoder_type type;
  380. u32 clock_select;
  381. void (*pre_crtc_configure)(struct drm_encoder *encoder, struct drm_atomic_state *state);
  382. void (*pre_crtc_enable)(struct drm_encoder *encoder, struct drm_atomic_state *state);
  383. void (*post_crtc_enable)(struct drm_encoder *encoder, struct drm_atomic_state *state);
  384. void (*post_crtc_disable)(struct drm_encoder *encoder, struct drm_atomic_state *state);
  385. void (*post_crtc_powerdown)(struct drm_encoder *encoder, struct drm_atomic_state *state);
  386. };
  387. #define to_vc4_encoder(_encoder) \
  388. container_of_const(_encoder, struct vc4_encoder, base)
  389. static inline
  390. struct drm_encoder *vc4_find_encoder_by_type(struct drm_device *drm,
  391. enum vc4_encoder_type type)
  392. {
  393. struct drm_encoder *encoder;
  394. drm_for_each_encoder(encoder, drm) {
  395. struct vc4_encoder *vc4_encoder = to_vc4_encoder(encoder);
  396. if (vc4_encoder->type == type)
  397. return encoder;
  398. }
  399. return NULL;
  400. }
  401. struct vc4_crtc_data {
  402. const char *name;
  403. const char *debugfs_name;
  404. /* Bitmask of channels (FIFOs) of the HVS that the output can source from */
  405. unsigned int hvs_available_channels;
  406. /* Which output of the HVS this pixelvalve sources from. */
  407. int hvs_output;
  408. };
  409. extern const struct vc4_crtc_data vc4_txp_crtc_data;
  410. struct vc4_pv_data {
  411. struct vc4_crtc_data base;
  412. /* Depth of the PixelValve FIFO in bytes */
  413. unsigned int fifo_depth;
  414. /* Number of pixels output per clock period */
  415. u8 pixels_per_clock;
  416. enum vc4_encoder_type encoder_types[4];
  417. };
  418. extern const struct vc4_pv_data bcm2835_pv0_data;
  419. extern const struct vc4_pv_data bcm2835_pv1_data;
  420. extern const struct vc4_pv_data bcm2835_pv2_data;
  421. extern const struct vc4_pv_data bcm2711_pv0_data;
  422. extern const struct vc4_pv_data bcm2711_pv1_data;
  423. extern const struct vc4_pv_data bcm2711_pv2_data;
  424. extern const struct vc4_pv_data bcm2711_pv3_data;
  425. extern const struct vc4_pv_data bcm2711_pv4_data;
  426. struct vc4_crtc {
  427. struct drm_crtc base;
  428. struct platform_device *pdev;
  429. const struct vc4_crtc_data *data;
  430. void __iomem *regs;
  431. /* Timestamp at start of vblank irq - unaffected by lock delays. */
  432. ktime_t t_vblank;
  433. u8 lut_r[256];
  434. u8 lut_g[256];
  435. u8 lut_b[256];
  436. struct drm_pending_vblank_event *event;
  437. struct debugfs_regset32 regset;
  438. /**
  439. * @feeds_txp: True if the CRTC feeds our writeback controller.
  440. */
  441. bool feeds_txp;
  442. /**
  443. * @irq_lock: Spinlock protecting the resources shared between
  444. * the atomic code and our vblank handler.
  445. */
  446. spinlock_t irq_lock;
  447. /**
  448. * @current_dlist: Start offset of the display list currently
  449. * set in the HVS for that CRTC. Protected by @irq_lock, and
  450. * copied in vc4_hvs_update_dlist() for the CRTC interrupt
  451. * handler to have access to that value.
  452. */
  453. unsigned int current_dlist;
  454. /**
  455. * @current_hvs_channel: HVS channel currently assigned to the
  456. * CRTC. Protected by @irq_lock, and copied in
  457. * vc4_hvs_atomic_begin() for the CRTC interrupt handler to have
  458. * access to that value.
  459. */
  460. unsigned int current_hvs_channel;
  461. };
  462. #define to_vc4_crtc(_crtc) \
  463. container_of_const(_crtc, struct vc4_crtc, base)
  464. static inline const struct vc4_crtc_data *
  465. vc4_crtc_to_vc4_crtc_data(const struct vc4_crtc *crtc)
  466. {
  467. return crtc->data;
  468. }
  469. static inline const struct vc4_pv_data *
  470. vc4_crtc_to_vc4_pv_data(const struct vc4_crtc *crtc)
  471. {
  472. const struct vc4_crtc_data *data = vc4_crtc_to_vc4_crtc_data(crtc);
  473. return container_of_const(data, struct vc4_pv_data, base);
  474. }
  475. struct drm_encoder *vc4_get_crtc_encoder(struct drm_crtc *crtc,
  476. struct drm_crtc_state *state);
  477. struct vc4_crtc_state {
  478. struct drm_crtc_state base;
  479. /* Dlist area for this CRTC configuration. */
  480. struct drm_mm_node mm;
  481. bool txp_armed;
  482. unsigned int assigned_channel;
  483. struct {
  484. unsigned int left;
  485. unsigned int right;
  486. unsigned int top;
  487. unsigned int bottom;
  488. } margins;
  489. unsigned long hvs_load;
  490. /* Transitional state below, only valid during atomic commits */
  491. bool update_muxing;
  492. };
  493. #define VC4_HVS_CHANNEL_DISABLED ((unsigned int)-1)
  494. #define to_vc4_crtc_state(_state) \
  495. container_of_const(_state, struct vc4_crtc_state, base)
  496. #define V3D_READ(offset) \
  497. ({ \
  498. kunit_fail_current_test("Accessing a register in a unit test!\n"); \
  499. readl(vc4->v3d->regs + (offset)); \
  500. })
  501. #define V3D_WRITE(offset, val) \
  502. do { \
  503. kunit_fail_current_test("Accessing a register in a unit test!\n"); \
  504. writel(val, vc4->v3d->regs + (offset)); \
  505. } while (0)
  506. #define HVS_READ(offset) \
  507. ({ \
  508. kunit_fail_current_test("Accessing a register in a unit test!\n"); \
  509. readl(hvs->regs + (offset)); \
  510. })
  511. #define HVS_WRITE(offset, val) \
  512. do { \
  513. kunit_fail_current_test("Accessing a register in a unit test!\n"); \
  514. writel(val, hvs->regs + (offset)); \
  515. } while (0)
  516. #define VC4_REG32(reg) { .name = #reg, .offset = reg }
  517. struct vc4_exec_info {
  518. struct vc4_dev *dev;
  519. /* Sequence number for this bin/render job. */
  520. uint64_t seqno;
  521. /* Latest write_seqno of any BO that binning depends on. */
  522. uint64_t bin_dep_seqno;
  523. struct dma_fence *fence;
  524. /* Last current addresses the hardware was processing when the
  525. * hangcheck timer checked on us.
  526. */
  527. uint32_t last_ct0ca, last_ct1ca;
  528. /* Kernel-space copy of the ioctl arguments */
  529. struct drm_vc4_submit_cl *args;
  530. /* This is the array of BOs that were looked up at the start of exec.
  531. * Command validation will use indices into this array.
  532. */
  533. struct drm_gem_object **bo;
  534. uint32_t bo_count;
  535. /* List of BOs that are being written by the RCL. Other than
  536. * the binner temporary storage, this is all the BOs written
  537. * by the job.
  538. */
  539. struct drm_gem_dma_object *rcl_write_bo[4];
  540. uint32_t rcl_write_bo_count;
  541. /* Pointers for our position in vc4->job_list */
  542. struct list_head head;
  543. /* List of other BOs used in the job that need to be released
  544. * once the job is complete.
  545. */
  546. struct list_head unref_list;
  547. /* Current unvalidated indices into @bo loaded by the non-hardware
  548. * VC4_PACKET_GEM_HANDLES.
  549. */
  550. uint32_t bo_index[2];
  551. /* This is the BO where we store the validated command lists, shader
  552. * records, and uniforms.
  553. */
  554. struct drm_gem_dma_object *exec_bo;
  555. /**
  556. * This tracks the per-shader-record state (packet 64) that
  557. * determines the length of the shader record and the offset
  558. * it's expected to be found at. It gets read in from the
  559. * command lists.
  560. */
  561. struct vc4_shader_state {
  562. uint32_t addr;
  563. /* Maximum vertex index referenced by any primitive using this
  564. * shader state.
  565. */
  566. uint32_t max_index;
  567. } *shader_state;
  568. /** How many shader states the user declared they were using. */
  569. uint32_t shader_state_size;
  570. /** How many shader state records the validator has seen. */
  571. uint32_t shader_state_count;
  572. bool found_tile_binning_mode_config_packet;
  573. bool found_start_tile_binning_packet;
  574. bool found_increment_semaphore_packet;
  575. bool found_flush;
  576. uint8_t bin_tiles_x, bin_tiles_y;
  577. /* Physical address of the start of the tile alloc array
  578. * (where each tile's binned CL will start)
  579. */
  580. uint32_t tile_alloc_offset;
  581. /* Bitmask of which binner slots are freed when this job completes. */
  582. uint32_t bin_slots;
  583. /**
  584. * Computed addresses pointing into exec_bo where we start the
  585. * bin thread (ct0) and render thread (ct1).
  586. */
  587. uint32_t ct0ca, ct0ea;
  588. uint32_t ct1ca, ct1ea;
  589. /* Pointer to the unvalidated bin CL (if present). */
  590. void *bin_u;
  591. /* Pointers to the shader recs. These paddr gets incremented as CL
  592. * packets are relocated in validate_gl_shader_state, and the vaddrs
  593. * (u and v) get incremented and size decremented as the shader recs
  594. * themselves are validated.
  595. */
  596. void *shader_rec_u;
  597. void *shader_rec_v;
  598. uint32_t shader_rec_p;
  599. uint32_t shader_rec_size;
  600. /* Pointers to the uniform data. These pointers are incremented, and
  601. * size decremented, as each batch of uniforms is uploaded.
  602. */
  603. void *uniforms_u;
  604. void *uniforms_v;
  605. uint32_t uniforms_p;
  606. uint32_t uniforms_size;
  607. /* Pointer to a performance monitor object if the user requested it,
  608. * NULL otherwise.
  609. */
  610. struct vc4_perfmon *perfmon;
  611. /* Whether the exec has taken a reference to the binner BO, which should
  612. * happen with a VC4_PACKET_TILE_BINNING_MODE_CONFIG packet.
  613. */
  614. bool bin_bo_used;
  615. };
  616. /* Per-open file private data. Any driver-specific resource that has to be
  617. * released when the DRM file is closed should be placed here.
  618. */
  619. struct vc4_file {
  620. struct vc4_dev *dev;
  621. struct {
  622. struct idr idr;
  623. struct mutex lock;
  624. } perfmon;
  625. bool bin_bo_used;
  626. };
  627. static inline struct vc4_exec_info *
  628. vc4_first_bin_job(struct vc4_dev *vc4)
  629. {
  630. return list_first_entry_or_null(&vc4->bin_job_list,
  631. struct vc4_exec_info, head);
  632. }
  633. static inline struct vc4_exec_info *
  634. vc4_first_render_job(struct vc4_dev *vc4)
  635. {
  636. return list_first_entry_or_null(&vc4->render_job_list,
  637. struct vc4_exec_info, head);
  638. }
  639. static inline struct vc4_exec_info *
  640. vc4_last_render_job(struct vc4_dev *vc4)
  641. {
  642. if (list_empty(&vc4->render_job_list))
  643. return NULL;
  644. return list_last_entry(&vc4->render_job_list,
  645. struct vc4_exec_info, head);
  646. }
  647. /**
  648. * struct vc4_texture_sample_info - saves the offsets into the UBO for texture
  649. * setup parameters.
  650. *
  651. * This will be used at draw time to relocate the reference to the texture
  652. * contents in p0, and validate that the offset combined with
  653. * width/height/stride/etc. from p1 and p2/p3 doesn't sample outside the BO.
  654. * Note that the hardware treats unprovided config parameters as 0, so not all
  655. * of them need to be set up for every texure sample, and we'll store ~0 as
  656. * the offset to mark the unused ones.
  657. *
  658. * See the VC4 3D architecture guide page 41 ("Texture and Memory Lookup Unit
  659. * Setup") for definitions of the texture parameters.
  660. */
  661. struct vc4_texture_sample_info {
  662. bool is_direct;
  663. uint32_t p_offset[4];
  664. };
  665. /**
  666. * struct vc4_validated_shader_info - information about validated shaders that
  667. * needs to be used from command list validation.
  668. *
  669. * For a given shader, each time a shader state record references it, we need
  670. * to verify that the shader doesn't read more uniforms than the shader state
  671. * record's uniform BO pointer can provide, and we need to apply relocations
  672. * and validate the shader state record's uniforms that define the texture
  673. * samples.
  674. */
  675. struct vc4_validated_shader_info {
  676. uint32_t uniforms_size;
  677. uint32_t uniforms_src_size;
  678. uint32_t num_texture_samples;
  679. struct vc4_texture_sample_info *texture_samples;
  680. uint32_t num_uniform_addr_offsets;
  681. uint32_t *uniform_addr_offsets;
  682. bool is_threaded;
  683. };
  684. /**
  685. * __wait_for - magic wait macro
  686. *
  687. * Macro to help avoid open coding check/wait/timeout patterns. Note that it's
  688. * important that we check the condition again after having timed out, since the
  689. * timeout could be due to preemption or similar and we've never had a chance to
  690. * check the condition before the timeout.
  691. */
  692. #define __wait_for(OP, COND, US, Wmin, Wmax) ({ \
  693. const ktime_t end__ = ktime_add_ns(ktime_get_raw(), 1000ll * (US)); \
  694. long wait__ = (Wmin); /* recommended min for usleep is 10 us */ \
  695. int ret__; \
  696. might_sleep(); \
  697. for (;;) { \
  698. const bool expired__ = ktime_after(ktime_get_raw(), end__); \
  699. OP; \
  700. /* Guarantee COND check prior to timeout */ \
  701. barrier(); \
  702. if (COND) { \
  703. ret__ = 0; \
  704. break; \
  705. } \
  706. if (expired__) { \
  707. ret__ = -ETIMEDOUT; \
  708. break; \
  709. } \
  710. usleep_range(wait__, wait__ * 2); \
  711. if (wait__ < (Wmax)) \
  712. wait__ <<= 1; \
  713. } \
  714. ret__; \
  715. })
  716. #define _wait_for(COND, US, Wmin, Wmax) __wait_for(, (COND), (US), (Wmin), \
  717. (Wmax))
  718. #define wait_for(COND, MS) _wait_for((COND), (MS) * 1000, 10, 1000)
  719. /* vc4_bo.c */
  720. struct drm_gem_object *vc4_create_object(struct drm_device *dev, size_t size);
  721. struct vc4_bo *vc4_bo_create(struct drm_device *dev, size_t size,
  722. bool from_cache, enum vc4_kernel_bo_type type);
  723. int vc4_bo_dumb_create(struct drm_file *file_priv,
  724. struct drm_device *dev,
  725. struct drm_mode_create_dumb *args);
  726. int vc4_create_bo_ioctl(struct drm_device *dev, void *data,
  727. struct drm_file *file_priv);
  728. int vc4_create_shader_bo_ioctl(struct drm_device *dev, void *data,
  729. struct drm_file *file_priv);
  730. int vc4_mmap_bo_ioctl(struct drm_device *dev, void *data,
  731. struct drm_file *file_priv);
  732. int vc4_set_tiling_ioctl(struct drm_device *dev, void *data,
  733. struct drm_file *file_priv);
  734. int vc4_get_tiling_ioctl(struct drm_device *dev, void *data,
  735. struct drm_file *file_priv);
  736. int vc4_get_hang_state_ioctl(struct drm_device *dev, void *data,
  737. struct drm_file *file_priv);
  738. int vc4_label_bo_ioctl(struct drm_device *dev, void *data,
  739. struct drm_file *file_priv);
  740. int vc4_bo_cache_init(struct drm_device *dev);
  741. int vc4_bo_inc_usecnt(struct vc4_bo *bo);
  742. void vc4_bo_dec_usecnt(struct vc4_bo *bo);
  743. void vc4_bo_add_to_purgeable_pool(struct vc4_bo *bo);
  744. void vc4_bo_remove_from_purgeable_pool(struct vc4_bo *bo);
  745. int vc4_bo_debugfs_init(struct drm_minor *minor);
  746. /* vc4_crtc.c */
  747. extern struct platform_driver vc4_crtc_driver;
  748. int vc4_crtc_disable_at_boot(struct drm_crtc *crtc);
  749. int __vc4_crtc_init(struct drm_device *drm, struct platform_device *pdev,
  750. struct vc4_crtc *vc4_crtc, const struct vc4_crtc_data *data,
  751. struct drm_plane *primary_plane,
  752. const struct drm_crtc_funcs *crtc_funcs,
  753. const struct drm_crtc_helper_funcs *crtc_helper_funcs,
  754. bool feeds_txp);
  755. int vc4_crtc_init(struct drm_device *drm, struct platform_device *pdev,
  756. struct vc4_crtc *vc4_crtc, const struct vc4_crtc_data *data,
  757. const struct drm_crtc_funcs *crtc_funcs,
  758. const struct drm_crtc_helper_funcs *crtc_helper_funcs,
  759. bool feeds_txp);
  760. int vc4_page_flip(struct drm_crtc *crtc,
  761. struct drm_framebuffer *fb,
  762. struct drm_pending_vblank_event *event,
  763. uint32_t flags,
  764. struct drm_modeset_acquire_ctx *ctx);
  765. int vc4_crtc_atomic_check(struct drm_crtc *crtc,
  766. struct drm_atomic_state *state);
  767. struct drm_crtc_state *vc4_crtc_duplicate_state(struct drm_crtc *crtc);
  768. void vc4_crtc_destroy_state(struct drm_crtc *crtc,
  769. struct drm_crtc_state *state);
  770. void vc4_crtc_reset(struct drm_crtc *crtc);
  771. void vc4_crtc_handle_vblank(struct vc4_crtc *crtc);
  772. void vc4_crtc_send_vblank(struct drm_crtc *crtc);
  773. int vc4_crtc_late_register(struct drm_crtc *crtc);
  774. void vc4_crtc_get_margins(struct drm_crtc_state *state,
  775. unsigned int *left, unsigned int *right,
  776. unsigned int *top, unsigned int *bottom);
  777. /* vc4_debugfs.c */
  778. void vc4_debugfs_init(struct drm_minor *minor);
  779. #ifdef CONFIG_DEBUG_FS
  780. void vc4_debugfs_add_regset32(struct drm_device *drm,
  781. const char *filename,
  782. struct debugfs_regset32 *regset);
  783. #else
  784. static inline void vc4_debugfs_add_regset32(struct drm_device *drm,
  785. const char *filename,
  786. struct debugfs_regset32 *regset)
  787. {}
  788. #endif
  789. /* vc4_drv.c */
  790. void __iomem *vc4_ioremap_regs(struct platform_device *dev, int index);
  791. int vc4_dumb_fixup_args(struct drm_mode_create_dumb *args);
  792. /* vc4_dpi.c */
  793. extern struct platform_driver vc4_dpi_driver;
  794. /* vc4_dsi.c */
  795. extern struct platform_driver vc4_dsi_driver;
  796. /* vc4_fence.c */
  797. extern const struct dma_fence_ops vc4_fence_ops;
  798. /* vc4_gem.c */
  799. int vc4_gem_init(struct drm_device *dev);
  800. int vc4_submit_cl_ioctl(struct drm_device *dev, void *data,
  801. struct drm_file *file_priv);
  802. int vc4_wait_seqno_ioctl(struct drm_device *dev, void *data,
  803. struct drm_file *file_priv);
  804. int vc4_wait_bo_ioctl(struct drm_device *dev, void *data,
  805. struct drm_file *file_priv);
  806. void vc4_submit_next_bin_job(struct drm_device *dev);
  807. void vc4_submit_next_render_job(struct drm_device *dev);
  808. void vc4_move_job_to_render(struct drm_device *dev, struct vc4_exec_info *exec);
  809. int vc4_wait_for_seqno(struct drm_device *dev, uint64_t seqno,
  810. uint64_t timeout_ns, bool interruptible);
  811. void vc4_job_handle_completed(struct vc4_dev *vc4);
  812. int vc4_queue_seqno_cb(struct drm_device *dev,
  813. struct vc4_seqno_cb *cb, uint64_t seqno,
  814. void (*func)(struct vc4_seqno_cb *cb));
  815. int vc4_gem_madvise_ioctl(struct drm_device *dev, void *data,
  816. struct drm_file *file_priv);
  817. /* vc4_hdmi.c */
  818. extern struct platform_driver vc4_hdmi_driver;
  819. /* vc4_vec.c */
  820. extern struct platform_driver vc4_vec_driver;
  821. /* vc4_txp.c */
  822. extern struct platform_driver vc4_txp_driver;
  823. /* vc4_irq.c */
  824. void vc4_irq_enable(struct drm_device *dev);
  825. void vc4_irq_disable(struct drm_device *dev);
  826. int vc4_irq_install(struct drm_device *dev, int irq);
  827. void vc4_irq_uninstall(struct drm_device *dev);
  828. void vc4_irq_reset(struct drm_device *dev);
  829. /* vc4_hvs.c */
  830. extern struct platform_driver vc4_hvs_driver;
  831. struct vc4_hvs *__vc4_hvs_alloc(struct vc4_dev *vc4, struct platform_device *pdev);
  832. void vc4_hvs_stop_channel(struct vc4_hvs *hvs, unsigned int output);
  833. int vc4_hvs_get_fifo_from_output(struct vc4_hvs *hvs, unsigned int output);
  834. u8 vc4_hvs_get_fifo_frame_count(struct vc4_hvs *hvs, unsigned int fifo);
  835. int vc4_hvs_atomic_check(struct drm_crtc *crtc, struct drm_atomic_state *state);
  836. void vc4_hvs_atomic_begin(struct drm_crtc *crtc, struct drm_atomic_state *state);
  837. void vc4_hvs_atomic_enable(struct drm_crtc *crtc, struct drm_atomic_state *state);
  838. void vc4_hvs_atomic_disable(struct drm_crtc *crtc, struct drm_atomic_state *state);
  839. void vc4_hvs_atomic_flush(struct drm_crtc *crtc, struct drm_atomic_state *state);
  840. void vc4_hvs_dump_state(struct vc4_hvs *hvs);
  841. void vc4_hvs_unmask_underrun(struct vc4_hvs *hvs, int channel);
  842. void vc4_hvs_mask_underrun(struct vc4_hvs *hvs, int channel);
  843. int vc4_hvs_debugfs_init(struct drm_minor *minor);
  844. /* vc4_kms.c */
  845. int vc4_kms_load(struct drm_device *dev);
  846. /* vc4_plane.c */
  847. struct drm_plane *vc4_plane_init(struct drm_device *dev,
  848. enum drm_plane_type type,
  849. uint32_t possible_crtcs);
  850. int vc4_plane_create_additional_planes(struct drm_device *dev);
  851. u32 vc4_plane_write_dlist(struct drm_plane *plane, u32 __iomem *dlist);
  852. u32 vc4_plane_dlist_size(const struct drm_plane_state *state);
  853. void vc4_plane_async_set_fb(struct drm_plane *plane,
  854. struct drm_framebuffer *fb);
  855. /* vc4_v3d.c */
  856. extern struct platform_driver vc4_v3d_driver;
  857. extern const struct of_device_id vc4_v3d_dt_match[];
  858. int vc4_v3d_get_bin_slot(struct vc4_dev *vc4);
  859. int vc4_v3d_bin_bo_get(struct vc4_dev *vc4, bool *used);
  860. void vc4_v3d_bin_bo_put(struct vc4_dev *vc4);
  861. int vc4_v3d_pm_get(struct vc4_dev *vc4);
  862. void vc4_v3d_pm_put(struct vc4_dev *vc4);
  863. int vc4_v3d_debugfs_init(struct drm_minor *minor);
  864. /* vc4_validate.c */
  865. int
  866. vc4_validate_bin_cl(struct drm_device *dev,
  867. void *validated,
  868. void *unvalidated,
  869. struct vc4_exec_info *exec);
  870. int
  871. vc4_validate_shader_recs(struct drm_device *dev, struct vc4_exec_info *exec);
  872. struct drm_gem_dma_object *vc4_use_bo(struct vc4_exec_info *exec,
  873. uint32_t hindex);
  874. int vc4_get_rcl(struct drm_device *dev, struct vc4_exec_info *exec);
  875. bool vc4_check_tex_size(struct vc4_exec_info *exec,
  876. struct drm_gem_dma_object *fbo,
  877. uint32_t offset, uint8_t tiling_format,
  878. uint32_t width, uint32_t height, uint8_t cpp);
  879. /* vc4_validate_shader.c */
  880. struct vc4_validated_shader_info *
  881. vc4_validate_shader(struct drm_gem_dma_object *shader_obj);
  882. /* vc4_perfmon.c */
  883. void vc4_perfmon_get(struct vc4_perfmon *perfmon);
  884. void vc4_perfmon_put(struct vc4_perfmon *perfmon);
  885. void vc4_perfmon_start(struct vc4_dev *vc4, struct vc4_perfmon *perfmon);
  886. void vc4_perfmon_stop(struct vc4_dev *vc4, struct vc4_perfmon *perfmon,
  887. bool capture);
  888. struct vc4_perfmon *vc4_perfmon_find(struct vc4_file *vc4file, int id);
  889. void vc4_perfmon_open_file(struct vc4_file *vc4file);
  890. void vc4_perfmon_close_file(struct vc4_file *vc4file);
  891. int vc4_perfmon_create_ioctl(struct drm_device *dev, void *data,
  892. struct drm_file *file_priv);
  893. int vc4_perfmon_destroy_ioctl(struct drm_device *dev, void *data,
  894. struct drm_file *file_priv);
  895. int vc4_perfmon_get_values_ioctl(struct drm_device *dev, void *data,
  896. struct drm_file *file_priv);
  897. #endif /* _VC4_DRV_H_ */