panthor_drv.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848
  1. // SPDX-License-Identifier: GPL-2.0 or MIT
  2. /* Copyright 2018 Marty E. Plummer <hanetzer@startmail.com> */
  3. /* Copyright 2019 Linaro, Ltd., Rob Herring <robh@kernel.org> */
  4. /* Copyright 2019 Collabora ltd. */
  5. #ifdef CONFIG_ARM_ARCH_TIMER
  6. #include <asm/arch_timer.h>
  7. #endif
  8. #include <linux/list.h>
  9. #include <linux/module.h>
  10. #include <linux/of_platform.h>
  11. #include <linux/pagemap.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/pm_runtime.h>
  14. #include <linux/time64.h>
  15. #include <drm/drm_auth.h>
  16. #include <drm/drm_debugfs.h>
  17. #include <drm/drm_drv.h>
  18. #include <drm/drm_exec.h>
  19. #include <drm/drm_ioctl.h>
  20. #include <drm/drm_print.h>
  21. #include <drm/drm_syncobj.h>
  22. #include <drm/drm_utils.h>
  23. #include <drm/gpu_scheduler.h>
  24. #include <drm/panthor_drm.h>
  25. #include "panthor_devfreq.h"
  26. #include "panthor_device.h"
  27. #include "panthor_fw.h"
  28. #include "panthor_gem.h"
  29. #include "panthor_gpu.h"
  30. #include "panthor_heap.h"
  31. #include "panthor_mmu.h"
  32. #include "panthor_regs.h"
  33. #include "panthor_sched.h"
  34. /**
  35. * DOC: user <-> kernel object copy helpers.
  36. */
  37. /**
  38. * panthor_set_uobj() - Copy kernel object to user object.
  39. * @usr_ptr: Users pointer.
  40. * @usr_size: Size of the user object.
  41. * @min_size: Minimum size for this object.
  42. * @kern_size: Size of the kernel object.
  43. * @in: Address of the kernel object to copy.
  44. *
  45. * Helper automating kernel -> user object copies.
  46. *
  47. * Don't use this function directly, use PANTHOR_UOBJ_SET() instead.
  48. *
  49. * Return: 0 on success, a negative error code otherwise.
  50. */
  51. static int
  52. panthor_set_uobj(u64 usr_ptr, u32 usr_size, u32 min_size, u32 kern_size, const void *in)
  53. {
  54. /* User size shouldn't be smaller than the minimal object size. */
  55. if (usr_size < min_size)
  56. return -EINVAL;
  57. if (copy_to_user(u64_to_user_ptr(usr_ptr), in, min_t(u32, usr_size, kern_size)))
  58. return -EFAULT;
  59. /* When the kernel object is smaller than the user object, we fill the gap with
  60. * zeros.
  61. */
  62. if (usr_size > kern_size &&
  63. clear_user(u64_to_user_ptr(usr_ptr + kern_size), usr_size - kern_size)) {
  64. return -EFAULT;
  65. }
  66. return 0;
  67. }
  68. /**
  69. * panthor_get_uobj_array() - Copy a user object array into a kernel accessible object array.
  70. * @in: The object array to copy.
  71. * @min_stride: Minimum array stride.
  72. * @obj_size: Kernel object size.
  73. *
  74. * Helper automating user -> kernel object copies.
  75. *
  76. * Don't use this function directly, use PANTHOR_UOBJ_GET_ARRAY() instead.
  77. *
  78. * Return: newly allocated object array or an ERR_PTR on error.
  79. */
  80. static void *
  81. panthor_get_uobj_array(const struct drm_panthor_obj_array *in, u32 min_stride,
  82. u32 obj_size)
  83. {
  84. int ret = 0;
  85. void *out_alloc;
  86. if (!in->count)
  87. return NULL;
  88. /* User stride must be at least the minimum object size, otherwise it might
  89. * lack useful information.
  90. */
  91. if (in->stride < min_stride)
  92. return ERR_PTR(-EINVAL);
  93. out_alloc = kvmalloc_array(in->count, obj_size, GFP_KERNEL);
  94. if (!out_alloc)
  95. return ERR_PTR(-ENOMEM);
  96. if (obj_size == in->stride) {
  97. /* Fast path when user/kernel have the same uAPI header version. */
  98. if (copy_from_user(out_alloc, u64_to_user_ptr(in->array),
  99. (unsigned long)obj_size * in->count))
  100. ret = -EFAULT;
  101. } else {
  102. void __user *in_ptr = u64_to_user_ptr(in->array);
  103. void *out_ptr = out_alloc;
  104. /* If the sizes differ, we need to copy elements one by one. */
  105. for (u32 i = 0; i < in->count; i++) {
  106. ret = copy_struct_from_user(out_ptr, obj_size, in_ptr, in->stride);
  107. if (ret)
  108. break;
  109. out_ptr += obj_size;
  110. in_ptr += in->stride;
  111. }
  112. }
  113. if (ret) {
  114. kvfree(out_alloc);
  115. return ERR_PTR(ret);
  116. }
  117. return out_alloc;
  118. }
  119. /**
  120. * PANTHOR_UOBJ_MIN_SIZE_INTERNAL() - Get the minimum user object size
  121. * @_typename: Object type.
  122. * @_last_mandatory_field: Last mandatory field.
  123. *
  124. * Get the minimum user object size based on the last mandatory field name,
  125. * A.K.A, the name of the last field of the structure at the time this
  126. * structure was added to the uAPI.
  127. *
  128. * Don't use directly, use PANTHOR_UOBJ_DECL() instead.
  129. */
  130. #define PANTHOR_UOBJ_MIN_SIZE_INTERNAL(_typename, _last_mandatory_field) \
  131. (offsetof(_typename, _last_mandatory_field) + \
  132. sizeof(((_typename *)NULL)->_last_mandatory_field))
  133. /**
  134. * PANTHOR_UOBJ_DECL() - Declare a new uAPI object whose subject to
  135. * evolutions.
  136. * @_typename: Object type.
  137. * @_last_mandatory_field: Last mandatory field.
  138. *
  139. * Should be used to extend the PANTHOR_UOBJ_MIN_SIZE() list.
  140. */
  141. #define PANTHOR_UOBJ_DECL(_typename, _last_mandatory_field) \
  142. _typename : PANTHOR_UOBJ_MIN_SIZE_INTERNAL(_typename, _last_mandatory_field)
  143. /**
  144. * PANTHOR_UOBJ_MIN_SIZE() - Get the minimum size of a given uAPI object
  145. * @_obj_name: Object to get the minimum size of.
  146. *
  147. * Don't use this macro directly, it's automatically called by
  148. * PANTHOR_UOBJ_{SET,GET_ARRAY}().
  149. */
  150. #define PANTHOR_UOBJ_MIN_SIZE(_obj_name) \
  151. _Generic(_obj_name, \
  152. PANTHOR_UOBJ_DECL(struct drm_panthor_gpu_info, tiler_present), \
  153. PANTHOR_UOBJ_DECL(struct drm_panthor_csif_info, pad), \
  154. PANTHOR_UOBJ_DECL(struct drm_panthor_timestamp_info, current_timestamp), \
  155. PANTHOR_UOBJ_DECL(struct drm_panthor_group_priorities_info, pad), \
  156. PANTHOR_UOBJ_DECL(struct drm_panthor_sync_op, timeline_value), \
  157. PANTHOR_UOBJ_DECL(struct drm_panthor_queue_submit, syncs), \
  158. PANTHOR_UOBJ_DECL(struct drm_panthor_queue_create, ringbuf_size), \
  159. PANTHOR_UOBJ_DECL(struct drm_panthor_vm_bind_op, syncs), \
  160. PANTHOR_UOBJ_DECL(struct drm_panthor_bo_sync_op, size))
  161. /**
  162. * PANTHOR_UOBJ_SET() - Copy a kernel object to a user object.
  163. * @_dest_usr_ptr: User pointer to copy to.
  164. * @_usr_size: Size of the user object.
  165. * @_src_obj: Kernel object to copy (not a pointer).
  166. *
  167. * Return: 0 on success, a negative error code otherwise.
  168. */
  169. #define PANTHOR_UOBJ_SET(_dest_usr_ptr, _usr_size, _src_obj) \
  170. panthor_set_uobj(_dest_usr_ptr, _usr_size, \
  171. PANTHOR_UOBJ_MIN_SIZE(_src_obj), \
  172. sizeof(_src_obj), &(_src_obj))
  173. /**
  174. * PANTHOR_UOBJ_GET_ARRAY() - Copy a user object array to a kernel accessible
  175. * object array.
  176. * @_dest_array: Local variable that will hold the newly allocated kernel
  177. * object array.
  178. * @_uobj_array: The drm_panthor_obj_array object describing the user object
  179. * array.
  180. *
  181. * Return: 0 on success, a negative error code otherwise.
  182. */
  183. #define PANTHOR_UOBJ_GET_ARRAY(_dest_array, _uobj_array) \
  184. ({ \
  185. typeof(_dest_array) _tmp; \
  186. _tmp = panthor_get_uobj_array(_uobj_array, \
  187. PANTHOR_UOBJ_MIN_SIZE((_dest_array)[0]), \
  188. sizeof((_dest_array)[0])); \
  189. if (!IS_ERR(_tmp)) \
  190. _dest_array = _tmp; \
  191. PTR_ERR_OR_ZERO(_tmp); \
  192. })
  193. /**
  194. * struct panthor_sync_signal - Represent a synchronization object point to attach
  195. * our job fence to.
  196. *
  197. * This structure is here to keep track of fences that are currently bound to
  198. * a specific syncobj point.
  199. *
  200. * At the beginning of a job submission, the fence
  201. * is retrieved from the syncobj itself, and can be NULL if no fence was attached
  202. * to this point.
  203. *
  204. * At the end, it points to the fence of the last job that had a
  205. * %DRM_PANTHOR_SYNC_OP_SIGNAL on this syncobj.
  206. *
  207. * With jobs being submitted in batches, the fence might change several times during
  208. * the process, allowing one job to wait on a job that's part of the same submission
  209. * but appears earlier in the drm_panthor_group_submit::queue_submits array.
  210. */
  211. struct panthor_sync_signal {
  212. /** @node: list_head to track signal ops within a submit operation */
  213. struct list_head node;
  214. /** @handle: The syncobj handle. */
  215. u32 handle;
  216. /**
  217. * @point: The syncobj point.
  218. *
  219. * Zero for regular syncobjs, and non-zero for timeline syncobjs.
  220. */
  221. u64 point;
  222. /**
  223. * @syncobj: The sync object pointed by @handle.
  224. */
  225. struct drm_syncobj *syncobj;
  226. /**
  227. * @chain: Chain object used to link the new fence to an existing
  228. * timeline syncobj.
  229. *
  230. * NULL for regular syncobj, non-NULL for timeline syncobjs.
  231. */
  232. struct dma_fence_chain *chain;
  233. /**
  234. * @fence: The fence to assign to the syncobj or syncobj-point.
  235. */
  236. struct dma_fence *fence;
  237. };
  238. /**
  239. * struct panthor_job_ctx - Job context
  240. */
  241. struct panthor_job_ctx {
  242. /** @job: The job that is about to be submitted to drm_sched. */
  243. struct drm_sched_job *job;
  244. /** @syncops: Array of sync operations. */
  245. struct drm_panthor_sync_op *syncops;
  246. /** @syncop_count: Number of sync operations. */
  247. u32 syncop_count;
  248. };
  249. /**
  250. * struct panthor_submit_ctx - Submission context
  251. *
  252. * Anything that's related to a submission (%DRM_IOCTL_PANTHOR_VM_BIND or
  253. * %DRM_IOCTL_PANTHOR_GROUP_SUBMIT) is kept here, so we can automate the
  254. * initialization and cleanup steps.
  255. */
  256. struct panthor_submit_ctx {
  257. /** @file: DRM file this submission happens on. */
  258. struct drm_file *file;
  259. /**
  260. * @signals: List of struct panthor_sync_signal.
  261. *
  262. * %DRM_PANTHOR_SYNC_OP_SIGNAL operations will be recorded here,
  263. * and %DRM_PANTHOR_SYNC_OP_WAIT will first check if an entry
  264. * matching the syncobj+point exists before calling
  265. * drm_syncobj_find_fence(). This allows us to describe dependencies
  266. * existing between jobs that are part of the same batch.
  267. */
  268. struct list_head signals;
  269. /** @jobs: Array of jobs. */
  270. struct panthor_job_ctx *jobs;
  271. /** @job_count: Number of entries in the @jobs array. */
  272. u32 job_count;
  273. /** @exec: drm_exec context used to acquire and prepare resv objects. */
  274. struct drm_exec exec;
  275. };
  276. #define PANTHOR_SYNC_OP_FLAGS_MASK \
  277. (DRM_PANTHOR_SYNC_OP_HANDLE_TYPE_MASK | DRM_PANTHOR_SYNC_OP_SIGNAL)
  278. static bool sync_op_is_signal(const struct drm_panthor_sync_op *sync_op)
  279. {
  280. return !!(sync_op->flags & DRM_PANTHOR_SYNC_OP_SIGNAL);
  281. }
  282. static bool sync_op_is_wait(const struct drm_panthor_sync_op *sync_op)
  283. {
  284. /* Note that DRM_PANTHOR_SYNC_OP_WAIT == 0 */
  285. return !(sync_op->flags & DRM_PANTHOR_SYNC_OP_SIGNAL);
  286. }
  287. /**
  288. * panthor_check_sync_op() - Check drm_panthor_sync_op fields
  289. * @sync_op: The sync operation to check.
  290. *
  291. * Return: 0 on success, -EINVAL otherwise.
  292. */
  293. static int
  294. panthor_check_sync_op(const struct drm_panthor_sync_op *sync_op)
  295. {
  296. u8 handle_type;
  297. if (sync_op->flags & ~PANTHOR_SYNC_OP_FLAGS_MASK)
  298. return -EINVAL;
  299. handle_type = sync_op->flags & DRM_PANTHOR_SYNC_OP_HANDLE_TYPE_MASK;
  300. if (handle_type != DRM_PANTHOR_SYNC_OP_HANDLE_TYPE_SYNCOBJ &&
  301. handle_type != DRM_PANTHOR_SYNC_OP_HANDLE_TYPE_TIMELINE_SYNCOBJ)
  302. return -EINVAL;
  303. if (handle_type == DRM_PANTHOR_SYNC_OP_HANDLE_TYPE_SYNCOBJ &&
  304. sync_op->timeline_value != 0)
  305. return -EINVAL;
  306. return 0;
  307. }
  308. /**
  309. * panthor_sync_signal_free() - Release resources and free a panthor_sync_signal object
  310. * @sig_sync: Signal object to free.
  311. */
  312. static void
  313. panthor_sync_signal_free(struct panthor_sync_signal *sig_sync)
  314. {
  315. if (!sig_sync)
  316. return;
  317. drm_syncobj_put(sig_sync->syncobj);
  318. dma_fence_chain_free(sig_sync->chain);
  319. dma_fence_put(sig_sync->fence);
  320. kfree(sig_sync);
  321. }
  322. /**
  323. * panthor_submit_ctx_add_sync_signal() - Add a signal operation to a submit context
  324. * @ctx: Context to add the signal operation to.
  325. * @handle: Syncobj handle.
  326. * @point: Syncobj point.
  327. *
  328. * Return: 0 on success, otherwise negative error value.
  329. */
  330. static int
  331. panthor_submit_ctx_add_sync_signal(struct panthor_submit_ctx *ctx, u32 handle, u64 point)
  332. {
  333. struct panthor_sync_signal *sig_sync;
  334. struct dma_fence *cur_fence;
  335. int ret;
  336. sig_sync = kzalloc_obj(*sig_sync);
  337. if (!sig_sync)
  338. return -ENOMEM;
  339. sig_sync->handle = handle;
  340. sig_sync->point = point;
  341. if (point > 0) {
  342. sig_sync->chain = dma_fence_chain_alloc();
  343. if (!sig_sync->chain) {
  344. ret = -ENOMEM;
  345. goto err_free_sig_sync;
  346. }
  347. }
  348. sig_sync->syncobj = drm_syncobj_find(ctx->file, handle);
  349. if (!sig_sync->syncobj) {
  350. ret = -EINVAL;
  351. goto err_free_sig_sync;
  352. }
  353. /* Retrieve the current fence attached to that point. It's
  354. * perfectly fine to get a NULL fence here, it just means there's
  355. * no fence attached to that point yet.
  356. */
  357. if (!drm_syncobj_find_fence(ctx->file, handle, point, 0, &cur_fence))
  358. sig_sync->fence = cur_fence;
  359. list_add_tail(&sig_sync->node, &ctx->signals);
  360. return 0;
  361. err_free_sig_sync:
  362. panthor_sync_signal_free(sig_sync);
  363. return ret;
  364. }
  365. /**
  366. * panthor_submit_ctx_search_sync_signal() - Search an existing signal operation in a
  367. * submit context.
  368. * @ctx: Context to search the signal operation in.
  369. * @handle: Syncobj handle.
  370. * @point: Syncobj point.
  371. *
  372. * Return: A valid panthor_sync_signal object if found, NULL otherwise.
  373. */
  374. static struct panthor_sync_signal *
  375. panthor_submit_ctx_search_sync_signal(struct panthor_submit_ctx *ctx, u32 handle, u64 point)
  376. {
  377. struct panthor_sync_signal *sig_sync;
  378. list_for_each_entry(sig_sync, &ctx->signals, node) {
  379. if (handle == sig_sync->handle && point == sig_sync->point)
  380. return sig_sync;
  381. }
  382. return NULL;
  383. }
  384. /**
  385. * panthor_submit_ctx_add_job() - Add a job to a submit context
  386. * @ctx: Context to search the signal operation in.
  387. * @idx: Index of the job in the context.
  388. * @job: Job to add.
  389. * @syncs: Sync operations provided by userspace.
  390. *
  391. * Return: 0 on success, a negative error code otherwise.
  392. */
  393. static int
  394. panthor_submit_ctx_add_job(struct panthor_submit_ctx *ctx, u32 idx,
  395. struct drm_sched_job *job,
  396. const struct drm_panthor_obj_array *syncs)
  397. {
  398. int ret;
  399. ctx->jobs[idx].job = job;
  400. ret = PANTHOR_UOBJ_GET_ARRAY(ctx->jobs[idx].syncops, syncs);
  401. if (ret)
  402. return ret;
  403. ctx->jobs[idx].syncop_count = syncs->count;
  404. return 0;
  405. }
  406. /**
  407. * panthor_submit_ctx_get_sync_signal() - Search signal operation and add one if none was found.
  408. * @ctx: Context to search the signal operation in.
  409. * @handle: Syncobj handle.
  410. * @point: Syncobj point.
  411. *
  412. * Return: 0 on success, a negative error code otherwise.
  413. */
  414. static int
  415. panthor_submit_ctx_get_sync_signal(struct panthor_submit_ctx *ctx, u32 handle, u64 point)
  416. {
  417. struct panthor_sync_signal *sig_sync;
  418. sig_sync = panthor_submit_ctx_search_sync_signal(ctx, handle, point);
  419. if (sig_sync)
  420. return 0;
  421. return panthor_submit_ctx_add_sync_signal(ctx, handle, point);
  422. }
  423. /**
  424. * panthor_submit_ctx_update_job_sync_signal_fences() - Update fences
  425. * on the signal operations specified by a job.
  426. * @ctx: Context to search the signal operation in.
  427. * @job_idx: Index of the job to operate on.
  428. *
  429. * Return: 0 on success, a negative error code otherwise.
  430. */
  431. static int
  432. panthor_submit_ctx_update_job_sync_signal_fences(struct panthor_submit_ctx *ctx,
  433. u32 job_idx)
  434. {
  435. struct panthor_device *ptdev = container_of(ctx->file->minor->dev,
  436. struct panthor_device,
  437. base);
  438. struct dma_fence *done_fence = &ctx->jobs[job_idx].job->s_fence->finished;
  439. const struct drm_panthor_sync_op *sync_ops = ctx->jobs[job_idx].syncops;
  440. u32 sync_op_count = ctx->jobs[job_idx].syncop_count;
  441. for (u32 i = 0; i < sync_op_count; i++) {
  442. struct dma_fence *old_fence;
  443. struct panthor_sync_signal *sig_sync;
  444. if (!sync_op_is_signal(&sync_ops[i]))
  445. continue;
  446. sig_sync = panthor_submit_ctx_search_sync_signal(ctx, sync_ops[i].handle,
  447. sync_ops[i].timeline_value);
  448. if (drm_WARN_ON(&ptdev->base, !sig_sync))
  449. return -EINVAL;
  450. old_fence = sig_sync->fence;
  451. sig_sync->fence = dma_fence_get(done_fence);
  452. dma_fence_put(old_fence);
  453. if (drm_WARN_ON(&ptdev->base, !sig_sync->fence))
  454. return -EINVAL;
  455. }
  456. return 0;
  457. }
  458. /**
  459. * panthor_submit_ctx_collect_job_signal_ops() - Iterate over all job signal operations
  460. * and add them to the context.
  461. * @ctx: Context to search the signal operation in.
  462. * @job_idx: Index of the job to operate on.
  463. *
  464. * Return: 0 on success, a negative error code otherwise.
  465. */
  466. static int
  467. panthor_submit_ctx_collect_job_signal_ops(struct panthor_submit_ctx *ctx,
  468. u32 job_idx)
  469. {
  470. const struct drm_panthor_sync_op *sync_ops = ctx->jobs[job_idx].syncops;
  471. u32 sync_op_count = ctx->jobs[job_idx].syncop_count;
  472. for (u32 i = 0; i < sync_op_count; i++) {
  473. int ret;
  474. if (!sync_op_is_signal(&sync_ops[i]))
  475. continue;
  476. ret = panthor_check_sync_op(&sync_ops[i]);
  477. if (ret)
  478. return ret;
  479. ret = panthor_submit_ctx_get_sync_signal(ctx,
  480. sync_ops[i].handle,
  481. sync_ops[i].timeline_value);
  482. if (ret)
  483. return ret;
  484. }
  485. return 0;
  486. }
  487. /**
  488. * panthor_submit_ctx_push_fences() - Iterate over the signal array, and for each entry, push
  489. * the currently assigned fence to the associated syncobj.
  490. * @ctx: Context to push fences on.
  491. *
  492. * This is the last step of a submission procedure, and is done once we know the submission
  493. * is effective and job fences are guaranteed to be signaled in finite time.
  494. */
  495. static void
  496. panthor_submit_ctx_push_fences(struct panthor_submit_ctx *ctx)
  497. {
  498. struct panthor_sync_signal *sig_sync;
  499. list_for_each_entry(sig_sync, &ctx->signals, node) {
  500. if (sig_sync->chain) {
  501. drm_syncobj_add_point(sig_sync->syncobj, sig_sync->chain,
  502. sig_sync->fence, sig_sync->point);
  503. sig_sync->chain = NULL;
  504. } else {
  505. drm_syncobj_replace_fence(sig_sync->syncobj, sig_sync->fence);
  506. }
  507. }
  508. }
  509. /**
  510. * panthor_submit_ctx_add_sync_deps_to_job() - Add sync wait operations as
  511. * job dependencies.
  512. * @ctx: Submit context.
  513. * @job_idx: Index of the job to operate on.
  514. *
  515. * Return: 0 on success, a negative error code otherwise.
  516. */
  517. static int
  518. panthor_submit_ctx_add_sync_deps_to_job(struct panthor_submit_ctx *ctx,
  519. u32 job_idx)
  520. {
  521. struct panthor_device *ptdev = container_of(ctx->file->minor->dev,
  522. struct panthor_device,
  523. base);
  524. const struct drm_panthor_sync_op *sync_ops = ctx->jobs[job_idx].syncops;
  525. struct drm_sched_job *job = ctx->jobs[job_idx].job;
  526. u32 sync_op_count = ctx->jobs[job_idx].syncop_count;
  527. int ret = 0;
  528. for (u32 i = 0; i < sync_op_count; i++) {
  529. struct panthor_sync_signal *sig_sync;
  530. struct dma_fence *fence;
  531. if (!sync_op_is_wait(&sync_ops[i]))
  532. continue;
  533. ret = panthor_check_sync_op(&sync_ops[i]);
  534. if (ret)
  535. return ret;
  536. sig_sync = panthor_submit_ctx_search_sync_signal(ctx, sync_ops[i].handle,
  537. sync_ops[i].timeline_value);
  538. if (sig_sync) {
  539. if (drm_WARN_ON(&ptdev->base, !sig_sync->fence))
  540. return -EINVAL;
  541. fence = dma_fence_get(sig_sync->fence);
  542. } else {
  543. ret = drm_syncobj_find_fence(ctx->file, sync_ops[i].handle,
  544. sync_ops[i].timeline_value,
  545. 0, &fence);
  546. if (ret)
  547. return ret;
  548. }
  549. ret = drm_sched_job_add_dependency(job, fence);
  550. if (ret)
  551. return ret;
  552. }
  553. return 0;
  554. }
  555. /**
  556. * panthor_submit_ctx_collect_jobs_signal_ops() - Collect all signal operations
  557. * and add them to the submit context.
  558. * @ctx: Submit context.
  559. *
  560. * Return: 0 on success, a negative error code otherwise.
  561. */
  562. static int
  563. panthor_submit_ctx_collect_jobs_signal_ops(struct panthor_submit_ctx *ctx)
  564. {
  565. for (u32 i = 0; i < ctx->job_count; i++) {
  566. int ret;
  567. ret = panthor_submit_ctx_collect_job_signal_ops(ctx, i);
  568. if (ret)
  569. return ret;
  570. }
  571. return 0;
  572. }
  573. /**
  574. * panthor_submit_ctx_add_deps_and_arm_jobs() - Add jobs dependencies and arm jobs
  575. * @ctx: Submit context.
  576. *
  577. * Must be called after the resv preparation has been taken care of.
  578. *
  579. * Return: 0 on success, a negative error code otherwise.
  580. */
  581. static int
  582. panthor_submit_ctx_add_deps_and_arm_jobs(struct panthor_submit_ctx *ctx)
  583. {
  584. for (u32 i = 0; i < ctx->job_count; i++) {
  585. int ret;
  586. ret = panthor_submit_ctx_add_sync_deps_to_job(ctx, i);
  587. if (ret)
  588. return ret;
  589. drm_sched_job_arm(ctx->jobs[i].job);
  590. ret = panthor_submit_ctx_update_job_sync_signal_fences(ctx, i);
  591. if (ret)
  592. return ret;
  593. }
  594. return 0;
  595. }
  596. /**
  597. * panthor_submit_ctx_push_jobs() - Push jobs to their scheduling entities.
  598. * @ctx: Submit context.
  599. * @upd_resvs: Callback used to update reservation objects that were previously
  600. * preapred.
  601. */
  602. static void
  603. panthor_submit_ctx_push_jobs(struct panthor_submit_ctx *ctx,
  604. void (*upd_resvs)(struct drm_exec *, struct drm_sched_job *))
  605. {
  606. for (u32 i = 0; i < ctx->job_count; i++) {
  607. upd_resvs(&ctx->exec, ctx->jobs[i].job);
  608. drm_sched_entity_push_job(ctx->jobs[i].job);
  609. /* Job is owned by the scheduler now. */
  610. ctx->jobs[i].job = NULL;
  611. }
  612. panthor_submit_ctx_push_fences(ctx);
  613. }
  614. /**
  615. * panthor_submit_ctx_init() - Initializes a submission context
  616. * @ctx: Submit context to initialize.
  617. * @file: drm_file this submission happens on.
  618. * @job_count: Number of jobs that will be submitted.
  619. *
  620. * Return: 0 on success, a negative error code otherwise.
  621. */
  622. static int panthor_submit_ctx_init(struct panthor_submit_ctx *ctx,
  623. struct drm_file *file, u32 job_count)
  624. {
  625. ctx->jobs = kvmalloc_objs(*ctx->jobs, job_count,
  626. GFP_KERNEL | __GFP_ZERO);
  627. if (!ctx->jobs)
  628. return -ENOMEM;
  629. ctx->file = file;
  630. ctx->job_count = job_count;
  631. INIT_LIST_HEAD(&ctx->signals);
  632. drm_exec_init(&ctx->exec,
  633. DRM_EXEC_INTERRUPTIBLE_WAIT | DRM_EXEC_IGNORE_DUPLICATES,
  634. 0);
  635. return 0;
  636. }
  637. /**
  638. * panthor_submit_ctx_cleanup() - Cleanup a submission context
  639. * @ctx: Submit context to cleanup.
  640. * @job_put: Job put callback.
  641. */
  642. static void panthor_submit_ctx_cleanup(struct panthor_submit_ctx *ctx,
  643. void (*job_put)(struct drm_sched_job *))
  644. {
  645. struct panthor_sync_signal *sig_sync, *tmp;
  646. unsigned long i;
  647. drm_exec_fini(&ctx->exec);
  648. list_for_each_entry_safe(sig_sync, tmp, &ctx->signals, node)
  649. panthor_sync_signal_free(sig_sync);
  650. for (i = 0; i < ctx->job_count; i++) {
  651. job_put(ctx->jobs[i].job);
  652. kvfree(ctx->jobs[i].syncops);
  653. }
  654. kvfree(ctx->jobs);
  655. }
  656. static int panthor_query_timestamp_info(struct panthor_device *ptdev,
  657. struct drm_panthor_timestamp_info *arg)
  658. {
  659. int ret;
  660. ret = panthor_device_resume_and_get(ptdev);
  661. if (ret)
  662. return ret;
  663. #ifdef CONFIG_ARM_ARCH_TIMER
  664. arg->timestamp_frequency = arch_timer_get_cntfrq();
  665. #else
  666. arg->timestamp_frequency = 0;
  667. #endif
  668. arg->current_timestamp = gpu_read64_counter(ptdev, GPU_TIMESTAMP);
  669. arg->timestamp_offset = gpu_read64(ptdev, GPU_TIMESTAMP_OFFSET);
  670. pm_runtime_put(ptdev->base.dev);
  671. return 0;
  672. }
  673. static int group_priority_permit(struct drm_file *file,
  674. u8 priority)
  675. {
  676. /* Ensure that priority is valid */
  677. if (priority > PANTHOR_GROUP_PRIORITY_REALTIME)
  678. return -EINVAL;
  679. /* Medium priority and below are always allowed */
  680. if (priority <= PANTHOR_GROUP_PRIORITY_MEDIUM)
  681. return 0;
  682. /* Higher priorities require CAP_SYS_NICE or DRM_MASTER */
  683. if (capable(CAP_SYS_NICE) || drm_is_current_master(file))
  684. return 0;
  685. return -EACCES;
  686. }
  687. static void panthor_query_group_priorities_info(struct drm_file *file,
  688. struct drm_panthor_group_priorities_info *arg)
  689. {
  690. int prio;
  691. memset(arg, 0, sizeof(*arg));
  692. for (prio = PANTHOR_GROUP_PRIORITY_REALTIME; prio >= 0; prio--) {
  693. if (!group_priority_permit(file, prio))
  694. arg->allowed_mask |= BIT(prio);
  695. }
  696. }
  697. static int panthor_ioctl_dev_query(struct drm_device *ddev, void *data, struct drm_file *file)
  698. {
  699. struct panthor_device *ptdev = container_of(ddev, struct panthor_device, base);
  700. struct drm_panthor_dev_query *args = data;
  701. struct drm_panthor_timestamp_info timestamp_info;
  702. struct drm_panthor_group_priorities_info priorities_info;
  703. int ret;
  704. if (!args->pointer) {
  705. switch (args->type) {
  706. case DRM_PANTHOR_DEV_QUERY_GPU_INFO:
  707. args->size = sizeof(ptdev->gpu_info);
  708. return 0;
  709. case DRM_PANTHOR_DEV_QUERY_CSIF_INFO:
  710. args->size = sizeof(ptdev->csif_info);
  711. return 0;
  712. case DRM_PANTHOR_DEV_QUERY_TIMESTAMP_INFO:
  713. args->size = sizeof(timestamp_info);
  714. return 0;
  715. case DRM_PANTHOR_DEV_QUERY_GROUP_PRIORITIES_INFO:
  716. args->size = sizeof(priorities_info);
  717. return 0;
  718. default:
  719. return -EINVAL;
  720. }
  721. }
  722. switch (args->type) {
  723. case DRM_PANTHOR_DEV_QUERY_GPU_INFO:
  724. return PANTHOR_UOBJ_SET(args->pointer, args->size, ptdev->gpu_info);
  725. case DRM_PANTHOR_DEV_QUERY_CSIF_INFO:
  726. return PANTHOR_UOBJ_SET(args->pointer, args->size, ptdev->csif_info);
  727. case DRM_PANTHOR_DEV_QUERY_TIMESTAMP_INFO:
  728. ret = panthor_query_timestamp_info(ptdev, &timestamp_info);
  729. if (ret)
  730. return ret;
  731. return PANTHOR_UOBJ_SET(args->pointer, args->size, timestamp_info);
  732. case DRM_PANTHOR_DEV_QUERY_GROUP_PRIORITIES_INFO:
  733. panthor_query_group_priorities_info(file, &priorities_info);
  734. return PANTHOR_UOBJ_SET(args->pointer, args->size, priorities_info);
  735. default:
  736. return -EINVAL;
  737. }
  738. }
  739. #define PANTHOR_VM_CREATE_FLAGS 0
  740. static int panthor_ioctl_vm_create(struct drm_device *ddev, void *data,
  741. struct drm_file *file)
  742. {
  743. struct panthor_device *ptdev = container_of(ddev, struct panthor_device, base);
  744. struct panthor_file *pfile = file->driver_priv;
  745. struct drm_panthor_vm_create *args = data;
  746. int cookie, ret;
  747. if (!drm_dev_enter(ddev, &cookie))
  748. return -ENODEV;
  749. ret = panthor_vm_pool_create_vm(ptdev, pfile->vms, args);
  750. if (ret >= 0) {
  751. args->id = ret;
  752. ret = 0;
  753. }
  754. drm_dev_exit(cookie);
  755. return ret;
  756. }
  757. static int panthor_ioctl_vm_destroy(struct drm_device *ddev, void *data,
  758. struct drm_file *file)
  759. {
  760. struct panthor_file *pfile = file->driver_priv;
  761. struct drm_panthor_vm_destroy *args = data;
  762. if (args->pad)
  763. return -EINVAL;
  764. return panthor_vm_pool_destroy_vm(pfile->vms, args->id);
  765. }
  766. #define PANTHOR_BO_FLAGS (DRM_PANTHOR_BO_NO_MMAP | \
  767. DRM_PANTHOR_BO_WB_MMAP)
  768. static int panthor_ioctl_bo_create(struct drm_device *ddev, void *data,
  769. struct drm_file *file)
  770. {
  771. struct panthor_file *pfile = file->driver_priv;
  772. struct drm_panthor_bo_create *args = data;
  773. struct panthor_vm *vm = NULL;
  774. int cookie, ret;
  775. if (!drm_dev_enter(ddev, &cookie))
  776. return -ENODEV;
  777. if (!args->size || args->pad ||
  778. (args->flags & ~PANTHOR_BO_FLAGS)) {
  779. ret = -EINVAL;
  780. goto out_dev_exit;
  781. }
  782. if ((args->flags & DRM_PANTHOR_BO_NO_MMAP) &&
  783. (args->flags & DRM_PANTHOR_BO_WB_MMAP)) {
  784. ret = -EINVAL;
  785. goto out_dev_exit;
  786. }
  787. if (args->exclusive_vm_id) {
  788. vm = panthor_vm_pool_get_vm(pfile->vms, args->exclusive_vm_id);
  789. if (!vm) {
  790. ret = -EINVAL;
  791. goto out_dev_exit;
  792. }
  793. }
  794. ret = panthor_gem_create_with_handle(file, ddev, vm, &args->size,
  795. args->flags, &args->handle);
  796. panthor_vm_put(vm);
  797. out_dev_exit:
  798. drm_dev_exit(cookie);
  799. return ret;
  800. }
  801. static int panthor_ioctl_bo_mmap_offset(struct drm_device *ddev, void *data,
  802. struct drm_file *file)
  803. {
  804. struct drm_panthor_bo_mmap_offset *args = data;
  805. struct panthor_gem_object *bo;
  806. struct drm_gem_object *obj;
  807. int ret;
  808. if (args->pad)
  809. return -EINVAL;
  810. obj = drm_gem_object_lookup(file, args->handle);
  811. if (!obj)
  812. return -ENOENT;
  813. bo = to_panthor_bo(obj);
  814. if (bo->flags & DRM_PANTHOR_BO_NO_MMAP) {
  815. ret = -EPERM;
  816. goto out;
  817. }
  818. ret = drm_gem_create_mmap_offset(obj);
  819. if (ret)
  820. goto out;
  821. args->offset = drm_vma_node_offset_addr(&obj->vma_node);
  822. out:
  823. drm_gem_object_put(obj);
  824. return ret;
  825. }
  826. static int panthor_ioctl_group_submit(struct drm_device *ddev, void *data,
  827. struct drm_file *file)
  828. {
  829. struct panthor_file *pfile = file->driver_priv;
  830. struct drm_panthor_group_submit *args = data;
  831. struct drm_panthor_queue_submit *jobs_args;
  832. struct panthor_submit_ctx ctx;
  833. int ret = 0, cookie;
  834. if (args->pad)
  835. return -EINVAL;
  836. if (!drm_dev_enter(ddev, &cookie))
  837. return -ENODEV;
  838. ret = PANTHOR_UOBJ_GET_ARRAY(jobs_args, &args->queue_submits);
  839. if (ret)
  840. goto out_dev_exit;
  841. ret = panthor_submit_ctx_init(&ctx, file, args->queue_submits.count);
  842. if (ret)
  843. goto out_free_jobs_args;
  844. /* Create jobs and attach sync operations */
  845. for (u32 i = 0; i < args->queue_submits.count; i++) {
  846. const struct drm_panthor_queue_submit *qsubmit = &jobs_args[i];
  847. struct drm_sched_job *job;
  848. job = panthor_job_create(pfile, args->group_handle, qsubmit,
  849. file->client_id);
  850. if (IS_ERR(job)) {
  851. ret = PTR_ERR(job);
  852. goto out_cleanup_submit_ctx;
  853. }
  854. ret = panthor_submit_ctx_add_job(&ctx, i, job, &qsubmit->syncs);
  855. if (ret)
  856. goto out_cleanup_submit_ctx;
  857. }
  858. /*
  859. * Collect signal operations on all jobs, such that each job can pick
  860. * from it for its dependencies and update the fence to signal when the
  861. * job is submitted.
  862. */
  863. ret = panthor_submit_ctx_collect_jobs_signal_ops(&ctx);
  864. if (ret)
  865. goto out_cleanup_submit_ctx;
  866. /*
  867. * We acquire/prepare revs on all jobs before proceeding with the
  868. * dependency registration.
  869. *
  870. * This is solving two problems:
  871. * 1. drm_sched_job_arm() and drm_sched_entity_push_job() must be
  872. * protected by a lock to make sure no concurrent access to the same
  873. * entity get interleaved, which would mess up with the fence seqno
  874. * ordering. Luckily, one of the resv being acquired is the VM resv,
  875. * and a scheduling entity is only bound to a single VM. As soon as
  876. * we acquire the VM resv, we should be safe.
  877. * 2. Jobs might depend on fences that were issued by previous jobs in
  878. * the same batch, so we can't add dependencies on all jobs before
  879. * arming previous jobs and registering the fence to the signal
  880. * array, otherwise we might miss dependencies, or point to an
  881. * outdated fence.
  882. */
  883. if (args->queue_submits.count > 0) {
  884. /* All jobs target the same group, so they also point to the same VM. */
  885. struct panthor_vm *vm = panthor_job_vm(ctx.jobs[0].job);
  886. drm_exec_until_all_locked(&ctx.exec) {
  887. ret = panthor_vm_prepare_mapped_bos_resvs(&ctx.exec, vm,
  888. args->queue_submits.count);
  889. }
  890. if (ret)
  891. goto out_cleanup_submit_ctx;
  892. }
  893. /*
  894. * Now that resvs are locked/prepared, we can iterate over each job to
  895. * add the dependencies, arm the job fence, register the job fence to
  896. * the signal array.
  897. */
  898. ret = panthor_submit_ctx_add_deps_and_arm_jobs(&ctx);
  899. if (ret)
  900. goto out_cleanup_submit_ctx;
  901. /* Nothing can fail after that point, so we can make our job fences
  902. * visible to the outside world. Push jobs and set the job fences to
  903. * the resv slots we reserved. This also pushes the fences to the
  904. * syncobjs that are part of the signal array.
  905. */
  906. panthor_submit_ctx_push_jobs(&ctx, panthor_job_update_resvs);
  907. out_cleanup_submit_ctx:
  908. panthor_submit_ctx_cleanup(&ctx, panthor_job_put);
  909. out_free_jobs_args:
  910. kvfree(jobs_args);
  911. out_dev_exit:
  912. drm_dev_exit(cookie);
  913. return ret;
  914. }
  915. static int panthor_ioctl_group_destroy(struct drm_device *ddev, void *data,
  916. struct drm_file *file)
  917. {
  918. struct panthor_file *pfile = file->driver_priv;
  919. struct drm_panthor_group_destroy *args = data;
  920. if (args->pad)
  921. return -EINVAL;
  922. return panthor_group_destroy(pfile, args->group_handle);
  923. }
  924. static int panthor_ioctl_group_create(struct drm_device *ddev, void *data,
  925. struct drm_file *file)
  926. {
  927. struct panthor_file *pfile = file->driver_priv;
  928. struct drm_panthor_group_create *args = data;
  929. struct drm_panthor_queue_create *queue_args;
  930. int ret;
  931. if (!args->queues.count || args->queues.count > MAX_CS_PER_CSG)
  932. return -EINVAL;
  933. ret = PANTHOR_UOBJ_GET_ARRAY(queue_args, &args->queues);
  934. if (ret)
  935. return ret;
  936. ret = group_priority_permit(file, args->priority);
  937. if (ret)
  938. goto out;
  939. ret = panthor_group_create(pfile, args, queue_args, file->client_id);
  940. if (ret < 0)
  941. goto out;
  942. args->group_handle = ret;
  943. ret = 0;
  944. out:
  945. kvfree(queue_args);
  946. return ret;
  947. }
  948. static int panthor_ioctl_group_get_state(struct drm_device *ddev, void *data,
  949. struct drm_file *file)
  950. {
  951. struct panthor_file *pfile = file->driver_priv;
  952. struct drm_panthor_group_get_state *args = data;
  953. return panthor_group_get_state(pfile, args);
  954. }
  955. static int panthor_ioctl_tiler_heap_create(struct drm_device *ddev, void *data,
  956. struct drm_file *file)
  957. {
  958. struct panthor_file *pfile = file->driver_priv;
  959. struct drm_panthor_tiler_heap_create *args = data;
  960. struct panthor_heap_pool *pool;
  961. struct panthor_vm *vm;
  962. int ret;
  963. vm = panthor_vm_pool_get_vm(pfile->vms, args->vm_id);
  964. if (!vm)
  965. return -EINVAL;
  966. pool = panthor_vm_get_heap_pool(vm, true);
  967. if (IS_ERR(pool)) {
  968. ret = PTR_ERR(pool);
  969. goto out_put_vm;
  970. }
  971. ret = panthor_heap_create(pool,
  972. args->initial_chunk_count,
  973. args->chunk_size,
  974. args->max_chunks,
  975. args->target_in_flight,
  976. &args->tiler_heap_ctx_gpu_va,
  977. &args->first_heap_chunk_gpu_va);
  978. if (ret < 0)
  979. goto out_put_heap_pool;
  980. /* Heap pools are per-VM. We combine the VM and HEAP id to make
  981. * a unique heap handle.
  982. */
  983. args->handle = (args->vm_id << 16) | ret;
  984. ret = 0;
  985. out_put_heap_pool:
  986. panthor_heap_pool_put(pool);
  987. out_put_vm:
  988. panthor_vm_put(vm);
  989. return ret;
  990. }
  991. static int panthor_ioctl_tiler_heap_destroy(struct drm_device *ddev, void *data,
  992. struct drm_file *file)
  993. {
  994. struct panthor_file *pfile = file->driver_priv;
  995. struct drm_panthor_tiler_heap_destroy *args = data;
  996. struct panthor_heap_pool *pool;
  997. struct panthor_vm *vm;
  998. int ret;
  999. if (args->pad)
  1000. return -EINVAL;
  1001. vm = panthor_vm_pool_get_vm(pfile->vms, args->handle >> 16);
  1002. if (!vm)
  1003. return -EINVAL;
  1004. pool = panthor_vm_get_heap_pool(vm, false);
  1005. if (IS_ERR(pool)) {
  1006. ret = PTR_ERR(pool);
  1007. goto out_put_vm;
  1008. }
  1009. ret = panthor_heap_destroy(pool, args->handle & GENMASK(15, 0));
  1010. panthor_heap_pool_put(pool);
  1011. out_put_vm:
  1012. panthor_vm_put(vm);
  1013. return ret;
  1014. }
  1015. static int panthor_ioctl_vm_bind_async(struct drm_device *ddev,
  1016. struct drm_panthor_vm_bind *args,
  1017. struct drm_file *file)
  1018. {
  1019. struct panthor_file *pfile = file->driver_priv;
  1020. struct drm_panthor_vm_bind_op *jobs_args;
  1021. struct panthor_submit_ctx ctx;
  1022. struct panthor_vm *vm;
  1023. int ret = 0;
  1024. vm = panthor_vm_pool_get_vm(pfile->vms, args->vm_id);
  1025. if (!vm)
  1026. return -EINVAL;
  1027. ret = PANTHOR_UOBJ_GET_ARRAY(jobs_args, &args->ops);
  1028. if (ret)
  1029. goto out_put_vm;
  1030. ret = panthor_submit_ctx_init(&ctx, file, args->ops.count);
  1031. if (ret)
  1032. goto out_free_jobs_args;
  1033. for (u32 i = 0; i < args->ops.count; i++) {
  1034. struct drm_panthor_vm_bind_op *op = &jobs_args[i];
  1035. struct drm_sched_job *job;
  1036. job = panthor_vm_bind_job_create(file, vm, op);
  1037. if (IS_ERR(job)) {
  1038. ret = PTR_ERR(job);
  1039. goto out_cleanup_submit_ctx;
  1040. }
  1041. ret = panthor_submit_ctx_add_job(&ctx, i, job, &op->syncs);
  1042. if (ret)
  1043. goto out_cleanup_submit_ctx;
  1044. }
  1045. ret = panthor_submit_ctx_collect_jobs_signal_ops(&ctx);
  1046. if (ret)
  1047. goto out_cleanup_submit_ctx;
  1048. /* Prepare reservation objects for each VM_BIND job. */
  1049. drm_exec_until_all_locked(&ctx.exec) {
  1050. for (u32 i = 0; i < ctx.job_count; i++) {
  1051. ret = panthor_vm_bind_job_prepare_resvs(&ctx.exec, ctx.jobs[i].job);
  1052. drm_exec_retry_on_contention(&ctx.exec);
  1053. if (ret)
  1054. goto out_cleanup_submit_ctx;
  1055. }
  1056. }
  1057. ret = panthor_submit_ctx_add_deps_and_arm_jobs(&ctx);
  1058. if (ret)
  1059. goto out_cleanup_submit_ctx;
  1060. /* Nothing can fail after that point. */
  1061. panthor_submit_ctx_push_jobs(&ctx, panthor_vm_bind_job_update_resvs);
  1062. out_cleanup_submit_ctx:
  1063. panthor_submit_ctx_cleanup(&ctx, panthor_vm_bind_job_put);
  1064. out_free_jobs_args:
  1065. kvfree(jobs_args);
  1066. out_put_vm:
  1067. panthor_vm_put(vm);
  1068. return ret;
  1069. }
  1070. static int panthor_ioctl_vm_bind_sync(struct drm_device *ddev,
  1071. struct drm_panthor_vm_bind *args,
  1072. struct drm_file *file)
  1073. {
  1074. struct panthor_file *pfile = file->driver_priv;
  1075. struct drm_panthor_vm_bind_op *jobs_args;
  1076. struct panthor_vm *vm;
  1077. int ret;
  1078. vm = panthor_vm_pool_get_vm(pfile->vms, args->vm_id);
  1079. if (!vm)
  1080. return -EINVAL;
  1081. ret = PANTHOR_UOBJ_GET_ARRAY(jobs_args, &args->ops);
  1082. if (ret)
  1083. goto out_put_vm;
  1084. for (u32 i = 0; i < args->ops.count; i++) {
  1085. ret = panthor_vm_bind_exec_sync_op(file, vm, &jobs_args[i]);
  1086. if (ret) {
  1087. /* Update ops.count so the user knows where things failed. */
  1088. args->ops.count = i;
  1089. break;
  1090. }
  1091. }
  1092. kvfree(jobs_args);
  1093. out_put_vm:
  1094. panthor_vm_put(vm);
  1095. return ret;
  1096. }
  1097. #define PANTHOR_VM_BIND_FLAGS DRM_PANTHOR_VM_BIND_ASYNC
  1098. static int panthor_ioctl_vm_bind(struct drm_device *ddev, void *data,
  1099. struct drm_file *file)
  1100. {
  1101. struct drm_panthor_vm_bind *args = data;
  1102. int cookie, ret;
  1103. if (!drm_dev_enter(ddev, &cookie))
  1104. return -ENODEV;
  1105. if (args->flags & DRM_PANTHOR_VM_BIND_ASYNC)
  1106. ret = panthor_ioctl_vm_bind_async(ddev, args, file);
  1107. else
  1108. ret = panthor_ioctl_vm_bind_sync(ddev, args, file);
  1109. drm_dev_exit(cookie);
  1110. return ret;
  1111. }
  1112. static int panthor_ioctl_vm_get_state(struct drm_device *ddev, void *data,
  1113. struct drm_file *file)
  1114. {
  1115. struct panthor_file *pfile = file->driver_priv;
  1116. struct drm_panthor_vm_get_state *args = data;
  1117. struct panthor_vm *vm;
  1118. vm = panthor_vm_pool_get_vm(pfile->vms, args->vm_id);
  1119. if (!vm)
  1120. return -EINVAL;
  1121. if (panthor_vm_is_unusable(vm))
  1122. args->state = DRM_PANTHOR_VM_STATE_UNUSABLE;
  1123. else
  1124. args->state = DRM_PANTHOR_VM_STATE_USABLE;
  1125. panthor_vm_put(vm);
  1126. return 0;
  1127. }
  1128. static int panthor_ioctl_bo_set_label(struct drm_device *ddev, void *data,
  1129. struct drm_file *file)
  1130. {
  1131. struct drm_panthor_bo_set_label *args = data;
  1132. struct drm_gem_object *obj;
  1133. const char *label = NULL;
  1134. int ret = 0;
  1135. if (args->pad)
  1136. return -EINVAL;
  1137. obj = drm_gem_object_lookup(file, args->handle);
  1138. if (!obj)
  1139. return -ENOENT;
  1140. if (args->label) {
  1141. label = strndup_user((const char __user *)(uintptr_t)args->label,
  1142. PANTHOR_BO_LABEL_MAXLEN);
  1143. if (IS_ERR(label)) {
  1144. ret = PTR_ERR(label);
  1145. if (ret == -EINVAL)
  1146. ret = -E2BIG;
  1147. goto err_put_obj;
  1148. }
  1149. }
  1150. /*
  1151. * We treat passing a label of length 0 and passing a NULL label
  1152. * differently, because even though they might seem conceptually
  1153. * similar, future uses of the BO label might expect a different
  1154. * behaviour in each case.
  1155. */
  1156. panthor_gem_bo_set_label(obj, label);
  1157. err_put_obj:
  1158. drm_gem_object_put(obj);
  1159. return ret;
  1160. }
  1161. static int panthor_ioctl_set_user_mmio_offset(struct drm_device *ddev,
  1162. void *data, struct drm_file *file)
  1163. {
  1164. struct drm_panthor_set_user_mmio_offset *args = data;
  1165. struct panthor_file *pfile = file->driver_priv;
  1166. if (args->offset != DRM_PANTHOR_USER_MMIO_OFFSET_32BIT &&
  1167. args->offset != DRM_PANTHOR_USER_MMIO_OFFSET_64BIT)
  1168. return -EINVAL;
  1169. WRITE_ONCE(pfile->user_mmio.offset, args->offset);
  1170. return 0;
  1171. }
  1172. static int panthor_ioctl_bo_sync(struct drm_device *ddev, void *data,
  1173. struct drm_file *file)
  1174. {
  1175. struct drm_panthor_bo_sync *args = data;
  1176. struct drm_panthor_bo_sync_op *ops;
  1177. struct drm_gem_object *obj;
  1178. int ret;
  1179. if (!args->ops.count)
  1180. return 0;
  1181. ret = PANTHOR_UOBJ_GET_ARRAY(ops, &args->ops);
  1182. if (ret)
  1183. return ret;
  1184. for (u32 i = 0; i < args->ops.count; i++) {
  1185. obj = drm_gem_object_lookup(file, ops[i].handle);
  1186. if (!obj) {
  1187. ret = -ENOENT;
  1188. goto err_ops;
  1189. }
  1190. ret = panthor_gem_sync(obj, ops[i].type, ops[i].offset,
  1191. ops[i].size);
  1192. drm_gem_object_put(obj);
  1193. if (ret)
  1194. goto err_ops;
  1195. }
  1196. err_ops:
  1197. kvfree(ops);
  1198. return ret;
  1199. }
  1200. static int panthor_ioctl_bo_query_info(struct drm_device *ddev, void *data,
  1201. struct drm_file *file)
  1202. {
  1203. struct drm_panthor_bo_query_info *args = data;
  1204. struct panthor_gem_object *bo;
  1205. struct drm_gem_object *obj;
  1206. obj = drm_gem_object_lookup(file, args->handle);
  1207. if (!obj)
  1208. return -ENOENT;
  1209. bo = to_panthor_bo(obj);
  1210. args->pad = 0;
  1211. args->create_flags = bo->flags;
  1212. args->extra_flags = 0;
  1213. if (drm_gem_is_imported(&bo->base.base))
  1214. args->extra_flags |= DRM_PANTHOR_BO_IS_IMPORTED;
  1215. drm_gem_object_put(obj);
  1216. return 0;
  1217. }
  1218. static int
  1219. panthor_open(struct drm_device *ddev, struct drm_file *file)
  1220. {
  1221. struct panthor_device *ptdev = container_of(ddev, struct panthor_device, base);
  1222. struct panthor_file *pfile;
  1223. int ret;
  1224. pfile = kzalloc_obj(*pfile);
  1225. if (!pfile)
  1226. return -ENOMEM;
  1227. pfile->ptdev = ptdev;
  1228. pfile->user_mmio.offset = DRM_PANTHOR_USER_MMIO_OFFSET;
  1229. #ifdef CONFIG_ARM64
  1230. /*
  1231. * With 32-bit systems being limited by the 32-bit representation of
  1232. * mmap2's pgoffset field, we need to make the MMIO offset arch
  1233. * specific.
  1234. */
  1235. if (test_tsk_thread_flag(current, TIF_32BIT))
  1236. pfile->user_mmio.offset = DRM_PANTHOR_USER_MMIO_OFFSET_32BIT;
  1237. #endif
  1238. ret = panthor_vm_pool_create(pfile);
  1239. if (ret)
  1240. goto err_free_file;
  1241. ret = panthor_group_pool_create(pfile);
  1242. if (ret)
  1243. goto err_destroy_vm_pool;
  1244. file->driver_priv = pfile;
  1245. return 0;
  1246. err_destroy_vm_pool:
  1247. panthor_vm_pool_destroy(pfile);
  1248. err_free_file:
  1249. kfree(pfile);
  1250. return ret;
  1251. }
  1252. static void
  1253. panthor_postclose(struct drm_device *ddev, struct drm_file *file)
  1254. {
  1255. struct panthor_file *pfile = file->driver_priv;
  1256. panthor_group_pool_destroy(pfile);
  1257. panthor_vm_pool_destroy(pfile);
  1258. kfree(pfile);
  1259. }
  1260. static const struct drm_ioctl_desc panthor_drm_driver_ioctls[] = {
  1261. #define PANTHOR_IOCTL(n, func, flags) \
  1262. DRM_IOCTL_DEF_DRV(PANTHOR_##n, panthor_ioctl_##func, flags)
  1263. PANTHOR_IOCTL(DEV_QUERY, dev_query, DRM_RENDER_ALLOW),
  1264. PANTHOR_IOCTL(VM_CREATE, vm_create, DRM_RENDER_ALLOW),
  1265. PANTHOR_IOCTL(VM_DESTROY, vm_destroy, DRM_RENDER_ALLOW),
  1266. PANTHOR_IOCTL(VM_BIND, vm_bind, DRM_RENDER_ALLOW),
  1267. PANTHOR_IOCTL(VM_GET_STATE, vm_get_state, DRM_RENDER_ALLOW),
  1268. PANTHOR_IOCTL(BO_CREATE, bo_create, DRM_RENDER_ALLOW),
  1269. PANTHOR_IOCTL(BO_MMAP_OFFSET, bo_mmap_offset, DRM_RENDER_ALLOW),
  1270. PANTHOR_IOCTL(GROUP_CREATE, group_create, DRM_RENDER_ALLOW),
  1271. PANTHOR_IOCTL(GROUP_DESTROY, group_destroy, DRM_RENDER_ALLOW),
  1272. PANTHOR_IOCTL(GROUP_GET_STATE, group_get_state, DRM_RENDER_ALLOW),
  1273. PANTHOR_IOCTL(TILER_HEAP_CREATE, tiler_heap_create, DRM_RENDER_ALLOW),
  1274. PANTHOR_IOCTL(TILER_HEAP_DESTROY, tiler_heap_destroy, DRM_RENDER_ALLOW),
  1275. PANTHOR_IOCTL(GROUP_SUBMIT, group_submit, DRM_RENDER_ALLOW),
  1276. PANTHOR_IOCTL(BO_SET_LABEL, bo_set_label, DRM_RENDER_ALLOW),
  1277. PANTHOR_IOCTL(SET_USER_MMIO_OFFSET, set_user_mmio_offset, DRM_RENDER_ALLOW),
  1278. PANTHOR_IOCTL(BO_SYNC, bo_sync, DRM_RENDER_ALLOW),
  1279. PANTHOR_IOCTL(BO_QUERY_INFO, bo_query_info, DRM_RENDER_ALLOW),
  1280. };
  1281. static int panthor_mmap(struct file *filp, struct vm_area_struct *vma)
  1282. {
  1283. struct drm_file *file = filp->private_data;
  1284. struct panthor_file *pfile = file->driver_priv;
  1285. struct panthor_device *ptdev = pfile->ptdev;
  1286. u64 offset = (u64)vma->vm_pgoff << PAGE_SHIFT;
  1287. u64 user_mmio_offset;
  1288. int ret, cookie;
  1289. if (!drm_dev_enter(file->minor->dev, &cookie))
  1290. return -ENODEV;
  1291. /* Adjust the user MMIO offset to match the offset used kernel side.
  1292. * We use a local variable with a READ_ONCE() here to make sure
  1293. * the user_mmio_offset we use for the is_user_mmio_mapping() check
  1294. * hasn't changed when we do the offset adjustment.
  1295. */
  1296. user_mmio_offset = READ_ONCE(pfile->user_mmio.offset);
  1297. if (offset >= user_mmio_offset) {
  1298. offset -= user_mmio_offset;
  1299. offset += DRM_PANTHOR_USER_MMIO_OFFSET;
  1300. vma->vm_pgoff = offset >> PAGE_SHIFT;
  1301. ret = panthor_device_mmap_io(ptdev, vma);
  1302. } else {
  1303. ret = drm_gem_mmap(filp, vma);
  1304. }
  1305. drm_dev_exit(cookie);
  1306. return ret;
  1307. }
  1308. static void panthor_gpu_show_fdinfo(struct panthor_device *ptdev,
  1309. struct panthor_file *pfile,
  1310. struct drm_printer *p)
  1311. {
  1312. if (ptdev->profile_mask & PANTHOR_DEVICE_PROFILING_ALL)
  1313. panthor_fdinfo_gather_group_samples(pfile);
  1314. if (ptdev->profile_mask & PANTHOR_DEVICE_PROFILING_TIMESTAMP) {
  1315. #ifdef CONFIG_ARM_ARCH_TIMER
  1316. drm_printf(p, "drm-engine-panthor:\t%llu ns\n",
  1317. DIV_ROUND_UP_ULL((pfile->stats.time * NSEC_PER_SEC),
  1318. arch_timer_get_cntfrq()));
  1319. #endif
  1320. }
  1321. if (ptdev->profile_mask & PANTHOR_DEVICE_PROFILING_CYCLES)
  1322. drm_printf(p, "drm-cycles-panthor:\t%llu\n", pfile->stats.cycles);
  1323. drm_printf(p, "drm-maxfreq-panthor:\t%lu Hz\n", ptdev->fast_rate);
  1324. drm_printf(p, "drm-curfreq-panthor:\t%lu Hz\n",
  1325. panthor_devfreq_get_freq(ptdev));
  1326. }
  1327. static void panthor_show_internal_memory_stats(struct drm_printer *p, struct drm_file *file)
  1328. {
  1329. char *drv_name = file->minor->dev->driver->name;
  1330. struct panthor_file *pfile = file->driver_priv;
  1331. struct drm_memory_stats stats = {0};
  1332. panthor_fdinfo_gather_group_mem_info(pfile, &stats);
  1333. panthor_vm_heaps_sizes(pfile, &stats);
  1334. drm_fdinfo_print_size(p, drv_name, "resident", "memory", stats.resident);
  1335. drm_fdinfo_print_size(p, drv_name, "active", "memory", stats.active);
  1336. }
  1337. static void panthor_show_fdinfo(struct drm_printer *p, struct drm_file *file)
  1338. {
  1339. struct drm_device *dev = file->minor->dev;
  1340. struct panthor_device *ptdev = container_of(dev, struct panthor_device, base);
  1341. panthor_gpu_show_fdinfo(ptdev, file->driver_priv, p);
  1342. panthor_show_internal_memory_stats(p, file);
  1343. drm_show_memory_stats(p, file);
  1344. }
  1345. static const struct file_operations panthor_drm_driver_fops = {
  1346. .owner = THIS_MODULE,
  1347. .open = drm_open,
  1348. .release = drm_release,
  1349. .unlocked_ioctl = drm_ioctl,
  1350. .compat_ioctl = drm_compat_ioctl,
  1351. .poll = drm_poll,
  1352. .read = drm_read,
  1353. .llseek = noop_llseek,
  1354. .mmap = panthor_mmap,
  1355. .get_unmapped_area = drm_gem_get_unmapped_area,
  1356. .show_fdinfo = drm_show_fdinfo,
  1357. .fop_flags = FOP_UNSIGNED_OFFSET,
  1358. };
  1359. #ifdef CONFIG_DEBUG_FS
  1360. static int panthor_gems_show(struct seq_file *m, void *data)
  1361. {
  1362. struct drm_info_node *node = m->private;
  1363. struct drm_device *dev = node->minor->dev;
  1364. struct panthor_device *ptdev = container_of(dev, struct panthor_device, base);
  1365. panthor_gem_debugfs_print_bos(ptdev, m);
  1366. return 0;
  1367. }
  1368. static struct drm_info_list panthor_debugfs_list[] = {
  1369. {"gems", panthor_gems_show, 0, NULL},
  1370. };
  1371. static int panthor_gems_debugfs_init(struct drm_minor *minor)
  1372. {
  1373. drm_debugfs_create_files(panthor_debugfs_list,
  1374. ARRAY_SIZE(panthor_debugfs_list),
  1375. minor->debugfs_root, minor);
  1376. return 0;
  1377. }
  1378. static void panthor_debugfs_init(struct drm_minor *minor)
  1379. {
  1380. panthor_mmu_debugfs_init(minor);
  1381. panthor_gems_debugfs_init(minor);
  1382. }
  1383. #endif
  1384. /*
  1385. * PanCSF driver version:
  1386. * - 1.0 - initial interface
  1387. * - 1.1 - adds DEV_QUERY_TIMESTAMP_INFO query
  1388. * - 1.2 - adds DEV_QUERY_GROUP_PRIORITIES_INFO query
  1389. * - adds PANTHOR_GROUP_PRIORITY_REALTIME priority
  1390. * - 1.3 - adds DRM_PANTHOR_GROUP_STATE_INNOCENT flag
  1391. * - 1.4 - adds DRM_IOCTL_PANTHOR_BO_SET_LABEL ioctl
  1392. * - 1.5 - adds DRM_PANTHOR_SET_USER_MMIO_OFFSET ioctl
  1393. * - 1.6 - enables GLB_COUNTER_EN
  1394. * - 1.7 - adds DRM_PANTHOR_BO_WB_MMAP flag
  1395. * - adds DRM_IOCTL_PANTHOR_BO_SYNC ioctl
  1396. * - adds DRM_IOCTL_PANTHOR_BO_QUERY_INFO ioctl
  1397. * - adds drm_panthor_gpu_info::selected_coherency
  1398. */
  1399. static const struct drm_driver panthor_drm_driver = {
  1400. .driver_features = DRIVER_RENDER | DRIVER_GEM | DRIVER_SYNCOBJ |
  1401. DRIVER_SYNCOBJ_TIMELINE | DRIVER_GEM_GPUVA,
  1402. .open = panthor_open,
  1403. .postclose = panthor_postclose,
  1404. .show_fdinfo = panthor_show_fdinfo,
  1405. .ioctls = panthor_drm_driver_ioctls,
  1406. .num_ioctls = ARRAY_SIZE(panthor_drm_driver_ioctls),
  1407. .fops = &panthor_drm_driver_fops,
  1408. .name = "panthor",
  1409. .desc = "Panthor DRM driver",
  1410. .major = 1,
  1411. .minor = 7,
  1412. .gem_create_object = panthor_gem_create_object,
  1413. .gem_prime_import_sg_table = drm_gem_shmem_prime_import_sg_table,
  1414. .gem_prime_import = panthor_gem_prime_import,
  1415. #ifdef CONFIG_DEBUG_FS
  1416. .debugfs_init = panthor_debugfs_init,
  1417. #endif
  1418. };
  1419. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  1420. bool panthor_transparent_hugepage = true;
  1421. module_param_named(transparent_hugepage, panthor_transparent_hugepage, bool, 0400);
  1422. MODULE_PARM_DESC(transparent_hugepage, "Use a dedicated tmpfs mount point with Transparent Hugepage enabled (true = default)");
  1423. #endif
  1424. static int panthor_probe(struct platform_device *pdev)
  1425. {
  1426. struct panthor_device *ptdev;
  1427. ptdev = devm_drm_dev_alloc(&pdev->dev, &panthor_drm_driver,
  1428. struct panthor_device, base);
  1429. if (IS_ERR(ptdev))
  1430. return -ENOMEM;
  1431. platform_set_drvdata(pdev, ptdev);
  1432. return panthor_device_init(ptdev);
  1433. }
  1434. static void panthor_remove(struct platform_device *pdev)
  1435. {
  1436. struct panthor_device *ptdev = platform_get_drvdata(pdev);
  1437. panthor_device_unplug(ptdev);
  1438. }
  1439. static ssize_t profiling_show(struct device *dev,
  1440. struct device_attribute *attr,
  1441. char *buf)
  1442. {
  1443. struct panthor_device *ptdev = dev_get_drvdata(dev);
  1444. return sysfs_emit(buf, "%d\n", ptdev->profile_mask);
  1445. }
  1446. static ssize_t profiling_store(struct device *dev,
  1447. struct device_attribute *attr,
  1448. const char *buf, size_t len)
  1449. {
  1450. struct panthor_device *ptdev = dev_get_drvdata(dev);
  1451. u32 value;
  1452. int err;
  1453. err = kstrtou32(buf, 0, &value);
  1454. if (err)
  1455. return err;
  1456. if ((value & ~PANTHOR_DEVICE_PROFILING_ALL) != 0)
  1457. return -EINVAL;
  1458. ptdev->profile_mask = value;
  1459. return len;
  1460. }
  1461. static DEVICE_ATTR_RW(profiling);
  1462. static struct attribute *panthor_attrs[] = {
  1463. &dev_attr_profiling.attr,
  1464. NULL,
  1465. };
  1466. ATTRIBUTE_GROUPS(panthor);
  1467. static const struct panthor_soc_data soc_data_mediatek_mt8196 = {
  1468. .asn_hash_enable = true,
  1469. .asn_hash = { 0xb, 0xe, 0x0, },
  1470. };
  1471. static const struct of_device_id dt_match[] = {
  1472. { .compatible = "mediatek,mt8196-mali", .data = &soc_data_mediatek_mt8196, },
  1473. { .compatible = "rockchip,rk3588-mali" },
  1474. { .compatible = "arm,mali-valhall-csf" },
  1475. {}
  1476. };
  1477. MODULE_DEVICE_TABLE(of, dt_match);
  1478. static DEFINE_RUNTIME_DEV_PM_OPS(panthor_pm_ops,
  1479. panthor_device_suspend,
  1480. panthor_device_resume,
  1481. NULL);
  1482. static struct platform_driver panthor_driver = {
  1483. .probe = panthor_probe,
  1484. .remove = panthor_remove,
  1485. .driver = {
  1486. .name = "panthor",
  1487. .pm = pm_ptr(&panthor_pm_ops),
  1488. .of_match_table = dt_match,
  1489. .dev_groups = panthor_groups,
  1490. },
  1491. };
  1492. /*
  1493. * Workqueue used to cleanup stuff.
  1494. *
  1495. * We create a dedicated workqueue so we can drain on unplug and
  1496. * make sure all resources are freed before the module is unloaded.
  1497. */
  1498. struct workqueue_struct *panthor_cleanup_wq;
  1499. static int __init panthor_init(void)
  1500. {
  1501. int ret;
  1502. ret = panthor_mmu_pt_cache_init();
  1503. if (ret)
  1504. return ret;
  1505. panthor_cleanup_wq = alloc_workqueue("panthor-cleanup", WQ_UNBOUND, 0);
  1506. if (!panthor_cleanup_wq) {
  1507. pr_err("panthor: Failed to allocate the workqueues");
  1508. ret = -ENOMEM;
  1509. goto err_mmu_pt_cache_fini;
  1510. }
  1511. ret = platform_driver_register(&panthor_driver);
  1512. if (ret)
  1513. goto err_destroy_cleanup_wq;
  1514. return 0;
  1515. err_destroy_cleanup_wq:
  1516. destroy_workqueue(panthor_cleanup_wq);
  1517. err_mmu_pt_cache_fini:
  1518. panthor_mmu_pt_cache_fini();
  1519. return ret;
  1520. }
  1521. module_init(panthor_init);
  1522. static void __exit panthor_exit(void)
  1523. {
  1524. platform_driver_unregister(&panthor_driver);
  1525. destroy_workqueue(panthor_cleanup_wq);
  1526. panthor_mmu_pt_cache_fini();
  1527. }
  1528. module_exit(panthor_exit);
  1529. MODULE_AUTHOR("Panthor Project Developers");
  1530. MODULE_DESCRIPTION("Panthor DRM Driver");
  1531. MODULE_LICENSE("Dual MIT/GPL");