panthor_drv.c 39 KB

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