msm_drv.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (c) 2016-2018, The Linux Foundation. All rights reserved.
  4. * Copyright (C) 2013 Red Hat
  5. * Author: Rob Clark <robdclark@gmail.com>
  6. */
  7. #ifndef __MSM_DRV_H__
  8. #define __MSM_DRV_H__
  9. #include <linux/kernel.h>
  10. #include <linux/clk.h>
  11. #include <linux/cpufreq.h>
  12. #include <linux/devfreq.h>
  13. #include <linux/module.h>
  14. #include <linux/component.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/pm.h>
  17. #include <linux/pm_runtime.h>
  18. #include <linux/slab.h>
  19. #include <linux/list.h>
  20. #include <linux/iommu.h>
  21. #include <linux/types.h>
  22. #include <linux/of_graph.h>
  23. #include <linux/of_device.h>
  24. #include <linux/sizes.h>
  25. #include <linux/kthread.h>
  26. #include <drm/drm_atomic.h>
  27. #include <drm/drm_atomic_helper.h>
  28. #include <drm/drm_probe_helper.h>
  29. #include <drm/display/drm_dsc.h>
  30. #include <drm/msm_drm.h>
  31. #include <drm/drm_gem.h>
  32. extern struct fault_attr fail_gem_alloc;
  33. extern struct fault_attr fail_gem_iova;
  34. struct msm_kms;
  35. struct msm_gpu;
  36. struct msm_mmu;
  37. struct msm_mdss;
  38. struct msm_rd_state;
  39. struct msm_perf_state;
  40. struct msm_gem_submit;
  41. struct msm_fence_context;
  42. struct msm_gem_address_space;
  43. struct msm_gem_vma;
  44. struct msm_disp_state;
  45. #define MAX_CRTCS 8
  46. #define MAX_BRIDGES 8
  47. #define FRAC_16_16(mult, div) (((mult) << 16) / (div))
  48. enum msm_dp_controller {
  49. MSM_DP_CONTROLLER_0,
  50. MSM_DP_CONTROLLER_1,
  51. MSM_DP_CONTROLLER_2,
  52. MSM_DP_CONTROLLER_3,
  53. MSM_DP_CONTROLLER_COUNT,
  54. };
  55. enum msm_dsi_controller {
  56. MSM_DSI_CONTROLLER_0,
  57. MSM_DSI_CONTROLLER_1,
  58. MSM_DSI_CONTROLLER_COUNT,
  59. };
  60. #define MSM_GPU_MAX_RINGS 4
  61. #define MAX_H_TILES_PER_DISPLAY 2
  62. /**
  63. * struct msm_display_topology - defines a display topology pipeline
  64. * @num_lm: number of layer mixers used
  65. * @num_intf: number of interfaces the panel is mounted on
  66. * @num_dspp: number of dspp blocks used
  67. * @num_dsc: number of Display Stream Compression (DSC) blocks used
  68. * @needs_cdm: indicates whether cdm block is needed for this display topology
  69. */
  70. struct msm_display_topology {
  71. u32 num_lm;
  72. u32 num_intf;
  73. u32 num_dspp;
  74. u32 num_dsc;
  75. bool needs_cdm;
  76. };
  77. /* Commit/Event thread specific structure */
  78. struct msm_drm_thread {
  79. struct drm_device *dev;
  80. struct kthread_worker *worker;
  81. };
  82. struct msm_drm_private {
  83. struct drm_device *dev;
  84. struct msm_kms *kms;
  85. int (*kms_init)(struct drm_device *dev);
  86. /* subordinate devices, if present: */
  87. struct platform_device *gpu_pdev;
  88. /* possibly this should be in the kms component, but it is
  89. * shared by both mdp4 and mdp5..
  90. */
  91. struct hdmi *hdmi;
  92. /* DSI is shared by mdp4 and mdp5 */
  93. struct msm_dsi *dsi[MSM_DSI_CONTROLLER_COUNT];
  94. struct msm_dp *dp[MSM_DP_CONTROLLER_COUNT];
  95. /* when we have more than one 'msm_gpu' these need to be an array: */
  96. struct msm_gpu *gpu;
  97. /* gpu is only set on open(), but we need this info earlier */
  98. bool is_a2xx;
  99. bool has_cached_coherent;
  100. struct msm_rd_state *rd; /* debugfs to dump all submits */
  101. struct msm_rd_state *hangrd; /* debugfs to dump hanging submits */
  102. struct msm_perf_state *perf;
  103. /**
  104. * total_mem: Total/global amount of memory backing GEM objects.
  105. */
  106. atomic64_t total_mem;
  107. /**
  108. * List of all GEM objects (mainly for debugfs, protected by obj_lock
  109. * (acquire before per GEM object lock)
  110. */
  111. struct list_head objects;
  112. struct mutex obj_lock;
  113. /**
  114. * lru:
  115. *
  116. * The various LRU's that a GEM object is in at various stages of
  117. * it's lifetime. Objects start out in the unbacked LRU. When
  118. * pinned (for scannout or permanently mapped GPU buffers, like
  119. * ringbuffer, memptr, fw, etc) it moves to the pinned LRU. When
  120. * unpinned, it moves into willneed or dontneed LRU depending on
  121. * madvise state. When backing pages are evicted (willneed) or
  122. * purged (dontneed) it moves back into the unbacked LRU.
  123. *
  124. * The dontneed LRU is considered by the shrinker for objects
  125. * that are candidate for purging, and the willneed LRU is
  126. * considered for objects that could be evicted.
  127. */
  128. struct {
  129. /**
  130. * unbacked:
  131. *
  132. * The LRU for GEM objects without backing pages allocated.
  133. * This mostly exists so that objects are always is one
  134. * LRU.
  135. */
  136. struct drm_gem_lru unbacked;
  137. /**
  138. * pinned:
  139. *
  140. * The LRU for pinned GEM objects
  141. */
  142. struct drm_gem_lru pinned;
  143. /**
  144. * willneed:
  145. *
  146. * The LRU for unpinned GEM objects which are in madvise
  147. * WILLNEED state (ie. can be evicted)
  148. */
  149. struct drm_gem_lru willneed;
  150. /**
  151. * dontneed:
  152. *
  153. * The LRU for unpinned GEM objects which are in madvise
  154. * DONTNEED state (ie. can be purged)
  155. */
  156. struct drm_gem_lru dontneed;
  157. /**
  158. * lock:
  159. *
  160. * Protects manipulation of all of the LRUs.
  161. */
  162. struct mutex lock;
  163. } lru;
  164. struct workqueue_struct *wq;
  165. unsigned int num_crtcs;
  166. struct msm_drm_thread event_thread[MAX_CRTCS];
  167. /* VRAM carveout, used when no IOMMU: */
  168. struct {
  169. unsigned long size;
  170. dma_addr_t paddr;
  171. /* NOTE: mm managed at the page level, size is in # of pages
  172. * and position mm_node->start is in # of pages:
  173. */
  174. struct drm_mm mm;
  175. spinlock_t lock; /* Protects drm_mm node allocation/removal */
  176. } vram;
  177. struct notifier_block vmap_notifier;
  178. struct shrinker *shrinker;
  179. /**
  180. * hangcheck_period: For hang detection, in ms
  181. *
  182. * Note that in practice, a submit/job will get at least two hangcheck
  183. * periods, due to checking for progress being implemented as simply
  184. * "have the CP position registers changed since last time?"
  185. */
  186. unsigned int hangcheck_period;
  187. /** gpu_devfreq_config: Devfreq tuning config for the GPU. */
  188. struct devfreq_simple_ondemand_data gpu_devfreq_config;
  189. /**
  190. * gpu_clamp_to_idle: Enable clamping to idle freq when inactive
  191. */
  192. bool gpu_clamp_to_idle;
  193. /**
  194. * disable_err_irq:
  195. *
  196. * Disable handling of GPU hw error interrupts, to force fallback to
  197. * sw hangcheck timer. Written (via debugfs) by igt tests to test
  198. * the sw hangcheck mechanism.
  199. */
  200. bool disable_err_irq;
  201. };
  202. const struct msm_format *mdp_get_format(struct msm_kms *kms, uint32_t format, uint64_t modifier);
  203. struct msm_pending_timer;
  204. int msm_atomic_init_pending_timer(struct msm_pending_timer *timer,
  205. struct msm_kms *kms, int crtc_idx);
  206. void msm_atomic_destroy_pending_timer(struct msm_pending_timer *timer);
  207. void msm_atomic_commit_tail(struct drm_atomic_state *state);
  208. int msm_atomic_check(struct drm_device *dev, struct drm_atomic_state *state);
  209. struct drm_atomic_state *msm_atomic_state_alloc(struct drm_device *dev);
  210. int msm_crtc_enable_vblank(struct drm_crtc *crtc);
  211. void msm_crtc_disable_vblank(struct drm_crtc *crtc);
  212. int msm_register_mmu(struct drm_device *dev, struct msm_mmu *mmu);
  213. void msm_unregister_mmu(struct drm_device *dev, struct msm_mmu *mmu);
  214. struct msm_gem_address_space *msm_kms_init_aspace(struct drm_device *dev);
  215. bool msm_use_mmu(struct drm_device *dev);
  216. int msm_ioctl_gem_submit(struct drm_device *dev, void *data,
  217. struct drm_file *file);
  218. #ifdef CONFIG_DEBUG_FS
  219. unsigned long msm_gem_shrinker_shrink(struct drm_device *dev, unsigned long nr_to_scan);
  220. #endif
  221. int msm_gem_shrinker_init(struct drm_device *dev);
  222. void msm_gem_shrinker_cleanup(struct drm_device *dev);
  223. struct sg_table *msm_gem_prime_get_sg_table(struct drm_gem_object *obj);
  224. int msm_gem_prime_vmap(struct drm_gem_object *obj, struct iosys_map *map);
  225. void msm_gem_prime_vunmap(struct drm_gem_object *obj, struct iosys_map *map);
  226. struct drm_gem_object *msm_gem_prime_import_sg_table(struct drm_device *dev,
  227. struct dma_buf_attachment *attach, struct sg_table *sg);
  228. int msm_gem_prime_pin(struct drm_gem_object *obj);
  229. void msm_gem_prime_unpin(struct drm_gem_object *obj);
  230. int msm_framebuffer_prepare(struct drm_framebuffer *fb,
  231. struct msm_gem_address_space *aspace, bool needs_dirtyfb);
  232. void msm_framebuffer_cleanup(struct drm_framebuffer *fb,
  233. struct msm_gem_address_space *aspace, bool needed_dirtyfb);
  234. uint32_t msm_framebuffer_iova(struct drm_framebuffer *fb,
  235. struct msm_gem_address_space *aspace, int plane);
  236. struct drm_gem_object *msm_framebuffer_bo(struct drm_framebuffer *fb, int plane);
  237. const struct msm_format *msm_framebuffer_format(struct drm_framebuffer *fb);
  238. struct drm_framebuffer *msm_framebuffer_create(struct drm_device *dev,
  239. struct drm_file *file, const struct drm_mode_fb_cmd2 *mode_cmd);
  240. struct drm_framebuffer * msm_alloc_stolen_fb(struct drm_device *dev,
  241. int w, int h, int p, uint32_t format);
  242. #ifdef CONFIG_DRM_FBDEV_EMULATION
  243. void msm_fbdev_setup(struct drm_device *dev);
  244. #else
  245. static inline void msm_fbdev_setup(struct drm_device *dev)
  246. {
  247. }
  248. #endif
  249. struct hdmi;
  250. #ifdef CONFIG_DRM_MSM_HDMI
  251. int msm_hdmi_modeset_init(struct hdmi *hdmi, struct drm_device *dev,
  252. struct drm_encoder *encoder);
  253. void __init msm_hdmi_register(void);
  254. void __exit msm_hdmi_unregister(void);
  255. #else
  256. static inline int msm_hdmi_modeset_init(struct hdmi *hdmi, struct drm_device *dev,
  257. struct drm_encoder *encoder)
  258. {
  259. return -EINVAL;
  260. }
  261. static inline void __init msm_hdmi_register(void) {}
  262. static inline void __exit msm_hdmi_unregister(void) {}
  263. #endif
  264. struct msm_dsi;
  265. #ifdef CONFIG_DRM_MSM_DSI
  266. int dsi_dev_attach(struct platform_device *pdev);
  267. void dsi_dev_detach(struct platform_device *pdev);
  268. void __init msm_dsi_register(void);
  269. void __exit msm_dsi_unregister(void);
  270. int msm_dsi_modeset_init(struct msm_dsi *msm_dsi, struct drm_device *dev,
  271. struct drm_encoder *encoder);
  272. void msm_dsi_snapshot(struct msm_disp_state *disp_state, struct msm_dsi *msm_dsi);
  273. bool msm_dsi_is_cmd_mode(struct msm_dsi *msm_dsi);
  274. bool msm_dsi_is_bonded_dsi(struct msm_dsi *msm_dsi);
  275. bool msm_dsi_is_master_dsi(struct msm_dsi *msm_dsi);
  276. bool msm_dsi_wide_bus_enabled(struct msm_dsi *msm_dsi);
  277. struct drm_dsc_config *msm_dsi_get_dsc_config(struct msm_dsi *msm_dsi);
  278. const char *msm_dsi_get_te_source(struct msm_dsi *msm_dsi);
  279. #else
  280. static inline void __init msm_dsi_register(void)
  281. {
  282. }
  283. static inline void __exit msm_dsi_unregister(void)
  284. {
  285. }
  286. static inline int msm_dsi_modeset_init(struct msm_dsi *msm_dsi,
  287. struct drm_device *dev,
  288. struct drm_encoder *encoder)
  289. {
  290. return -EINVAL;
  291. }
  292. static inline void msm_dsi_snapshot(struct msm_disp_state *disp_state, struct msm_dsi *msm_dsi)
  293. {
  294. }
  295. static inline bool msm_dsi_is_cmd_mode(struct msm_dsi *msm_dsi)
  296. {
  297. return false;
  298. }
  299. static inline bool msm_dsi_is_bonded_dsi(struct msm_dsi *msm_dsi)
  300. {
  301. return false;
  302. }
  303. static inline bool msm_dsi_is_master_dsi(struct msm_dsi *msm_dsi)
  304. {
  305. return false;
  306. }
  307. static inline bool msm_dsi_wide_bus_enabled(struct msm_dsi *msm_dsi)
  308. {
  309. return false;
  310. }
  311. static inline struct drm_dsc_config *msm_dsi_get_dsc_config(struct msm_dsi *msm_dsi)
  312. {
  313. return NULL;
  314. }
  315. static inline const char *msm_dsi_get_te_source(struct msm_dsi *msm_dsi)
  316. {
  317. return NULL;
  318. }
  319. #endif
  320. #ifdef CONFIG_DRM_MSM_DP
  321. int __init msm_dp_register(void);
  322. void __exit msm_dp_unregister(void);
  323. int msm_dp_modeset_init(struct msm_dp *dp_display, struct drm_device *dev,
  324. struct drm_encoder *encoder, bool yuv_supported);
  325. void msm_dp_snapshot(struct msm_disp_state *disp_state, struct msm_dp *dp_display);
  326. bool msm_dp_is_yuv_420_enabled(const struct msm_dp *dp_display,
  327. const struct drm_display_mode *mode);
  328. bool msm_dp_needs_periph_flush(const struct msm_dp *dp_display,
  329. const struct drm_display_mode *mode);
  330. bool msm_dp_wide_bus_available(const struct msm_dp *dp_display);
  331. #else
  332. static inline int __init msm_dp_register(void)
  333. {
  334. return -EINVAL;
  335. }
  336. static inline void __exit msm_dp_unregister(void)
  337. {
  338. }
  339. static inline int msm_dp_modeset_init(struct msm_dp *dp_display,
  340. struct drm_device *dev,
  341. struct drm_encoder *encoder,
  342. bool yuv_supported)
  343. {
  344. return -EINVAL;
  345. }
  346. static inline void msm_dp_snapshot(struct msm_disp_state *disp_state, struct msm_dp *dp_display)
  347. {
  348. }
  349. static inline bool msm_dp_is_yuv_420_enabled(const struct msm_dp *dp_display,
  350. const struct drm_display_mode *mode)
  351. {
  352. return false;
  353. }
  354. static inline bool msm_dp_needs_periph_flush(const struct msm_dp *dp_display,
  355. const struct drm_display_mode *mode)
  356. {
  357. return false;
  358. }
  359. static inline bool msm_dp_wide_bus_available(const struct msm_dp *dp_display)
  360. {
  361. return false;
  362. }
  363. #endif
  364. #ifdef CONFIG_DRM_MSM_MDP4
  365. void msm_mdp4_register(void);
  366. void msm_mdp4_unregister(void);
  367. #else
  368. static inline void msm_mdp4_register(void) {}
  369. static inline void msm_mdp4_unregister(void) {}
  370. #endif
  371. #ifdef CONFIG_DRM_MSM_MDP5
  372. void msm_mdp_register(void);
  373. void msm_mdp_unregister(void);
  374. #else
  375. static inline void msm_mdp_register(void) {}
  376. static inline void msm_mdp_unregister(void) {}
  377. #endif
  378. #ifdef CONFIG_DRM_MSM_DPU
  379. void msm_dpu_register(void);
  380. void msm_dpu_unregister(void);
  381. #else
  382. static inline void msm_dpu_register(void) {}
  383. static inline void msm_dpu_unregister(void) {}
  384. #endif
  385. #ifdef CONFIG_DRM_MSM_MDSS
  386. void msm_mdss_register(void);
  387. void msm_mdss_unregister(void);
  388. #else
  389. static inline void msm_mdss_register(void) {}
  390. static inline void msm_mdss_unregister(void) {}
  391. #endif
  392. #ifdef CONFIG_DEBUG_FS
  393. void msm_framebuffer_describe(struct drm_framebuffer *fb, struct seq_file *m);
  394. int msm_debugfs_late_init(struct drm_device *dev);
  395. int msm_rd_debugfs_init(struct drm_minor *minor);
  396. void msm_rd_debugfs_cleanup(struct msm_drm_private *priv);
  397. __printf(3, 4)
  398. void msm_rd_dump_submit(struct msm_rd_state *rd, struct msm_gem_submit *submit,
  399. const char *fmt, ...);
  400. int msm_perf_debugfs_init(struct drm_minor *minor);
  401. void msm_perf_debugfs_cleanup(struct msm_drm_private *priv);
  402. #else
  403. static inline int msm_debugfs_late_init(struct drm_device *dev) { return 0; }
  404. __printf(3, 4)
  405. static inline void msm_rd_dump_submit(struct msm_rd_state *rd,
  406. struct msm_gem_submit *submit,
  407. const char *fmt, ...) {}
  408. static inline void msm_rd_debugfs_cleanup(struct msm_drm_private *priv) {}
  409. static inline void msm_perf_debugfs_cleanup(struct msm_drm_private *priv) {}
  410. #endif
  411. struct clk *msm_clk_get(struct platform_device *pdev, const char *name);
  412. struct clk *msm_clk_bulk_get_clock(struct clk_bulk_data *bulk, int count,
  413. const char *name);
  414. void __iomem *msm_ioremap(struct platform_device *pdev, const char *name);
  415. void __iomem *msm_ioremap_size(struct platform_device *pdev, const char *name,
  416. phys_addr_t *size);
  417. void __iomem *msm_ioremap_quiet(struct platform_device *pdev, const char *name);
  418. void __iomem *msm_ioremap_mdss(struct platform_device *mdss_pdev,
  419. struct platform_device *dev,
  420. const char *name);
  421. struct icc_path *msm_icc_get(struct device *dev, const char *name);
  422. static inline void msm_rmw(void __iomem *addr, u32 mask, u32 or)
  423. {
  424. u32 val = readl(addr);
  425. val &= ~mask;
  426. writel(val | or, addr);
  427. }
  428. /**
  429. * struct msm_hrtimer_work - a helper to combine an hrtimer with kthread_work
  430. *
  431. * @timer: hrtimer to control when the kthread work is triggered
  432. * @work: the kthread work
  433. * @worker: the kthread worker the work will be scheduled on
  434. */
  435. struct msm_hrtimer_work {
  436. struct hrtimer timer;
  437. struct kthread_work work;
  438. struct kthread_worker *worker;
  439. };
  440. void msm_hrtimer_queue_work(struct msm_hrtimer_work *work,
  441. ktime_t wakeup_time,
  442. enum hrtimer_mode mode);
  443. void msm_hrtimer_work_init(struct msm_hrtimer_work *work,
  444. struct kthread_worker *worker,
  445. kthread_work_func_t fn,
  446. clockid_t clock_id,
  447. enum hrtimer_mode mode);
  448. #define DBG(fmt, ...) DRM_DEBUG_DRIVER(fmt"\n", ##__VA_ARGS__)
  449. #define VERB(fmt, ...) if (0) DRM_DEBUG_DRIVER(fmt"\n", ##__VA_ARGS__)
  450. static inline int align_pitch(int width, int bpp)
  451. {
  452. int bytespp = (bpp + 7) / 8;
  453. /* adreno needs pitch aligned to 32 pixels: */
  454. return bytespp * ALIGN(width, 32);
  455. }
  456. /* for the generated headers: */
  457. #define INVALID_IDX(idx) ({BUG(); 0;})
  458. #define fui(x) ({BUG(); 0;})
  459. #define _mesa_float_to_half(x) ({BUG(); 0;})
  460. #define FIELD(val, name) (((val) & name ## __MASK) >> name ## __SHIFT)
  461. /* for conditionally setting boolean flag(s): */
  462. #define COND(bool, val) ((bool) ? (val) : 0)
  463. static inline unsigned long timeout_to_jiffies(const ktime_t *timeout)
  464. {
  465. ktime_t now = ktime_get();
  466. if (ktime_compare(*timeout, now) <= 0)
  467. return 0;
  468. ktime_t rem = ktime_sub(*timeout, now);
  469. s64 remaining_jiffies = ktime_divns(rem, NSEC_PER_SEC / HZ);
  470. return clamp(remaining_jiffies, 1LL, (s64)INT_MAX);
  471. }
  472. /* Driver helpers */
  473. extern const struct component_master_ops msm_drm_ops;
  474. int msm_kms_pm_prepare(struct device *dev);
  475. void msm_kms_pm_complete(struct device *dev);
  476. int msm_drv_probe(struct device *dev,
  477. int (*kms_init)(struct drm_device *dev),
  478. struct msm_kms *kms);
  479. void msm_kms_shutdown(struct platform_device *pdev);
  480. bool msm_disp_drv_should_bind(struct device *dev, bool dpu_driver);
  481. #endif /* __MSM_DRV_H__ */