pvr_drv.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501
  1. // SPDX-License-Identifier: GPL-2.0-only OR MIT
  2. /* Copyright (c) 2023 Imagination Technologies Ltd. */
  3. #include "pvr_context.h"
  4. #include "pvr_debugfs.h"
  5. #include "pvr_device.h"
  6. #include "pvr_drv.h"
  7. #include "pvr_free_list.h"
  8. #include "pvr_gem.h"
  9. #include "pvr_hwrt.h"
  10. #include "pvr_job.h"
  11. #include "pvr_mmu.h"
  12. #include "pvr_power.h"
  13. #include "pvr_rogue_defs.h"
  14. #include "pvr_rogue_fwif_client.h"
  15. #include "pvr_rogue_fwif_shared.h"
  16. #include "pvr_vm.h"
  17. #include <uapi/drm/pvr_drm.h>
  18. #include <drm/drm_device.h>
  19. #include <drm/drm_drv.h>
  20. #include <drm/drm_file.h>
  21. #include <drm/drm_gem.h>
  22. #include <drm/drm_ioctl.h>
  23. #include <linux/err.h>
  24. #include <linux/export.h>
  25. #include <linux/fs.h>
  26. #include <linux/kernel.h>
  27. #include <linux/list.h>
  28. #include <linux/mod_devicetable.h>
  29. #include <linux/module.h>
  30. #include <linux/moduleparam.h>
  31. #include <linux/of_device.h>
  32. #include <linux/of_platform.h>
  33. #include <linux/platform_device.h>
  34. #include <linux/pm_runtime.h>
  35. #include <linux/xarray.h>
  36. /**
  37. * DOC: PowerVR (Series 6 and later) and IMG Graphics Driver
  38. *
  39. * This driver supports the following PowerVR/IMG graphics cores from Imagination Technologies:
  40. *
  41. * * AXE-1-16M (found in Texas Instruments AM62)
  42. */
  43. /**
  44. * pvr_ioctl_create_bo() - IOCTL to create a GEM buffer object.
  45. * @drm_dev: [IN] Target DRM device.
  46. * @raw_args: [IN/OUT] Arguments passed to this IOCTL. This must be of type
  47. * &struct drm_pvr_ioctl_create_bo_args.
  48. * @file: [IN] DRM file-private data.
  49. *
  50. * Called from userspace with %DRM_IOCTL_PVR_CREATE_BO.
  51. *
  52. * Return:
  53. * * 0 on success,
  54. * * -%EINVAL if the value of &drm_pvr_ioctl_create_bo_args.size is zero
  55. * or wider than &typedef size_t,
  56. * * -%EINVAL if any bits in &drm_pvr_ioctl_create_bo_args.flags that are
  57. * reserved or undefined are set,
  58. * * -%EINVAL if any padding fields in &drm_pvr_ioctl_create_bo_args are not
  59. * zero,
  60. * * Any error encountered while creating the object (see
  61. * pvr_gem_object_create()), or
  62. * * Any error encountered while transferring ownership of the object into a
  63. * userspace-accessible handle (see pvr_gem_object_into_handle()).
  64. */
  65. static int
  66. pvr_ioctl_create_bo(struct drm_device *drm_dev, void *raw_args,
  67. struct drm_file *file)
  68. {
  69. struct drm_pvr_ioctl_create_bo_args *args = raw_args;
  70. struct pvr_device *pvr_dev = to_pvr_device(drm_dev);
  71. struct pvr_file *pvr_file = to_pvr_file(file);
  72. struct pvr_gem_object *pvr_obj;
  73. size_t sanitized_size;
  74. int idx;
  75. int err;
  76. if (!drm_dev_enter(drm_dev, &idx))
  77. return -EIO;
  78. /* All padding fields must be zeroed. */
  79. if (args->_padding_c != 0) {
  80. err = -EINVAL;
  81. goto err_drm_dev_exit;
  82. }
  83. /*
  84. * On 64-bit platforms (our primary target), size_t is a u64. However,
  85. * on other architectures we have to check for overflow when casting
  86. * down to size_t from u64.
  87. *
  88. * We also disallow zero-sized allocations, and reserved (kernel-only)
  89. * flags.
  90. */
  91. if (args->size > SIZE_MAX || args->size == 0 || args->flags &
  92. ~DRM_PVR_BO_FLAGS_MASK || args->size & (PVR_DEVICE_PAGE_SIZE - 1)) {
  93. err = -EINVAL;
  94. goto err_drm_dev_exit;
  95. }
  96. sanitized_size = (size_t)args->size;
  97. /*
  98. * Create a buffer object and transfer ownership to a userspace-
  99. * accessible handle.
  100. */
  101. pvr_obj = pvr_gem_object_create(pvr_dev, sanitized_size, args->flags);
  102. if (IS_ERR(pvr_obj)) {
  103. err = PTR_ERR(pvr_obj);
  104. goto err_drm_dev_exit;
  105. }
  106. /* This function will not modify &args->handle unless it succeeds. */
  107. err = pvr_gem_object_into_handle(pvr_obj, pvr_file, &args->handle);
  108. if (err)
  109. goto err_destroy_obj;
  110. drm_dev_exit(idx);
  111. return 0;
  112. err_destroy_obj:
  113. /*
  114. * GEM objects are refcounted, so there is no explicit destructor
  115. * function. Instead, we release the singular reference we currently
  116. * hold on the object and let GEM take care of the rest.
  117. */
  118. pvr_gem_object_put(pvr_obj);
  119. err_drm_dev_exit:
  120. drm_dev_exit(idx);
  121. return err;
  122. }
  123. /**
  124. * pvr_ioctl_get_bo_mmap_offset() - IOCTL to generate a "fake" offset to be
  125. * used when calling mmap() from userspace to map the given GEM buffer object
  126. * @drm_dev: [IN] DRM device (unused).
  127. * @raw_args: [IN/OUT] Arguments passed to this IOCTL. This must be of type
  128. * &struct drm_pvr_ioctl_get_bo_mmap_offset_args.
  129. * @file: [IN] DRM file private data.
  130. *
  131. * Called from userspace with %DRM_IOCTL_PVR_GET_BO_MMAP_OFFSET.
  132. *
  133. * This IOCTL does *not* perform an mmap. See the docs on
  134. * &struct drm_pvr_ioctl_get_bo_mmap_offset_args for details.
  135. *
  136. * Return:
  137. * * 0 on success,
  138. * * -%ENOENT if the handle does not reference a valid GEM buffer object,
  139. * * -%EINVAL if any padding fields in &struct
  140. * drm_pvr_ioctl_get_bo_mmap_offset_args are not zero, or
  141. * * Any error returned by drm_gem_create_mmap_offset().
  142. */
  143. static int
  144. pvr_ioctl_get_bo_mmap_offset(struct drm_device *drm_dev, void *raw_args,
  145. struct drm_file *file)
  146. {
  147. struct drm_pvr_ioctl_get_bo_mmap_offset_args *args = raw_args;
  148. struct pvr_file *pvr_file = to_pvr_file(file);
  149. struct pvr_gem_object *pvr_obj;
  150. struct drm_gem_object *gem_obj;
  151. int idx;
  152. int ret;
  153. if (!drm_dev_enter(drm_dev, &idx))
  154. return -EIO;
  155. /* All padding fields must be zeroed. */
  156. if (args->_padding_4 != 0) {
  157. ret = -EINVAL;
  158. goto err_drm_dev_exit;
  159. }
  160. /*
  161. * Obtain a kernel reference to the buffer object. This reference is
  162. * counted and must be manually dropped before returning. If a buffer
  163. * object cannot be found for the specified handle, return -%ENOENT (No
  164. * such file or directory).
  165. */
  166. pvr_obj = pvr_gem_object_from_handle(pvr_file, args->handle);
  167. if (!pvr_obj) {
  168. ret = -ENOENT;
  169. goto err_drm_dev_exit;
  170. }
  171. gem_obj = gem_from_pvr_gem(pvr_obj);
  172. /*
  173. * Allocate a fake offset which can be used in userspace calls to mmap
  174. * on the DRM device file. If this fails, return the error code. This
  175. * operation is idempotent.
  176. */
  177. ret = drm_gem_create_mmap_offset(gem_obj);
  178. if (ret != 0) {
  179. /* Drop our reference to the buffer object. */
  180. drm_gem_object_put(gem_obj);
  181. goto err_drm_dev_exit;
  182. }
  183. /*
  184. * Read out the fake offset allocated by the earlier call to
  185. * drm_gem_create_mmap_offset.
  186. */
  187. args->offset = drm_vma_node_offset_addr(&gem_obj->vma_node);
  188. /* Drop our reference to the buffer object. */
  189. pvr_gem_object_put(pvr_obj);
  190. err_drm_dev_exit:
  191. drm_dev_exit(idx);
  192. return ret;
  193. }
  194. static __always_inline u64
  195. pvr_fw_version_packed(u32 major, u32 minor)
  196. {
  197. return ((u64)major << 32) | minor;
  198. }
  199. static u32
  200. rogue_get_common_store_partition_space_size(struct pvr_device *pvr_dev)
  201. {
  202. u32 max_partitions = 0;
  203. u32 tile_size_x = 0;
  204. u32 tile_size_y = 0;
  205. PVR_FEATURE_VALUE(pvr_dev, tile_size_x, &tile_size_x);
  206. PVR_FEATURE_VALUE(pvr_dev, tile_size_y, &tile_size_y);
  207. PVR_FEATURE_VALUE(pvr_dev, max_partitions, &max_partitions);
  208. if (tile_size_x == 16 && tile_size_y == 16) {
  209. u32 usc_min_output_registers_per_pix = 0;
  210. PVR_FEATURE_VALUE(pvr_dev, usc_min_output_registers_per_pix,
  211. &usc_min_output_registers_per_pix);
  212. return tile_size_x * tile_size_y * max_partitions *
  213. usc_min_output_registers_per_pix;
  214. }
  215. return max_partitions * 1024;
  216. }
  217. static u32
  218. rogue_get_common_store_alloc_region_size(struct pvr_device *pvr_dev)
  219. {
  220. u32 common_store_size_in_dwords = 512 * 4 * 4;
  221. u32 alloc_region_size;
  222. PVR_FEATURE_VALUE(pvr_dev, common_store_size_in_dwords, &common_store_size_in_dwords);
  223. alloc_region_size = common_store_size_in_dwords - (256U * 4U) -
  224. rogue_get_common_store_partition_space_size(pvr_dev);
  225. if (PVR_HAS_QUIRK(pvr_dev, 44079)) {
  226. u32 common_store_split_point = (768U * 4U * 4U);
  227. return min(common_store_split_point - (256U * 4U), alloc_region_size);
  228. }
  229. return alloc_region_size;
  230. }
  231. static inline u32
  232. rogue_get_num_phantoms(struct pvr_device *pvr_dev)
  233. {
  234. u32 num_clusters = 1;
  235. PVR_FEATURE_VALUE(pvr_dev, num_clusters, &num_clusters);
  236. return ROGUE_REQ_NUM_PHANTOMS(num_clusters);
  237. }
  238. static inline u32
  239. rogue_get_max_coeffs(struct pvr_device *pvr_dev)
  240. {
  241. u32 max_coeff_additional_portion = ROGUE_MAX_VERTEX_SHARED_REGISTERS;
  242. u32 pending_allocation_shared_regs = 2U * 1024U;
  243. u32 pending_allocation_coeff_regs = 0U;
  244. u32 num_phantoms = rogue_get_num_phantoms(pvr_dev);
  245. u32 tiles_in_flight = 0;
  246. u32 max_coeff_pixel_portion;
  247. PVR_FEATURE_VALUE(pvr_dev, isp_max_tiles_in_flight, &tiles_in_flight);
  248. max_coeff_pixel_portion = DIV_ROUND_UP(tiles_in_flight, num_phantoms);
  249. max_coeff_pixel_portion *= ROGUE_MAX_PIXEL_SHARED_REGISTERS;
  250. /*
  251. * Compute tasks on cores with BRN48492 and without compute overlap may lock
  252. * up without two additional lines of coeffs.
  253. */
  254. if (PVR_HAS_QUIRK(pvr_dev, 48492) && !PVR_HAS_FEATURE(pvr_dev, compute_overlap))
  255. pending_allocation_coeff_regs = 2U * 1024U;
  256. if (PVR_HAS_ENHANCEMENT(pvr_dev, 38748))
  257. pending_allocation_shared_regs = 0;
  258. if (PVR_HAS_ENHANCEMENT(pvr_dev, 38020))
  259. max_coeff_additional_portion += ROGUE_MAX_COMPUTE_SHARED_REGISTERS;
  260. return rogue_get_common_store_alloc_region_size(pvr_dev) + pending_allocation_coeff_regs -
  261. (max_coeff_pixel_portion + max_coeff_additional_portion +
  262. pending_allocation_shared_regs);
  263. }
  264. static inline u32
  265. rogue_get_cdm_max_local_mem_size_regs(struct pvr_device *pvr_dev)
  266. {
  267. u32 available_coeffs_in_dwords = rogue_get_max_coeffs(pvr_dev);
  268. if (PVR_HAS_QUIRK(pvr_dev, 48492) && PVR_HAS_FEATURE(pvr_dev, roguexe) &&
  269. !PVR_HAS_FEATURE(pvr_dev, compute_overlap)) {
  270. /* Driver must not use the 2 reserved lines. */
  271. available_coeffs_in_dwords -= ROGUE_CSRM_LINE_SIZE_IN_DWORDS * 2;
  272. }
  273. /*
  274. * The maximum amount of local memory available to a kernel is the minimum
  275. * of the total number of coefficient registers available and the max common
  276. * store allocation size which can be made by the CDM.
  277. *
  278. * If any coeff lines are reserved for tessellation or pixel then we need to
  279. * subtract those too.
  280. */
  281. return min(available_coeffs_in_dwords, (u32)ROGUE_MAX_PER_KERNEL_LOCAL_MEM_SIZE_REGS);
  282. }
  283. /**
  284. * pvr_dev_query_gpu_info_get()
  285. * @pvr_dev: Device pointer.
  286. * @args: [IN] Device query arguments containing a pointer to a userspace
  287. * struct drm_pvr_dev_query_gpu_info.
  288. *
  289. * If the query object pointer is NULL, the size field is updated with the
  290. * expected size of the query object.
  291. *
  292. * Returns:
  293. * * 0 on success, or if size is requested using a NULL pointer, or
  294. * * -%E2BIG if the indicated length of the allocation is less than is
  295. * required to contain the copied data, or
  296. * * -%EFAULT if local memory could not be copied to userspace.
  297. */
  298. static int
  299. pvr_dev_query_gpu_info_get(struct pvr_device *pvr_dev,
  300. struct drm_pvr_ioctl_dev_query_args *args)
  301. {
  302. struct drm_pvr_dev_query_gpu_info gpu_info = {0};
  303. int err;
  304. if (!args->pointer) {
  305. args->size = sizeof(struct drm_pvr_dev_query_gpu_info);
  306. return 0;
  307. }
  308. gpu_info.gpu_id =
  309. pvr_gpu_id_to_packed_bvnc(&pvr_dev->gpu_id);
  310. gpu_info.num_phantoms = rogue_get_num_phantoms(pvr_dev);
  311. err = PVR_UOBJ_SET(args->pointer, args->size, gpu_info);
  312. if (err < 0)
  313. return err;
  314. if (args->size > sizeof(gpu_info))
  315. args->size = sizeof(gpu_info);
  316. return 0;
  317. }
  318. /**
  319. * pvr_dev_query_runtime_info_get()
  320. * @pvr_dev: Device pointer.
  321. * @args: [IN] Device query arguments containing a pointer to a userspace
  322. * struct drm_pvr_dev_query_runtime_info.
  323. *
  324. * If the query object pointer is NULL, the size field is updated with the
  325. * expected size of the query object.
  326. *
  327. * Returns:
  328. * * 0 on success, or if size is requested using a NULL pointer, or
  329. * * -%E2BIG if the indicated length of the allocation is less than is
  330. * required to contain the copied data, or
  331. * * -%EFAULT if local memory could not be copied to userspace.
  332. */
  333. static int
  334. pvr_dev_query_runtime_info_get(struct pvr_device *pvr_dev,
  335. struct drm_pvr_ioctl_dev_query_args *args)
  336. {
  337. struct drm_pvr_dev_query_runtime_info runtime_info = {0};
  338. int err;
  339. if (!args->pointer) {
  340. args->size = sizeof(struct drm_pvr_dev_query_runtime_info);
  341. return 0;
  342. }
  343. runtime_info.free_list_min_pages =
  344. pvr_get_free_list_min_pages(pvr_dev);
  345. runtime_info.free_list_max_pages =
  346. ROGUE_PM_MAX_FREELIST_SIZE / ROGUE_PM_PAGE_SIZE;
  347. runtime_info.common_store_alloc_region_size =
  348. rogue_get_common_store_alloc_region_size(pvr_dev);
  349. runtime_info.common_store_partition_space_size =
  350. rogue_get_common_store_partition_space_size(pvr_dev);
  351. runtime_info.max_coeffs = rogue_get_max_coeffs(pvr_dev);
  352. runtime_info.cdm_max_local_mem_size_regs =
  353. rogue_get_cdm_max_local_mem_size_regs(pvr_dev);
  354. err = PVR_UOBJ_SET(args->pointer, args->size, runtime_info);
  355. if (err < 0)
  356. return err;
  357. if (args->size > sizeof(runtime_info))
  358. args->size = sizeof(runtime_info);
  359. return 0;
  360. }
  361. /**
  362. * pvr_dev_query_quirks_get() - Unpack array of quirks at the address given
  363. * in a struct drm_pvr_dev_query_quirks, or gets the amount of space required
  364. * for it.
  365. * @pvr_dev: Device pointer.
  366. * @args: [IN] Device query arguments containing a pointer to a userspace
  367. * struct drm_pvr_dev_query_query_quirks.
  368. *
  369. * If the query object pointer is NULL, the size field is updated with the
  370. * expected size of the query object.
  371. * If the userspace pointer in the query object is NULL, or the count is
  372. * short, no data is copied.
  373. * The count field will be updated to that copied, or if either pointer is
  374. * NULL, that which would have been copied.
  375. * The size field in the query object will be updated to the size copied.
  376. *
  377. * Returns:
  378. * * 0 on success, or if size/count is requested using a NULL pointer, or
  379. * * -%EINVAL if args contained non-zero reserved fields, or
  380. * * -%E2BIG if the indicated length of the allocation is less than is
  381. * required to contain the copied data, or
  382. * * -%EFAULT if local memory could not be copied to userspace.
  383. */
  384. static int
  385. pvr_dev_query_quirks_get(struct pvr_device *pvr_dev,
  386. struct drm_pvr_ioctl_dev_query_args *args)
  387. {
  388. /*
  389. * @FIXME - hardcoding of numbers here is intended as an
  390. * intermediate step so the UAPI can be fixed, but requires a
  391. * a refactor in the future to store them in a more appropriate
  392. * location
  393. */
  394. static const u32 umd_quirks_musthave[] = {
  395. 47217,
  396. 49927,
  397. 62269,
  398. };
  399. static const u32 umd_quirks[] = {
  400. 48545,
  401. 51764,
  402. };
  403. struct drm_pvr_dev_query_quirks query;
  404. u32 out[ARRAY_SIZE(umd_quirks_musthave) + ARRAY_SIZE(umd_quirks)];
  405. size_t out_musthave_count = 0;
  406. size_t out_count = 0;
  407. int err;
  408. if (!args->pointer) {
  409. args->size = sizeof(struct drm_pvr_dev_query_quirks);
  410. return 0;
  411. }
  412. err = PVR_UOBJ_GET(query, args->size, args->pointer);
  413. if (err < 0)
  414. return err;
  415. if (query._padding_c)
  416. return -EINVAL;
  417. for (int i = 0; i < ARRAY_SIZE(umd_quirks_musthave); i++) {
  418. if (pvr_device_has_uapi_quirk(pvr_dev, umd_quirks_musthave[i])) {
  419. out[out_count++] = umd_quirks_musthave[i];
  420. out_musthave_count++;
  421. }
  422. }
  423. for (int i = 0; i < ARRAY_SIZE(umd_quirks); i++) {
  424. if (pvr_device_has_uapi_quirk(pvr_dev, umd_quirks[i]))
  425. out[out_count++] = umd_quirks[i];
  426. }
  427. if (!query.quirks)
  428. goto copy_out;
  429. if (query.count < out_count)
  430. return -E2BIG;
  431. if (copy_to_user(u64_to_user_ptr(query.quirks), out,
  432. out_count * sizeof(u32))) {
  433. return -EFAULT;
  434. }
  435. query.musthave_count = out_musthave_count;
  436. copy_out:
  437. query.count = out_count;
  438. err = PVR_UOBJ_SET(args->pointer, args->size, query);
  439. if (err < 0)
  440. return err;
  441. args->size = sizeof(query);
  442. return 0;
  443. }
  444. /**
  445. * pvr_dev_query_enhancements_get() - Unpack array of enhancements at the
  446. * address given in a struct drm_pvr_dev_query_enhancements, or gets the amount
  447. * of space required for it.
  448. * @pvr_dev: Device pointer.
  449. * @args: [IN] Device query arguments containing a pointer to a userspace
  450. * struct drm_pvr_dev_query_enhancements.
  451. *
  452. * If the query object pointer is NULL, the size field is updated with the
  453. * expected size of the query object.
  454. * If the userspace pointer in the query object is NULL, or the count is
  455. * short, no data is copied.
  456. * The count field will be updated to that copied, or if either pointer is
  457. * NULL, that which would have been copied.
  458. * The size field in the query object will be updated to the size copied.
  459. *
  460. * Returns:
  461. * * 0 on success, or if size/count is requested using a NULL pointer, or
  462. * * -%EINVAL if args contained non-zero reserved fields, or
  463. * * -%E2BIG if the indicated length of the allocation is less than is
  464. * required to contain the copied data, or
  465. * * -%EFAULT if local memory could not be copied to userspace.
  466. */
  467. static int
  468. pvr_dev_query_enhancements_get(struct pvr_device *pvr_dev,
  469. struct drm_pvr_ioctl_dev_query_args *args)
  470. {
  471. /*
  472. * @FIXME - hardcoding of numbers here is intended as an
  473. * intermediate step so the UAPI can be fixed, but requires a
  474. * a refactor in the future to store them in a more appropriate
  475. * location
  476. */
  477. const u32 umd_enhancements[] = {
  478. 35421,
  479. 42064,
  480. };
  481. struct drm_pvr_dev_query_enhancements query;
  482. u32 out[ARRAY_SIZE(umd_enhancements)];
  483. size_t out_idx = 0;
  484. int err;
  485. if (!args->pointer) {
  486. args->size = sizeof(struct drm_pvr_dev_query_enhancements);
  487. return 0;
  488. }
  489. err = PVR_UOBJ_GET(query, args->size, args->pointer);
  490. if (err < 0)
  491. return err;
  492. if (query._padding_a)
  493. return -EINVAL;
  494. if (query._padding_c)
  495. return -EINVAL;
  496. for (int i = 0; i < ARRAY_SIZE(umd_enhancements); i++) {
  497. if (pvr_device_has_uapi_enhancement(pvr_dev, umd_enhancements[i]))
  498. out[out_idx++] = umd_enhancements[i];
  499. }
  500. if (!query.enhancements)
  501. goto copy_out;
  502. if (query.count < out_idx)
  503. return -E2BIG;
  504. if (copy_to_user(u64_to_user_ptr(query.enhancements), out,
  505. out_idx * sizeof(u32))) {
  506. return -EFAULT;
  507. }
  508. copy_out:
  509. query.count = out_idx;
  510. err = PVR_UOBJ_SET(args->pointer, args->size, query);
  511. if (err < 0)
  512. return err;
  513. args->size = sizeof(query);
  514. return 0;
  515. }
  516. /**
  517. * pvr_ioctl_dev_query() - IOCTL to copy information about a device
  518. * @drm_dev: [IN] DRM device.
  519. * @raw_args: [IN/OUT] Arguments passed to this IOCTL. This must be of type
  520. * &struct drm_pvr_ioctl_dev_query_args.
  521. * @file: [IN] DRM file private data.
  522. *
  523. * Called from userspace with %DRM_IOCTL_PVR_DEV_QUERY.
  524. * If the given receiving struct pointer is NULL, or the indicated size is too
  525. * small, the expected size of the struct type will be returned in the size
  526. * argument field.
  527. *
  528. * Return:
  529. * * 0 on success or when fetching the size with args->pointer == NULL, or
  530. * * -%E2BIG if the indicated size of the receiving struct is less than is
  531. * required to contain the copied data, or
  532. * * -%EINVAL if the indicated struct type is unknown, or
  533. * * -%ENOMEM if local memory could not be allocated, or
  534. * * -%EFAULT if local memory could not be copied to userspace.
  535. */
  536. static int
  537. pvr_ioctl_dev_query(struct drm_device *drm_dev, void *raw_args,
  538. struct drm_file *file)
  539. {
  540. struct pvr_device *pvr_dev = to_pvr_device(drm_dev);
  541. struct drm_pvr_ioctl_dev_query_args *args = raw_args;
  542. int idx;
  543. int ret = -EINVAL;
  544. if (!drm_dev_enter(drm_dev, &idx))
  545. return -EIO;
  546. switch ((enum drm_pvr_dev_query)args->type) {
  547. case DRM_PVR_DEV_QUERY_GPU_INFO_GET:
  548. ret = pvr_dev_query_gpu_info_get(pvr_dev, args);
  549. break;
  550. case DRM_PVR_DEV_QUERY_RUNTIME_INFO_GET:
  551. ret = pvr_dev_query_runtime_info_get(pvr_dev, args);
  552. break;
  553. case DRM_PVR_DEV_QUERY_QUIRKS_GET:
  554. ret = pvr_dev_query_quirks_get(pvr_dev, args);
  555. break;
  556. case DRM_PVR_DEV_QUERY_ENHANCEMENTS_GET:
  557. ret = pvr_dev_query_enhancements_get(pvr_dev, args);
  558. break;
  559. case DRM_PVR_DEV_QUERY_HEAP_INFO_GET:
  560. ret = pvr_heap_info_get(pvr_dev, args);
  561. break;
  562. case DRM_PVR_DEV_QUERY_STATIC_DATA_AREAS_GET:
  563. ret = pvr_static_data_areas_get(pvr_dev, args);
  564. break;
  565. }
  566. drm_dev_exit(idx);
  567. return ret;
  568. }
  569. /**
  570. * pvr_ioctl_create_context() - IOCTL to create a context
  571. * @drm_dev: [IN] DRM device.
  572. * @raw_args: [IN/OUT] Arguments passed to this IOCTL. This must be of type
  573. * &struct drm_pvr_ioctl_create_context_args.
  574. * @file: [IN] DRM file private data.
  575. *
  576. * Called from userspace with %DRM_IOCTL_PVR_CREATE_CONTEXT.
  577. *
  578. * Return:
  579. * * 0 on success, or
  580. * * -%EINVAL if provided arguments are invalid, or
  581. * * -%EFAULT if arguments can't be copied from userspace, or
  582. * * Any error returned by pvr_create_render_context().
  583. */
  584. static int
  585. pvr_ioctl_create_context(struct drm_device *drm_dev, void *raw_args,
  586. struct drm_file *file)
  587. {
  588. struct drm_pvr_ioctl_create_context_args *args = raw_args;
  589. struct pvr_file *pvr_file = file->driver_priv;
  590. int idx;
  591. int ret;
  592. if (!drm_dev_enter(drm_dev, &idx))
  593. return -EIO;
  594. ret = pvr_context_create(pvr_file, args);
  595. drm_dev_exit(idx);
  596. return ret;
  597. }
  598. /**
  599. * pvr_ioctl_destroy_context() - IOCTL to destroy a context
  600. * @drm_dev: [IN] DRM device.
  601. * @raw_args: [IN/OUT] Arguments passed to this IOCTL. This must be of type
  602. * &struct drm_pvr_ioctl_destroy_context_args.
  603. * @file: [IN] DRM file private data.
  604. *
  605. * Called from userspace with %DRM_IOCTL_PVR_DESTROY_CONTEXT.
  606. *
  607. * Return:
  608. * * 0 on success, or
  609. * * -%EINVAL if context not in context list.
  610. */
  611. static int
  612. pvr_ioctl_destroy_context(struct drm_device *drm_dev, void *raw_args,
  613. struct drm_file *file)
  614. {
  615. struct drm_pvr_ioctl_destroy_context_args *args = raw_args;
  616. struct pvr_file *pvr_file = file->driver_priv;
  617. if (args->_padding_4)
  618. return -EINVAL;
  619. return pvr_context_destroy(pvr_file, args->handle);
  620. }
  621. /**
  622. * pvr_ioctl_create_free_list() - IOCTL to create a free list
  623. * @drm_dev: [IN] DRM device.
  624. * @raw_args: [IN/OUT] Arguments passed to this IOCTL. This must be of type
  625. * &struct drm_pvr_ioctl_create_free_list_args.
  626. * @file: [IN] DRM file private data.
  627. *
  628. * Called from userspace with %DRM_IOCTL_PVR_CREATE_FREE_LIST.
  629. *
  630. * Return:
  631. * * 0 on success, or
  632. * * Any error returned by pvr_free_list_create().
  633. */
  634. static int
  635. pvr_ioctl_create_free_list(struct drm_device *drm_dev, void *raw_args,
  636. struct drm_file *file)
  637. {
  638. struct drm_pvr_ioctl_create_free_list_args *args = raw_args;
  639. struct pvr_file *pvr_file = to_pvr_file(file);
  640. struct pvr_free_list *free_list;
  641. int idx;
  642. int err;
  643. if (!drm_dev_enter(drm_dev, &idx))
  644. return -EIO;
  645. free_list = pvr_free_list_create(pvr_file, args);
  646. if (IS_ERR(free_list)) {
  647. err = PTR_ERR(free_list);
  648. goto err_drm_dev_exit;
  649. }
  650. /* Allocate object handle for userspace. */
  651. err = xa_alloc(&pvr_file->free_list_handles,
  652. &args->handle,
  653. free_list,
  654. xa_limit_32b,
  655. GFP_KERNEL);
  656. if (err < 0)
  657. goto err_cleanup;
  658. drm_dev_exit(idx);
  659. return 0;
  660. err_cleanup:
  661. pvr_free_list_put(free_list);
  662. err_drm_dev_exit:
  663. drm_dev_exit(idx);
  664. return err;
  665. }
  666. /**
  667. * pvr_ioctl_destroy_free_list() - IOCTL to destroy a free list
  668. * @drm_dev: [IN] DRM device.
  669. * @raw_args: [IN] Arguments passed to this IOCTL. This must be of type
  670. * &struct drm_pvr_ioctl_destroy_free_list_args.
  671. * @file: [IN] DRM file private data.
  672. *
  673. * Called from userspace with %DRM_IOCTL_PVR_DESTROY_FREE_LIST.
  674. *
  675. * Return:
  676. * * 0 on success, or
  677. * * -%EINVAL if free list not in object list.
  678. */
  679. static int
  680. pvr_ioctl_destroy_free_list(struct drm_device *drm_dev, void *raw_args,
  681. struct drm_file *file)
  682. {
  683. struct drm_pvr_ioctl_destroy_free_list_args *args = raw_args;
  684. struct pvr_file *pvr_file = to_pvr_file(file);
  685. struct pvr_free_list *free_list;
  686. if (args->_padding_4)
  687. return -EINVAL;
  688. free_list = xa_erase(&pvr_file->free_list_handles, args->handle);
  689. if (!free_list)
  690. return -EINVAL;
  691. pvr_free_list_put(free_list);
  692. return 0;
  693. }
  694. /**
  695. * pvr_ioctl_create_hwrt_dataset() - IOCTL to create a HWRT dataset
  696. * @drm_dev: [IN] DRM device.
  697. * @raw_args: [IN/OUT] Arguments passed to this IOCTL. This must be of type
  698. * &struct drm_pvr_ioctl_create_hwrt_dataset_args.
  699. * @file: [IN] DRM file private data.
  700. *
  701. * Called from userspace with %DRM_IOCTL_PVR_CREATE_HWRT_DATASET.
  702. *
  703. * Return:
  704. * * 0 on success, or
  705. * * Any error returned by pvr_hwrt_dataset_create().
  706. */
  707. static int
  708. pvr_ioctl_create_hwrt_dataset(struct drm_device *drm_dev, void *raw_args,
  709. struct drm_file *file)
  710. {
  711. struct drm_pvr_ioctl_create_hwrt_dataset_args *args = raw_args;
  712. struct pvr_file *pvr_file = to_pvr_file(file);
  713. struct pvr_hwrt_dataset *hwrt;
  714. int idx;
  715. int err;
  716. if (!drm_dev_enter(drm_dev, &idx))
  717. return -EIO;
  718. hwrt = pvr_hwrt_dataset_create(pvr_file, args);
  719. if (IS_ERR(hwrt)) {
  720. err = PTR_ERR(hwrt);
  721. goto err_drm_dev_exit;
  722. }
  723. /* Allocate object handle for userspace. */
  724. err = xa_alloc(&pvr_file->hwrt_handles,
  725. &args->handle,
  726. hwrt,
  727. xa_limit_32b,
  728. GFP_KERNEL);
  729. if (err < 0)
  730. goto err_cleanup;
  731. drm_dev_exit(idx);
  732. return 0;
  733. err_cleanup:
  734. pvr_hwrt_dataset_put(hwrt);
  735. err_drm_dev_exit:
  736. drm_dev_exit(idx);
  737. return err;
  738. }
  739. /**
  740. * pvr_ioctl_destroy_hwrt_dataset() - IOCTL to destroy a HWRT dataset
  741. * @drm_dev: [IN] DRM device.
  742. * @raw_args: [IN] Arguments passed to this IOCTL. This must be of type
  743. * &struct drm_pvr_ioctl_destroy_hwrt_dataset_args.
  744. * @file: [IN] DRM file private data.
  745. *
  746. * Called from userspace with %DRM_IOCTL_PVR_DESTROY_HWRT_DATASET.
  747. *
  748. * Return:
  749. * * 0 on success, or
  750. * * -%EINVAL if HWRT dataset not in object list.
  751. */
  752. static int
  753. pvr_ioctl_destroy_hwrt_dataset(struct drm_device *drm_dev, void *raw_args,
  754. struct drm_file *file)
  755. {
  756. struct drm_pvr_ioctl_destroy_hwrt_dataset_args *args = raw_args;
  757. struct pvr_file *pvr_file = to_pvr_file(file);
  758. struct pvr_hwrt_dataset *hwrt;
  759. if (args->_padding_4)
  760. return -EINVAL;
  761. hwrt = xa_erase(&pvr_file->hwrt_handles, args->handle);
  762. if (!hwrt)
  763. return -EINVAL;
  764. pvr_hwrt_dataset_put(hwrt);
  765. return 0;
  766. }
  767. /**
  768. * pvr_ioctl_create_vm_context() - IOCTL to create a VM context
  769. * @drm_dev: [IN] DRM device.
  770. * @raw_args: [IN/OUT] Arguments passed to this IOCTL. This must be of type
  771. * &struct drm_pvr_ioctl_create_vm_context_args.
  772. * @file: [IN] DRM file private data.
  773. *
  774. * Called from userspace with %DRM_IOCTL_PVR_CREATE_VM_CONTEXT.
  775. *
  776. * Return:
  777. * * 0 on success, or
  778. * * Any error returned by pvr_vm_create_context().
  779. */
  780. static int
  781. pvr_ioctl_create_vm_context(struct drm_device *drm_dev, void *raw_args,
  782. struct drm_file *file)
  783. {
  784. struct drm_pvr_ioctl_create_vm_context_args *args = raw_args;
  785. struct pvr_file *pvr_file = to_pvr_file(file);
  786. struct pvr_vm_context *vm_ctx;
  787. int idx;
  788. int err;
  789. if (!drm_dev_enter(drm_dev, &idx))
  790. return -EIO;
  791. if (args->_padding_4) {
  792. err = -EINVAL;
  793. goto err_drm_dev_exit;
  794. }
  795. vm_ctx = pvr_vm_create_context(pvr_file->pvr_dev, true);
  796. if (IS_ERR(vm_ctx)) {
  797. err = PTR_ERR(vm_ctx);
  798. goto err_drm_dev_exit;
  799. }
  800. /* Allocate object handle for userspace. */
  801. err = xa_alloc(&pvr_file->vm_ctx_handles,
  802. &args->handle,
  803. vm_ctx,
  804. xa_limit_32b,
  805. GFP_KERNEL);
  806. if (err < 0)
  807. goto err_cleanup;
  808. drm_dev_exit(idx);
  809. return 0;
  810. err_cleanup:
  811. pvr_vm_context_put(vm_ctx);
  812. err_drm_dev_exit:
  813. drm_dev_exit(idx);
  814. return err;
  815. }
  816. /**
  817. * pvr_ioctl_destroy_vm_context() - IOCTL to destroy a VM context
  818. * @drm_dev: [IN] DRM device.
  819. * @raw_args: [IN] Arguments passed to this IOCTL. This must be of type
  820. * &struct drm_pvr_ioctl_destroy_vm_context_args.
  821. * @file: [IN] DRM file private data.
  822. *
  823. * Called from userspace with %DRM_IOCTL_PVR_DESTROY_VM_CONTEXT.
  824. *
  825. * Return:
  826. * * 0 on success, or
  827. * * -%EINVAL if object not in object list.
  828. */
  829. static int
  830. pvr_ioctl_destroy_vm_context(struct drm_device *drm_dev, void *raw_args,
  831. struct drm_file *file)
  832. {
  833. struct drm_pvr_ioctl_destroy_vm_context_args *args = raw_args;
  834. struct pvr_file *pvr_file = to_pvr_file(file);
  835. struct pvr_vm_context *vm_ctx;
  836. if (args->_padding_4)
  837. return -EINVAL;
  838. vm_ctx = xa_erase(&pvr_file->vm_ctx_handles, args->handle);
  839. if (!vm_ctx)
  840. return -EINVAL;
  841. pvr_vm_context_put(vm_ctx);
  842. return 0;
  843. }
  844. /**
  845. * pvr_ioctl_vm_map() - IOCTL to map buffer to GPU address space.
  846. * @drm_dev: [IN] DRM device.
  847. * @raw_args: [IN] Arguments passed to this IOCTL. This must be of type
  848. * &struct drm_pvr_ioctl_vm_map_args.
  849. * @file: [IN] DRM file private data.
  850. *
  851. * Called from userspace with %DRM_IOCTL_PVR_VM_MAP.
  852. *
  853. * Return:
  854. * * 0 on success,
  855. * * -%EINVAL if &drm_pvr_ioctl_vm_op_map_args.flags is not zero,
  856. * * -%EINVAL if the bounds specified by &drm_pvr_ioctl_vm_op_map_args.offset
  857. * and &drm_pvr_ioctl_vm_op_map_args.size are not valid or do not fall
  858. * within the buffer object specified by
  859. * &drm_pvr_ioctl_vm_op_map_args.handle,
  860. * * -%EINVAL if the bounds specified by
  861. * &drm_pvr_ioctl_vm_op_map_args.device_addr and
  862. * &drm_pvr_ioctl_vm_op_map_args.size do not form a valid device-virtual
  863. * address range which falls entirely within a single heap, or
  864. * * -%ENOENT if &drm_pvr_ioctl_vm_op_map_args.handle does not refer to a
  865. * valid PowerVR buffer object.
  866. */
  867. static int
  868. pvr_ioctl_vm_map(struct drm_device *drm_dev, void *raw_args,
  869. struct drm_file *file)
  870. {
  871. struct pvr_device *pvr_dev = to_pvr_device(drm_dev);
  872. struct drm_pvr_ioctl_vm_map_args *args = raw_args;
  873. struct pvr_file *pvr_file = to_pvr_file(file);
  874. struct pvr_vm_context *vm_ctx;
  875. struct pvr_gem_object *pvr_obj;
  876. size_t pvr_obj_size;
  877. u64 offset_plus_size;
  878. int idx;
  879. int err;
  880. if (!drm_dev_enter(drm_dev, &idx))
  881. return -EIO;
  882. /* Initial validation of args. */
  883. if (args->_padding_14) {
  884. err = -EINVAL;
  885. goto err_drm_dev_exit;
  886. }
  887. if (args->flags != 0 ||
  888. check_add_overflow(args->offset, args->size, &offset_plus_size) ||
  889. !pvr_find_heap_containing(pvr_dev, args->device_addr, args->size)) {
  890. err = -EINVAL;
  891. goto err_drm_dev_exit;
  892. }
  893. vm_ctx = pvr_vm_context_lookup(pvr_file, args->vm_context_handle);
  894. if (!vm_ctx) {
  895. err = -EINVAL;
  896. goto err_drm_dev_exit;
  897. }
  898. pvr_obj = pvr_gem_object_from_handle(pvr_file, args->handle);
  899. if (!pvr_obj) {
  900. err = -ENOENT;
  901. goto err_put_vm_context;
  902. }
  903. pvr_obj_size = pvr_gem_object_size(pvr_obj);
  904. /*
  905. * Validate offset and size args. The alignment of these will be
  906. * checked when mapping; for now just check that they're within valid
  907. * bounds
  908. */
  909. if (args->offset >= pvr_obj_size || offset_plus_size > pvr_obj_size) {
  910. err = -EINVAL;
  911. goto err_put_pvr_object;
  912. }
  913. err = pvr_vm_map(vm_ctx, pvr_obj, args->offset,
  914. args->device_addr, args->size);
  915. if (err)
  916. goto err_put_pvr_object;
  917. /*
  918. * In order to set up the mapping, we needed a reference to &pvr_obj.
  919. * However, pvr_vm_map() obtains and stores its own reference, so we
  920. * must release ours before returning.
  921. */
  922. err_put_pvr_object:
  923. pvr_gem_object_put(pvr_obj);
  924. err_put_vm_context:
  925. pvr_vm_context_put(vm_ctx);
  926. err_drm_dev_exit:
  927. drm_dev_exit(idx);
  928. return err;
  929. }
  930. /**
  931. * pvr_ioctl_vm_unmap() - IOCTL to unmap buffer from GPU address space.
  932. * @drm_dev: [IN] DRM device.
  933. * @raw_args: [IN] Arguments passed to this IOCTL. This must be of type
  934. * &struct drm_pvr_ioctl_vm_unmap_args.
  935. * @file: [IN] DRM file private data.
  936. *
  937. * Called from userspace with %DRM_IOCTL_PVR_VM_UNMAP.
  938. *
  939. * Return:
  940. * * 0 on success,
  941. * * -%EINVAL if &drm_pvr_ioctl_vm_op_unmap_args.device_addr is not a valid
  942. * device page-aligned device-virtual address, or
  943. * * -%ENOENT if there is currently no PowerVR buffer object mapped at
  944. * &drm_pvr_ioctl_vm_op_unmap_args.device_addr.
  945. */
  946. static int
  947. pvr_ioctl_vm_unmap(struct drm_device *drm_dev, void *raw_args,
  948. struct drm_file *file)
  949. {
  950. struct drm_pvr_ioctl_vm_unmap_args *args = raw_args;
  951. struct pvr_file *pvr_file = to_pvr_file(file);
  952. struct pvr_vm_context *vm_ctx;
  953. int err;
  954. /* Initial validation of args. */
  955. if (args->_padding_4)
  956. return -EINVAL;
  957. vm_ctx = pvr_vm_context_lookup(pvr_file, args->vm_context_handle);
  958. if (!vm_ctx)
  959. return -EINVAL;
  960. err = pvr_vm_unmap(vm_ctx, args->device_addr, args->size);
  961. pvr_vm_context_put(vm_ctx);
  962. return err;
  963. }
  964. /*
  965. * pvr_ioctl_submit_job() - IOCTL to submit a job to the GPU
  966. * @drm_dev: [IN] DRM device.
  967. * @raw_args: [IN] Arguments passed to this IOCTL. This must be of type
  968. * &struct drm_pvr_ioctl_submit_job_args.
  969. * @file: [IN] DRM file private data.
  970. *
  971. * Called from userspace with %DRM_IOCTL_PVR_SUBMIT_JOB.
  972. *
  973. * Return:
  974. * * 0 on success, or
  975. * * -%EINVAL if arguments are invalid.
  976. */
  977. static int
  978. pvr_ioctl_submit_jobs(struct drm_device *drm_dev, void *raw_args,
  979. struct drm_file *file)
  980. {
  981. struct drm_pvr_ioctl_submit_jobs_args *args = raw_args;
  982. struct pvr_device *pvr_dev = to_pvr_device(drm_dev);
  983. struct pvr_file *pvr_file = to_pvr_file(file);
  984. int idx;
  985. int err;
  986. if (!drm_dev_enter(drm_dev, &idx))
  987. return -EIO;
  988. err = pvr_submit_jobs(pvr_dev, pvr_file, args);
  989. drm_dev_exit(idx);
  990. return err;
  991. }
  992. int
  993. pvr_get_uobj(u64 usr_ptr, u32 usr_stride, u32 min_stride, u32 obj_size, void *out)
  994. {
  995. if (usr_stride < min_stride)
  996. return -EINVAL;
  997. return copy_struct_from_user(out, obj_size, u64_to_user_ptr(usr_ptr), usr_stride);
  998. }
  999. int
  1000. pvr_set_uobj(u64 usr_ptr, u32 usr_stride, u32 min_stride, u32 obj_size, const void *in)
  1001. {
  1002. if (usr_stride < min_stride)
  1003. return -EINVAL;
  1004. if (copy_to_user(u64_to_user_ptr(usr_ptr), in, min_t(u32, usr_stride, obj_size)))
  1005. return -EFAULT;
  1006. if (usr_stride > obj_size &&
  1007. clear_user(u64_to_user_ptr(usr_ptr + obj_size), usr_stride - obj_size)) {
  1008. return -EFAULT;
  1009. }
  1010. return 0;
  1011. }
  1012. int
  1013. pvr_get_uobj_array(const struct drm_pvr_obj_array *in, u32 min_stride, u32 obj_size, void **out)
  1014. {
  1015. int ret = 0;
  1016. void *out_alloc;
  1017. if (in->stride < min_stride)
  1018. return -EINVAL;
  1019. if (!in->count)
  1020. return 0;
  1021. out_alloc = kvmalloc_array(in->count, obj_size, GFP_KERNEL);
  1022. if (!out_alloc)
  1023. return -ENOMEM;
  1024. if (obj_size == in->stride) {
  1025. if (copy_from_user(out_alloc, u64_to_user_ptr(in->array),
  1026. (unsigned long)obj_size * in->count))
  1027. ret = -EFAULT;
  1028. } else {
  1029. void __user *in_ptr = u64_to_user_ptr(in->array);
  1030. void *out_ptr = out_alloc;
  1031. for (u32 i = 0; i < in->count; i++) {
  1032. ret = copy_struct_from_user(out_ptr, obj_size, in_ptr, in->stride);
  1033. if (ret)
  1034. break;
  1035. out_ptr += obj_size;
  1036. in_ptr += in->stride;
  1037. }
  1038. }
  1039. if (ret) {
  1040. kvfree(out_alloc);
  1041. return ret;
  1042. }
  1043. *out = out_alloc;
  1044. return 0;
  1045. }
  1046. int
  1047. pvr_set_uobj_array(const struct drm_pvr_obj_array *out, u32 min_stride, u32 obj_size,
  1048. const void *in)
  1049. {
  1050. if (out->stride < min_stride)
  1051. return -EINVAL;
  1052. if (!out->count)
  1053. return 0;
  1054. if (obj_size == out->stride) {
  1055. if (copy_to_user(u64_to_user_ptr(out->array), in,
  1056. (unsigned long)obj_size * out->count))
  1057. return -EFAULT;
  1058. } else {
  1059. u32 cpy_elem_size = min_t(u32, out->stride, obj_size);
  1060. void __user *out_ptr = u64_to_user_ptr(out->array);
  1061. const void *in_ptr = in;
  1062. for (u32 i = 0; i < out->count; i++) {
  1063. if (copy_to_user(out_ptr, in_ptr, cpy_elem_size))
  1064. return -EFAULT;
  1065. out_ptr += obj_size;
  1066. in_ptr += out->stride;
  1067. }
  1068. if (out->stride > obj_size &&
  1069. clear_user(u64_to_user_ptr(out->array + obj_size),
  1070. out->stride - obj_size)) {
  1071. return -EFAULT;
  1072. }
  1073. }
  1074. return 0;
  1075. }
  1076. #define DRM_PVR_IOCTL(_name, _func, _flags) \
  1077. DRM_IOCTL_DEF_DRV(PVR_##_name, pvr_ioctl_##_func, _flags)
  1078. /* clang-format off */
  1079. static const struct drm_ioctl_desc pvr_drm_driver_ioctls[] = {
  1080. DRM_PVR_IOCTL(DEV_QUERY, dev_query, DRM_RENDER_ALLOW),
  1081. DRM_PVR_IOCTL(CREATE_BO, create_bo, DRM_RENDER_ALLOW),
  1082. DRM_PVR_IOCTL(GET_BO_MMAP_OFFSET, get_bo_mmap_offset, DRM_RENDER_ALLOW),
  1083. DRM_PVR_IOCTL(CREATE_VM_CONTEXT, create_vm_context, DRM_RENDER_ALLOW),
  1084. DRM_PVR_IOCTL(DESTROY_VM_CONTEXT, destroy_vm_context, DRM_RENDER_ALLOW),
  1085. DRM_PVR_IOCTL(VM_MAP, vm_map, DRM_RENDER_ALLOW),
  1086. DRM_PVR_IOCTL(VM_UNMAP, vm_unmap, DRM_RENDER_ALLOW),
  1087. DRM_PVR_IOCTL(CREATE_CONTEXT, create_context, DRM_RENDER_ALLOW),
  1088. DRM_PVR_IOCTL(DESTROY_CONTEXT, destroy_context, DRM_RENDER_ALLOW),
  1089. DRM_PVR_IOCTL(CREATE_FREE_LIST, create_free_list, DRM_RENDER_ALLOW),
  1090. DRM_PVR_IOCTL(DESTROY_FREE_LIST, destroy_free_list, DRM_RENDER_ALLOW),
  1091. DRM_PVR_IOCTL(CREATE_HWRT_DATASET, create_hwrt_dataset, DRM_RENDER_ALLOW),
  1092. DRM_PVR_IOCTL(DESTROY_HWRT_DATASET, destroy_hwrt_dataset, DRM_RENDER_ALLOW),
  1093. DRM_PVR_IOCTL(SUBMIT_JOBS, submit_jobs, DRM_RENDER_ALLOW),
  1094. };
  1095. /* clang-format on */
  1096. #undef DRM_PVR_IOCTL
  1097. /**
  1098. * pvr_drm_driver_open() - Driver callback when a new &struct drm_file is opened
  1099. * @drm_dev: [IN] DRM device.
  1100. * @file: [IN] DRM file private data.
  1101. *
  1102. * Allocates powervr-specific file private data (&struct pvr_file).
  1103. *
  1104. * Registered in &pvr_drm_driver.
  1105. *
  1106. * Return:
  1107. * * 0 on success,
  1108. * * -%ENOMEM if the allocation of a &struct ipvr_file fails, or
  1109. * * Any error returned by pvr_memory_context_init().
  1110. */
  1111. static int
  1112. pvr_drm_driver_open(struct drm_device *drm_dev, struct drm_file *file)
  1113. {
  1114. struct pvr_device *pvr_dev = to_pvr_device(drm_dev);
  1115. struct pvr_file *pvr_file;
  1116. pvr_file = kzalloc(sizeof(*pvr_file), GFP_KERNEL);
  1117. if (!pvr_file)
  1118. return -ENOMEM;
  1119. /*
  1120. * Store reference to base DRM file private data for use by
  1121. * from_pvr_file.
  1122. */
  1123. pvr_file->file = file;
  1124. /*
  1125. * Store reference to powervr-specific outer device struct in file
  1126. * private data for convenient access.
  1127. */
  1128. pvr_file->pvr_dev = pvr_dev;
  1129. INIT_LIST_HEAD(&pvr_file->contexts);
  1130. xa_init_flags(&pvr_file->ctx_handles, XA_FLAGS_ALLOC1);
  1131. xa_init_flags(&pvr_file->free_list_handles, XA_FLAGS_ALLOC1);
  1132. xa_init_flags(&pvr_file->hwrt_handles, XA_FLAGS_ALLOC1);
  1133. xa_init_flags(&pvr_file->vm_ctx_handles, XA_FLAGS_ALLOC1);
  1134. /*
  1135. * Store reference to powervr-specific file private data in DRM file
  1136. * private data.
  1137. */
  1138. file->driver_priv = pvr_file;
  1139. return 0;
  1140. }
  1141. /**
  1142. * pvr_drm_driver_postclose() - One of the driver callbacks when a &struct
  1143. * drm_file is closed.
  1144. * @drm_dev: [IN] DRM device (unused).
  1145. * @file: [IN] DRM file private data.
  1146. *
  1147. * Frees powervr-specific file private data (&struct pvr_file).
  1148. *
  1149. * Registered in &pvr_drm_driver.
  1150. */
  1151. static void
  1152. pvr_drm_driver_postclose(__always_unused struct drm_device *drm_dev,
  1153. struct drm_file *file)
  1154. {
  1155. struct pvr_file *pvr_file = to_pvr_file(file);
  1156. /* Kill remaining contexts. */
  1157. pvr_destroy_contexts_for_file(pvr_file);
  1158. /* Drop references on any remaining objects. */
  1159. pvr_destroy_free_lists_for_file(pvr_file);
  1160. pvr_destroy_hwrt_datasets_for_file(pvr_file);
  1161. pvr_destroy_vm_contexts_for_file(pvr_file);
  1162. kfree(pvr_file);
  1163. file->driver_priv = NULL;
  1164. }
  1165. DEFINE_DRM_GEM_FOPS(pvr_drm_driver_fops);
  1166. static struct drm_driver pvr_drm_driver = {
  1167. .driver_features = DRIVER_GEM | DRIVER_GEM_GPUVA | DRIVER_RENDER |
  1168. DRIVER_SYNCOBJ | DRIVER_SYNCOBJ_TIMELINE,
  1169. .open = pvr_drm_driver_open,
  1170. .postclose = pvr_drm_driver_postclose,
  1171. .ioctls = pvr_drm_driver_ioctls,
  1172. .num_ioctls = ARRAY_SIZE(pvr_drm_driver_ioctls),
  1173. .fops = &pvr_drm_driver_fops,
  1174. #if defined(CONFIG_DEBUG_FS)
  1175. .debugfs_init = pvr_debugfs_init,
  1176. #endif
  1177. .name = PVR_DRIVER_NAME,
  1178. .desc = PVR_DRIVER_DESC,
  1179. .date = PVR_DRIVER_DATE,
  1180. .major = PVR_DRIVER_MAJOR,
  1181. .minor = PVR_DRIVER_MINOR,
  1182. .patchlevel = PVR_DRIVER_PATCHLEVEL,
  1183. .gem_prime_import_sg_table = drm_gem_shmem_prime_import_sg_table,
  1184. .gem_create_object = pvr_gem_create_object,
  1185. };
  1186. static int
  1187. pvr_probe(struct platform_device *plat_dev)
  1188. {
  1189. struct pvr_device *pvr_dev;
  1190. struct drm_device *drm_dev;
  1191. int err;
  1192. pvr_dev = devm_drm_dev_alloc(&plat_dev->dev, &pvr_drm_driver,
  1193. struct pvr_device, base);
  1194. if (IS_ERR(pvr_dev))
  1195. return PTR_ERR(pvr_dev);
  1196. drm_dev = &pvr_dev->base;
  1197. platform_set_drvdata(plat_dev, drm_dev);
  1198. init_rwsem(&pvr_dev->reset_sem);
  1199. pvr_context_device_init(pvr_dev);
  1200. err = pvr_queue_device_init(pvr_dev);
  1201. if (err)
  1202. goto err_context_fini;
  1203. devm_pm_runtime_enable(&plat_dev->dev);
  1204. pm_runtime_mark_last_busy(&plat_dev->dev);
  1205. pm_runtime_set_autosuspend_delay(&plat_dev->dev, 50);
  1206. pm_runtime_use_autosuspend(&plat_dev->dev);
  1207. pvr_watchdog_init(pvr_dev);
  1208. err = pvr_device_init(pvr_dev);
  1209. if (err)
  1210. goto err_watchdog_fini;
  1211. err = drm_dev_register(drm_dev, 0);
  1212. if (err)
  1213. goto err_device_fini;
  1214. xa_init_flags(&pvr_dev->free_list_ids, XA_FLAGS_ALLOC1);
  1215. xa_init_flags(&pvr_dev->job_ids, XA_FLAGS_ALLOC1);
  1216. return 0;
  1217. err_device_fini:
  1218. pvr_device_fini(pvr_dev);
  1219. err_watchdog_fini:
  1220. pvr_watchdog_fini(pvr_dev);
  1221. pvr_queue_device_fini(pvr_dev);
  1222. err_context_fini:
  1223. pvr_context_device_fini(pvr_dev);
  1224. return err;
  1225. }
  1226. static void pvr_remove(struct platform_device *plat_dev)
  1227. {
  1228. struct drm_device *drm_dev = platform_get_drvdata(plat_dev);
  1229. struct pvr_device *pvr_dev = to_pvr_device(drm_dev);
  1230. WARN_ON(!xa_empty(&pvr_dev->job_ids));
  1231. WARN_ON(!xa_empty(&pvr_dev->free_list_ids));
  1232. xa_destroy(&pvr_dev->job_ids);
  1233. xa_destroy(&pvr_dev->free_list_ids);
  1234. pm_runtime_suspend(drm_dev->dev);
  1235. pvr_device_fini(pvr_dev);
  1236. drm_dev_unplug(drm_dev);
  1237. pvr_watchdog_fini(pvr_dev);
  1238. pvr_queue_device_fini(pvr_dev);
  1239. pvr_context_device_fini(pvr_dev);
  1240. }
  1241. static const struct of_device_id dt_match[] = {
  1242. { .compatible = "img,img-axe", .data = NULL },
  1243. {}
  1244. };
  1245. MODULE_DEVICE_TABLE(of, dt_match);
  1246. static const struct dev_pm_ops pvr_pm_ops = {
  1247. RUNTIME_PM_OPS(pvr_power_device_suspend, pvr_power_device_resume, pvr_power_device_idle)
  1248. };
  1249. static struct platform_driver pvr_driver = {
  1250. .probe = pvr_probe,
  1251. .remove_new = pvr_remove,
  1252. .driver = {
  1253. .name = PVR_DRIVER_NAME,
  1254. .pm = &pvr_pm_ops,
  1255. .of_match_table = dt_match,
  1256. },
  1257. };
  1258. module_platform_driver(pvr_driver);
  1259. MODULE_AUTHOR("Imagination Technologies Ltd.");
  1260. MODULE_DESCRIPTION(PVR_DRIVER_DESC);
  1261. MODULE_LICENSE("Dual MIT/GPL");
  1262. MODULE_IMPORT_NS(DMA_BUF);
  1263. MODULE_FIRMWARE("powervr/rogue_33.15.11.3_v1.fw");