panfrost_device.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /* Copyright 2018 Marty E. Plummer <hanetzer@startmail.com> */
  3. /* Copyright 2019 Linaro, Ltd, Rob Herring <robh@kernel.org> */
  4. #ifndef __PANFROST_DEVICE_H__
  5. #define __PANFROST_DEVICE_H__
  6. #include <linux/atomic.h>
  7. #include <linux/io-pgtable.h>
  8. #include <linux/pm.h>
  9. #include <linux/regulator/consumer.h>
  10. #include <linux/spinlock.h>
  11. #include <drm/drm_auth.h>
  12. #include <drm/drm_device.h>
  13. #include <drm/drm_mm.h>
  14. #include <drm/gpu_scheduler.h>
  15. #include "panfrost_devfreq.h"
  16. #include "panfrost_job.h"
  17. struct panfrost_device;
  18. struct panfrost_mmu;
  19. struct panfrost_job_slot;
  20. struct panfrost_job;
  21. struct panfrost_perfcnt;
  22. #define MAX_PM_DOMAINS 5
  23. #define ALL_JS_INT_MASK \
  24. (GENMASK(16 + NUM_JOB_SLOTS - 1, 16) | \
  25. GENMASK(NUM_JOB_SLOTS - 1, 0))
  26. enum panfrost_drv_comp_bits {
  27. PANFROST_COMP_BIT_GPU,
  28. PANFROST_COMP_BIT_JOB,
  29. PANFROST_COMP_BIT_MMU,
  30. PANFROST_COMP_BIT_MAX
  31. };
  32. /**
  33. * enum panfrost_gpu_pm - Supported kernel power management features
  34. * @GPU_PM_CLK_DIS: Allow disabling clocks during system suspend
  35. * @GPU_PM_VREG_OFF: Allow turning off regulators during system suspend
  36. * @GPU_PM_RT: Allow disabling clocks and asserting the reset control during
  37. * system runtime suspend
  38. */
  39. enum panfrost_gpu_pm {
  40. GPU_PM_CLK_DIS,
  41. GPU_PM_VREG_OFF,
  42. GPU_PM_RT
  43. };
  44. /**
  45. * enum panfrost_gpu_quirks - GPU optional quirks
  46. * @GPU_QUIRK_FORCE_AARCH64_PGTABLE: Use AARCH64_4K page table format
  47. */
  48. enum panfrost_gpu_quirks {
  49. GPU_QUIRK_FORCE_AARCH64_PGTABLE,
  50. };
  51. struct panfrost_features {
  52. u16 id;
  53. u16 revision;
  54. u64 shader_present;
  55. u64 tiler_present;
  56. u64 l2_present;
  57. u64 stack_present;
  58. u32 as_present;
  59. u32 js_present;
  60. u32 l2_features;
  61. u32 core_features;
  62. u32 tiler_features;
  63. u32 mem_features;
  64. u32 mmu_features;
  65. u32 thread_features;
  66. u32 max_threads;
  67. u32 thread_max_workgroup_sz;
  68. u32 thread_max_barrier_sz;
  69. u32 coherency_features;
  70. u32 selected_coherency;
  71. u32 afbc_features;
  72. u32 texture_features[4];
  73. u32 js_features[16];
  74. u32 nr_core_groups;
  75. u32 thread_tls_alloc;
  76. unsigned long hw_features[64 / BITS_PER_LONG];
  77. unsigned long hw_issues[64 / BITS_PER_LONG];
  78. };
  79. /*
  80. * Features that cannot be automatically detected and need matching using the
  81. * compatible string, typically SoC-specific.
  82. */
  83. struct panfrost_compatible {
  84. /* Supplies count and names. */
  85. int num_supplies;
  86. const char * const *supply_names;
  87. /*
  88. * Number of power domains required, note that values 0 and 1 are
  89. * handled identically, as only values > 1 need special handling.
  90. */
  91. int num_pm_domains;
  92. /* Only required if num_pm_domains > 1. */
  93. const char * const *pm_domain_names;
  94. /* Vendor implementation quirks callback */
  95. void (*vendor_quirk)(struct panfrost_device *pfdev);
  96. /* Allowed PM features */
  97. u8 pm_features;
  98. /* GPU configuration quirks */
  99. u8 gpu_quirks;
  100. };
  101. /**
  102. * struct panfrost_device_debugfs - Device-wide DebugFS tracking structures
  103. */
  104. struct panfrost_device_debugfs {
  105. /** @gems_list: Device-wide list of GEM objects owned by at least one file. */
  106. struct list_head gems_list;
  107. /** @gems_lock: Serializes access to the device-wide list of GEM objects. */
  108. struct mutex gems_lock;
  109. };
  110. struct panfrost_device {
  111. struct drm_device base;
  112. int gpu_irq;
  113. int mmu_irq;
  114. void __iomem *iomem;
  115. struct clk *clock;
  116. struct clk *bus_clock;
  117. struct regulator_bulk_data *regulators;
  118. struct reset_control *rstc;
  119. /* pm_domains for devices with more than one. */
  120. struct device *pm_domain_devs[MAX_PM_DOMAINS];
  121. struct device_link *pm_domain_links[MAX_PM_DOMAINS];
  122. bool coherent;
  123. struct panfrost_features features;
  124. const struct panfrost_compatible *comp;
  125. DECLARE_BITMAP(is_suspended, PANFROST_COMP_BIT_MAX);
  126. spinlock_t as_lock;
  127. unsigned long as_alloc_mask;
  128. unsigned long as_faulty_mask;
  129. struct list_head as_lru_list;
  130. struct panfrost_job_slot *js;
  131. struct panfrost_job *jobs[NUM_JOB_SLOTS][2];
  132. struct list_head scheduled_jobs;
  133. struct panfrost_perfcnt *perfcnt;
  134. bool profile_mode;
  135. struct mutex sched_lock;
  136. struct {
  137. struct workqueue_struct *wq;
  138. struct work_struct work;
  139. atomic_t pending;
  140. } reset;
  141. struct mutex shrinker_lock;
  142. struct list_head shrinker_list;
  143. struct shrinker *shrinker;
  144. struct panfrost_devfreq pfdevfreq;
  145. struct {
  146. atomic_t use_count;
  147. spinlock_t lock;
  148. } cycle_counter;
  149. #ifdef CONFIG_DEBUG_FS
  150. struct panfrost_device_debugfs debugfs;
  151. #endif
  152. };
  153. struct panfrost_mmu {
  154. struct panfrost_device *pfdev;
  155. struct kref refcount;
  156. struct io_pgtable_cfg pgtbl_cfg;
  157. struct io_pgtable_ops *pgtbl_ops;
  158. struct drm_mm mm;
  159. spinlock_t mm_lock;
  160. int as;
  161. atomic_t as_count;
  162. struct list_head list;
  163. struct {
  164. u64 transtab;
  165. u64 memattr;
  166. u64 transcfg;
  167. } cfg;
  168. };
  169. struct panfrost_engine_usage {
  170. unsigned long long elapsed_ns[NUM_JOB_SLOTS];
  171. unsigned long long cycles[NUM_JOB_SLOTS];
  172. };
  173. struct panfrost_file_priv {
  174. struct panfrost_device *pfdev;
  175. struct xarray jm_ctxs;
  176. struct panfrost_mmu *mmu;
  177. struct panfrost_engine_usage engine_usage;
  178. };
  179. static inline bool panfrost_high_prio_allowed(struct drm_file *file)
  180. {
  181. /* Higher priorities require CAP_SYS_NICE or DRM_MASTER */
  182. return (capable(CAP_SYS_NICE) || drm_is_current_master(file));
  183. }
  184. static inline struct panfrost_device *to_panfrost_device(struct drm_device *ddev)
  185. {
  186. return container_of(ddev, struct panfrost_device, base);
  187. }
  188. static inline int panfrost_model_cmp(struct panfrost_device *pfdev, s32 id)
  189. {
  190. s32 match_id = pfdev->features.id;
  191. if (match_id & 0xf000)
  192. match_id &= 0xf00f;
  193. return match_id - id;
  194. }
  195. static inline bool panfrost_model_is_bifrost(struct panfrost_device *pfdev)
  196. {
  197. return panfrost_model_cmp(pfdev, 0x1000) >= 0;
  198. }
  199. static inline bool panfrost_model_eq(struct panfrost_device *pfdev, s32 id)
  200. {
  201. return !panfrost_model_cmp(pfdev, id);
  202. }
  203. int panfrost_unstable_ioctl_check(void);
  204. int panfrost_device_init(struct panfrost_device *pfdev);
  205. void panfrost_device_fini(struct panfrost_device *pfdev);
  206. void panfrost_device_reset(struct panfrost_device *pfdev, bool enable_job_int);
  207. extern const struct dev_pm_ops panfrost_pm_ops;
  208. enum drm_panfrost_exception_type {
  209. DRM_PANFROST_EXCEPTION_OK = 0x00,
  210. DRM_PANFROST_EXCEPTION_DONE = 0x01,
  211. DRM_PANFROST_EXCEPTION_INTERRUPTED = 0x02,
  212. DRM_PANFROST_EXCEPTION_STOPPED = 0x03,
  213. DRM_PANFROST_EXCEPTION_TERMINATED = 0x04,
  214. DRM_PANFROST_EXCEPTION_KABOOM = 0x05,
  215. DRM_PANFROST_EXCEPTION_EUREKA = 0x06,
  216. DRM_PANFROST_EXCEPTION_ACTIVE = 0x08,
  217. DRM_PANFROST_EXCEPTION_MAX_NON_FAULT = 0x3f,
  218. DRM_PANFROST_EXCEPTION_JOB_CONFIG_FAULT = 0x40,
  219. DRM_PANFROST_EXCEPTION_JOB_POWER_FAULT = 0x41,
  220. DRM_PANFROST_EXCEPTION_JOB_READ_FAULT = 0x42,
  221. DRM_PANFROST_EXCEPTION_JOB_WRITE_FAULT = 0x43,
  222. DRM_PANFROST_EXCEPTION_JOB_AFFINITY_FAULT = 0x44,
  223. DRM_PANFROST_EXCEPTION_JOB_BUS_FAULT = 0x48,
  224. DRM_PANFROST_EXCEPTION_INSTR_INVALID_PC = 0x50,
  225. DRM_PANFROST_EXCEPTION_INSTR_INVALID_ENC = 0x51,
  226. DRM_PANFROST_EXCEPTION_INSTR_TYPE_MISMATCH = 0x52,
  227. DRM_PANFROST_EXCEPTION_INSTR_OPERAND_FAULT = 0x53,
  228. DRM_PANFROST_EXCEPTION_INSTR_TLS_FAULT = 0x54,
  229. DRM_PANFROST_EXCEPTION_INSTR_BARRIER_FAULT = 0x55,
  230. DRM_PANFROST_EXCEPTION_INSTR_ALIGN_FAULT = 0x56,
  231. DRM_PANFROST_EXCEPTION_DATA_INVALID_FAULT = 0x58,
  232. DRM_PANFROST_EXCEPTION_TILE_RANGE_FAULT = 0x59,
  233. DRM_PANFROST_EXCEPTION_ADDR_RANGE_FAULT = 0x5a,
  234. DRM_PANFROST_EXCEPTION_IMPRECISE_FAULT = 0x5b,
  235. DRM_PANFROST_EXCEPTION_OOM = 0x60,
  236. DRM_PANFROST_EXCEPTION_OOM_AFBC = 0x61,
  237. DRM_PANFROST_EXCEPTION_UNKNOWN = 0x7f,
  238. DRM_PANFROST_EXCEPTION_DELAYED_BUS_FAULT = 0x80,
  239. DRM_PANFROST_EXCEPTION_GPU_SHAREABILITY_FAULT = 0x88,
  240. DRM_PANFROST_EXCEPTION_SYS_SHAREABILITY_FAULT = 0x89,
  241. DRM_PANFROST_EXCEPTION_GPU_CACHEABILITY_FAULT = 0x8a,
  242. DRM_PANFROST_EXCEPTION_TRANSLATION_FAULT_0 = 0xc0,
  243. DRM_PANFROST_EXCEPTION_TRANSLATION_FAULT_1 = 0xc1,
  244. DRM_PANFROST_EXCEPTION_TRANSLATION_FAULT_2 = 0xc2,
  245. DRM_PANFROST_EXCEPTION_TRANSLATION_FAULT_3 = 0xc3,
  246. DRM_PANFROST_EXCEPTION_TRANSLATION_FAULT_4 = 0xc4,
  247. DRM_PANFROST_EXCEPTION_TRANSLATION_FAULT_IDENTITY = 0xc7,
  248. DRM_PANFROST_EXCEPTION_PERM_FAULT_0 = 0xc8,
  249. DRM_PANFROST_EXCEPTION_PERM_FAULT_1 = 0xc9,
  250. DRM_PANFROST_EXCEPTION_PERM_FAULT_2 = 0xca,
  251. DRM_PANFROST_EXCEPTION_PERM_FAULT_3 = 0xcb,
  252. DRM_PANFROST_EXCEPTION_TRANSTAB_BUS_FAULT_0 = 0xd0,
  253. DRM_PANFROST_EXCEPTION_TRANSTAB_BUS_FAULT_1 = 0xd1,
  254. DRM_PANFROST_EXCEPTION_TRANSTAB_BUS_FAULT_2 = 0xd2,
  255. DRM_PANFROST_EXCEPTION_TRANSTAB_BUS_FAULT_3 = 0xd3,
  256. DRM_PANFROST_EXCEPTION_ACCESS_FLAG_0 = 0xd8,
  257. DRM_PANFROST_EXCEPTION_ACCESS_FLAG_1 = 0xd9,
  258. DRM_PANFROST_EXCEPTION_ACCESS_FLAG_2 = 0xda,
  259. DRM_PANFROST_EXCEPTION_ACCESS_FLAG_3 = 0xdb,
  260. DRM_PANFROST_EXCEPTION_ADDR_SIZE_FAULT_IN0 = 0xe0,
  261. DRM_PANFROST_EXCEPTION_ADDR_SIZE_FAULT_IN1 = 0xe1,
  262. DRM_PANFROST_EXCEPTION_ADDR_SIZE_FAULT_IN2 = 0xe2,
  263. DRM_PANFROST_EXCEPTION_ADDR_SIZE_FAULT_IN3 = 0xe3,
  264. DRM_PANFROST_EXCEPTION_ADDR_SIZE_FAULT_OUT0 = 0xe4,
  265. DRM_PANFROST_EXCEPTION_ADDR_SIZE_FAULT_OUT1 = 0xe5,
  266. DRM_PANFROST_EXCEPTION_ADDR_SIZE_FAULT_OUT2 = 0xe6,
  267. DRM_PANFROST_EXCEPTION_ADDR_SIZE_FAULT_OUT3 = 0xe7,
  268. DRM_PANFROST_EXCEPTION_MEM_ATTR_FAULT_0 = 0xe8,
  269. DRM_PANFROST_EXCEPTION_MEM_ATTR_FAULT_1 = 0xe9,
  270. DRM_PANFROST_EXCEPTION_MEM_ATTR_FAULT_2 = 0xea,
  271. DRM_PANFROST_EXCEPTION_MEM_ATTR_FAULT_3 = 0xeb,
  272. DRM_PANFROST_EXCEPTION_MEM_ATTR_NONCACHE_0 = 0xec,
  273. DRM_PANFROST_EXCEPTION_MEM_ATTR_NONCACHE_1 = 0xed,
  274. DRM_PANFROST_EXCEPTION_MEM_ATTR_NONCACHE_2 = 0xee,
  275. DRM_PANFROST_EXCEPTION_MEM_ATTR_NONCACHE_3 = 0xef,
  276. };
  277. static inline bool
  278. panfrost_exception_is_fault(u32 exception_code)
  279. {
  280. return exception_code > DRM_PANFROST_EXCEPTION_MAX_NON_FAULT;
  281. }
  282. const char *panfrost_exception_name(u32 exception_code);
  283. bool panfrost_exception_needs_reset(const struct panfrost_device *pfdev,
  284. u32 exception_code);
  285. static inline void
  286. panfrost_device_schedule_reset(struct panfrost_device *pfdev)
  287. {
  288. atomic_set(&pfdev->reset.pending, 1);
  289. queue_work(pfdev->reset.wq, &pfdev->reset.work);
  290. }
  291. #endif