pvr_vm.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188
  1. // SPDX-License-Identifier: GPL-2.0-only OR MIT
  2. /* Copyright (c) 2023 Imagination Technologies Ltd. */
  3. #include "pvr_vm.h"
  4. #include "pvr_device.h"
  5. #include "pvr_drv.h"
  6. #include "pvr_gem.h"
  7. #include "pvr_mmu.h"
  8. #include "pvr_rogue_fwif.h"
  9. #include "pvr_rogue_heap_config.h"
  10. #include <drm/drm_exec.h>
  11. #include <drm/drm_gem.h>
  12. #include <drm/drm_gpuvm.h>
  13. #include <linux/bug.h>
  14. #include <linux/container_of.h>
  15. #include <linux/err.h>
  16. #include <linux/errno.h>
  17. #include <linux/gfp_types.h>
  18. #include <linux/kref.h>
  19. #include <linux/mutex.h>
  20. #include <linux/stddef.h>
  21. /**
  22. * DOC: Memory context
  23. *
  24. * This is the "top level" datatype in the VM code. It's exposed in the public
  25. * API as an opaque handle.
  26. */
  27. /**
  28. * struct pvr_vm_context - Context type used to represent a single VM.
  29. */
  30. struct pvr_vm_context {
  31. /**
  32. * @pvr_dev: The PowerVR device to which this context is bound.
  33. * This binding is immutable for the life of the context.
  34. */
  35. struct pvr_device *pvr_dev;
  36. /** @mmu_ctx: The context for binding to physical memory. */
  37. struct pvr_mmu_context *mmu_ctx;
  38. /** @gpuvm_mgr: GPUVM object associated with this context. */
  39. struct drm_gpuvm gpuvm_mgr;
  40. /** @lock: Global lock on this VM. */
  41. struct mutex lock;
  42. /**
  43. * @fw_mem_ctx_obj: Firmware object representing firmware memory
  44. * context.
  45. */
  46. struct pvr_fw_object *fw_mem_ctx_obj;
  47. /** @ref_count: Reference count of object. */
  48. struct kref ref_count;
  49. /**
  50. * @dummy_gem: GEM object to enable VM reservation. All private BOs
  51. * should use the @dummy_gem.resv and not their own _resv field.
  52. */
  53. struct drm_gem_object dummy_gem;
  54. };
  55. static inline
  56. struct pvr_vm_context *to_pvr_vm_context(struct drm_gpuvm *gpuvm)
  57. {
  58. return container_of(gpuvm, struct pvr_vm_context, gpuvm_mgr);
  59. }
  60. struct pvr_vm_context *pvr_vm_context_get(struct pvr_vm_context *vm_ctx)
  61. {
  62. if (vm_ctx)
  63. kref_get(&vm_ctx->ref_count);
  64. return vm_ctx;
  65. }
  66. /**
  67. * pvr_vm_get_page_table_root_addr() - Get the DMA address of the root of the
  68. * page table structure behind a VM context.
  69. * @vm_ctx: Target VM context.
  70. */
  71. dma_addr_t pvr_vm_get_page_table_root_addr(struct pvr_vm_context *vm_ctx)
  72. {
  73. return pvr_mmu_get_root_table_dma_addr(vm_ctx->mmu_ctx);
  74. }
  75. /**
  76. * pvr_vm_get_dma_resv() - Expose the dma_resv owned by the VM context.
  77. * @vm_ctx: Target VM context.
  78. *
  79. * This is used to allow private BOs to share a dma_resv for faster fence
  80. * updates.
  81. *
  82. * Returns: The dma_resv pointer.
  83. */
  84. struct dma_resv *pvr_vm_get_dma_resv(struct pvr_vm_context *vm_ctx)
  85. {
  86. return vm_ctx->dummy_gem.resv;
  87. }
  88. /**
  89. * DOC: Memory mappings
  90. */
  91. /**
  92. * struct pvr_vm_gpuva - Wrapper type representing a single VM mapping.
  93. */
  94. struct pvr_vm_gpuva {
  95. /** @base: The wrapped drm_gpuva object. */
  96. struct drm_gpuva base;
  97. };
  98. #define to_pvr_vm_gpuva(va) container_of_const(va, struct pvr_vm_gpuva, base)
  99. enum pvr_vm_bind_type {
  100. PVR_VM_BIND_TYPE_MAP,
  101. PVR_VM_BIND_TYPE_UNMAP,
  102. };
  103. /**
  104. * struct pvr_vm_bind_op - Context of a map/unmap operation.
  105. */
  106. struct pvr_vm_bind_op {
  107. /** @type: Map or unmap. */
  108. enum pvr_vm_bind_type type;
  109. /** @pvr_obj: Object associated with mapping (map only). */
  110. struct pvr_gem_object *pvr_obj;
  111. /**
  112. * @vm_ctx: VM context where the mapping will be created or destroyed.
  113. */
  114. struct pvr_vm_context *vm_ctx;
  115. /** @mmu_op_ctx: MMU op context. */
  116. struct pvr_mmu_op_context *mmu_op_ctx;
  117. /** @gpuvm_bo: Prealloced wrapped BO for attaching to the gpuvm. */
  118. struct drm_gpuvm_bo *gpuvm_bo;
  119. /**
  120. * @new_va: Prealloced VA mapping object (init in callback).
  121. * Used when creating a mapping.
  122. */
  123. struct pvr_vm_gpuva *new_va;
  124. /**
  125. * @prev_va: Prealloced VA mapping object (init in callback).
  126. * Used when a mapping or unmapping operation overlaps an existing
  127. * mapping and splits away the beginning into a new mapping.
  128. */
  129. struct pvr_vm_gpuva *prev_va;
  130. /**
  131. * @next_va: Prealloced VA mapping object (init in callback).
  132. * Used when a mapping or unmapping operation overlaps an existing
  133. * mapping and splits away the end into a new mapping.
  134. */
  135. struct pvr_vm_gpuva *next_va;
  136. /** @offset: Offset into @pvr_obj to begin mapping from. */
  137. u64 offset;
  138. /** @device_addr: Device-virtual address at the start of the mapping. */
  139. u64 device_addr;
  140. /** @size: Size of the desired mapping. */
  141. u64 size;
  142. };
  143. /**
  144. * pvr_vm_bind_op_exec() - Execute a single bind op.
  145. * @bind_op: Bind op context.
  146. *
  147. * Returns:
  148. * * 0 on success,
  149. * * Any error code returned by drm_gpuva_sm_map(), drm_gpuva_sm_unmap(), or
  150. * a callback function.
  151. */
  152. static int pvr_vm_bind_op_exec(struct pvr_vm_bind_op *bind_op)
  153. {
  154. switch (bind_op->type) {
  155. case PVR_VM_BIND_TYPE_MAP:
  156. return drm_gpuvm_sm_map(&bind_op->vm_ctx->gpuvm_mgr,
  157. bind_op, bind_op->device_addr,
  158. bind_op->size,
  159. gem_from_pvr_gem(bind_op->pvr_obj),
  160. bind_op->offset);
  161. case PVR_VM_BIND_TYPE_UNMAP:
  162. return drm_gpuvm_sm_unmap(&bind_op->vm_ctx->gpuvm_mgr,
  163. bind_op, bind_op->device_addr,
  164. bind_op->size);
  165. }
  166. /*
  167. * This shouldn't happen unless something went wrong
  168. * in drm_sched.
  169. */
  170. WARN_ON(1);
  171. return -EINVAL;
  172. }
  173. static void pvr_vm_bind_op_fini(struct pvr_vm_bind_op *bind_op)
  174. {
  175. drm_gpuvm_bo_put(bind_op->gpuvm_bo);
  176. kfree(bind_op->new_va);
  177. kfree(bind_op->prev_va);
  178. kfree(bind_op->next_va);
  179. if (bind_op->pvr_obj)
  180. pvr_gem_object_put(bind_op->pvr_obj);
  181. if (bind_op->mmu_op_ctx)
  182. pvr_mmu_op_context_destroy(bind_op->mmu_op_ctx);
  183. }
  184. static int
  185. pvr_vm_bind_op_map_init(struct pvr_vm_bind_op *bind_op,
  186. struct pvr_vm_context *vm_ctx,
  187. struct pvr_gem_object *pvr_obj, u64 offset,
  188. u64 device_addr, u64 size)
  189. {
  190. struct drm_gem_object *obj = gem_from_pvr_gem(pvr_obj);
  191. const bool is_user = vm_ctx != vm_ctx->pvr_dev->kernel_vm_ctx;
  192. const u64 pvr_obj_size = pvr_gem_object_size(pvr_obj);
  193. struct sg_table *sgt;
  194. u64 offset_plus_size;
  195. int err;
  196. if (check_add_overflow(offset, size, &offset_plus_size))
  197. return -EINVAL;
  198. if (is_user &&
  199. !pvr_find_heap_containing(vm_ctx->pvr_dev, device_addr, size)) {
  200. return -EINVAL;
  201. }
  202. if (!pvr_device_addr_and_size_are_valid(vm_ctx, device_addr, size) ||
  203. offset & ~PAGE_MASK || size & ~PAGE_MASK ||
  204. offset >= pvr_obj_size || offset_plus_size > pvr_obj_size)
  205. return -EINVAL;
  206. bind_op->type = PVR_VM_BIND_TYPE_MAP;
  207. dma_resv_lock(obj->resv, NULL);
  208. bind_op->gpuvm_bo = drm_gpuvm_bo_obtain(&vm_ctx->gpuvm_mgr, obj);
  209. dma_resv_unlock(obj->resv);
  210. if (IS_ERR(bind_op->gpuvm_bo))
  211. return PTR_ERR(bind_op->gpuvm_bo);
  212. bind_op->new_va = kzalloc(sizeof(*bind_op->new_va), GFP_KERNEL);
  213. bind_op->prev_va = kzalloc(sizeof(*bind_op->prev_va), GFP_KERNEL);
  214. bind_op->next_va = kzalloc(sizeof(*bind_op->next_va), GFP_KERNEL);
  215. if (!bind_op->new_va || !bind_op->prev_va || !bind_op->next_va) {
  216. err = -ENOMEM;
  217. goto err_bind_op_fini;
  218. }
  219. /* Pin pages so they're ready for use. */
  220. sgt = pvr_gem_object_get_pages_sgt(pvr_obj);
  221. err = PTR_ERR_OR_ZERO(sgt);
  222. if (err)
  223. goto err_bind_op_fini;
  224. bind_op->mmu_op_ctx =
  225. pvr_mmu_op_context_create(vm_ctx->mmu_ctx, sgt, offset, size);
  226. err = PTR_ERR_OR_ZERO(bind_op->mmu_op_ctx);
  227. if (err) {
  228. bind_op->mmu_op_ctx = NULL;
  229. goto err_bind_op_fini;
  230. }
  231. bind_op->pvr_obj = pvr_obj;
  232. bind_op->vm_ctx = vm_ctx;
  233. bind_op->device_addr = device_addr;
  234. bind_op->size = size;
  235. bind_op->offset = offset;
  236. return 0;
  237. err_bind_op_fini:
  238. pvr_vm_bind_op_fini(bind_op);
  239. return err;
  240. }
  241. static int
  242. pvr_vm_bind_op_unmap_init(struct pvr_vm_bind_op *bind_op,
  243. struct pvr_vm_context *vm_ctx,
  244. struct pvr_gem_object *pvr_obj,
  245. u64 device_addr, u64 size)
  246. {
  247. int err;
  248. if (!pvr_device_addr_and_size_are_valid(vm_ctx, device_addr, size))
  249. return -EINVAL;
  250. bind_op->type = PVR_VM_BIND_TYPE_UNMAP;
  251. bind_op->prev_va = kzalloc(sizeof(*bind_op->prev_va), GFP_KERNEL);
  252. bind_op->next_va = kzalloc(sizeof(*bind_op->next_va), GFP_KERNEL);
  253. if (!bind_op->prev_va || !bind_op->next_va) {
  254. err = -ENOMEM;
  255. goto err_bind_op_fini;
  256. }
  257. bind_op->mmu_op_ctx =
  258. pvr_mmu_op_context_create(vm_ctx->mmu_ctx, NULL, 0, 0);
  259. err = PTR_ERR_OR_ZERO(bind_op->mmu_op_ctx);
  260. if (err) {
  261. bind_op->mmu_op_ctx = NULL;
  262. goto err_bind_op_fini;
  263. }
  264. bind_op->pvr_obj = pvr_obj;
  265. bind_op->vm_ctx = vm_ctx;
  266. bind_op->device_addr = device_addr;
  267. bind_op->size = size;
  268. return 0;
  269. err_bind_op_fini:
  270. pvr_vm_bind_op_fini(bind_op);
  271. return err;
  272. }
  273. /**
  274. * pvr_vm_gpuva_map() - Insert a mapping into a memory context.
  275. * @op: gpuva op containing the remap details.
  276. * @op_ctx: Operation context.
  277. *
  278. * Context: Called by drm_gpuvm_sm_map following a successful mapping while
  279. * @op_ctx.vm_ctx mutex is held.
  280. *
  281. * Return:
  282. * * 0 on success, or
  283. * * Any error returned by pvr_mmu_map().
  284. */
  285. static int
  286. pvr_vm_gpuva_map(struct drm_gpuva_op *op, void *op_ctx)
  287. {
  288. struct pvr_gem_object *pvr_gem = gem_to_pvr_gem(op->map.gem.obj);
  289. struct pvr_vm_bind_op *ctx = op_ctx;
  290. int err;
  291. if ((op->map.gem.offset | op->map.va.range) & ~PVR_DEVICE_PAGE_MASK)
  292. return -EINVAL;
  293. err = pvr_mmu_map(ctx->mmu_op_ctx, op->map.va.range, pvr_gem->flags,
  294. op->map.va.addr);
  295. if (err)
  296. return err;
  297. drm_gpuva_map(&ctx->vm_ctx->gpuvm_mgr, &ctx->new_va->base, &op->map);
  298. drm_gpuva_link(&ctx->new_va->base, ctx->gpuvm_bo);
  299. ctx->new_va = NULL;
  300. return 0;
  301. }
  302. /**
  303. * pvr_vm_gpuva_unmap() - Remove a mapping from a memory context.
  304. * @op: gpuva op containing the unmap details.
  305. * @op_ctx: Operation context.
  306. *
  307. * Context: Called by drm_gpuvm_sm_unmap following a successful unmapping while
  308. * @op_ctx.vm_ctx mutex is held.
  309. *
  310. * Return:
  311. * * 0 on success, or
  312. * * Any error returned by pvr_mmu_unmap().
  313. */
  314. static int
  315. pvr_vm_gpuva_unmap(struct drm_gpuva_op *op, void *op_ctx)
  316. {
  317. struct pvr_vm_bind_op *ctx = op_ctx;
  318. int err = pvr_mmu_unmap(ctx->mmu_op_ctx, op->unmap.va->va.addr,
  319. op->unmap.va->va.range);
  320. if (err)
  321. return err;
  322. drm_gpuva_unmap(&op->unmap);
  323. drm_gpuva_unlink(op->unmap.va);
  324. kfree(to_pvr_vm_gpuva(op->unmap.va));
  325. return 0;
  326. }
  327. /**
  328. * pvr_vm_gpuva_remap() - Remap a mapping within a memory context.
  329. * @op: gpuva op containing the remap details.
  330. * @op_ctx: Operation context.
  331. *
  332. * Context: Called by either drm_gpuvm_sm_map or drm_gpuvm_sm_unmap when a
  333. * mapping or unmapping operation causes a region to be split. The
  334. * @op_ctx.vm_ctx mutex is held.
  335. *
  336. * Return:
  337. * * 0 on success, or
  338. * * Any error returned by pvr_vm_gpuva_unmap() or pvr_vm_gpuva_unmap().
  339. */
  340. static int
  341. pvr_vm_gpuva_remap(struct drm_gpuva_op *op, void *op_ctx)
  342. {
  343. struct pvr_vm_bind_op *ctx = op_ctx;
  344. u64 va_start = 0, va_range = 0;
  345. int err;
  346. drm_gpuva_op_remap_to_unmap_range(&op->remap, &va_start, &va_range);
  347. err = pvr_mmu_unmap(ctx->mmu_op_ctx, va_start, va_range);
  348. if (err)
  349. return err;
  350. /* No actual remap required: the page table tree depth is fixed to 3,
  351. * and we use 4k page table entries only for now.
  352. */
  353. drm_gpuva_remap(&ctx->prev_va->base, &ctx->next_va->base, &op->remap);
  354. if (op->remap.prev) {
  355. pvr_gem_object_get(gem_to_pvr_gem(ctx->prev_va->base.gem.obj));
  356. drm_gpuva_link(&ctx->prev_va->base, ctx->gpuvm_bo);
  357. ctx->prev_va = NULL;
  358. }
  359. if (op->remap.next) {
  360. pvr_gem_object_get(gem_to_pvr_gem(ctx->next_va->base.gem.obj));
  361. drm_gpuva_link(&ctx->next_va->base, ctx->gpuvm_bo);
  362. ctx->next_va = NULL;
  363. }
  364. drm_gpuva_unlink(op->remap.unmap->va);
  365. kfree(to_pvr_vm_gpuva(op->remap.unmap->va));
  366. return 0;
  367. }
  368. /*
  369. * Public API
  370. *
  371. * For an overview of these functions, see *DOC: Public API* in "pvr_vm.h".
  372. */
  373. /**
  374. * pvr_device_addr_is_valid() - Tests whether a device-virtual address
  375. * is valid.
  376. * @device_addr: Virtual device address to test.
  377. *
  378. * Return:
  379. * * %true if @device_addr is within the valid range for a device page
  380. * table and is aligned to the device page size, or
  381. * * %false otherwise.
  382. */
  383. bool
  384. pvr_device_addr_is_valid(u64 device_addr)
  385. {
  386. return (device_addr & ~PVR_PAGE_TABLE_ADDR_MASK) == 0 &&
  387. (device_addr & ~PVR_DEVICE_PAGE_MASK) == 0;
  388. }
  389. /**
  390. * pvr_device_addr_and_size_are_valid() - Tests whether a device-virtual
  391. * address and associated size are both valid.
  392. * @vm_ctx: Target VM context.
  393. * @device_addr: Virtual device address to test.
  394. * @size: Size of the range based at @device_addr to test.
  395. *
  396. * Calling pvr_device_addr_is_valid() twice (once on @size, and again on
  397. * @device_addr + @size) to verify a device-virtual address range initially
  398. * seems intuitive, but it produces a false-negative when the address range
  399. * is right at the end of device-virtual address space.
  400. *
  401. * This function catches that corner case, as well as checking that
  402. * @size is non-zero.
  403. *
  404. * Return:
  405. * * %true if @device_addr is device page aligned; @size is device page
  406. * aligned; the range specified by @device_addr and @size is within the
  407. * bounds of the device-virtual address space, and @size is non-zero, or
  408. * * %false otherwise.
  409. */
  410. bool
  411. pvr_device_addr_and_size_are_valid(struct pvr_vm_context *vm_ctx,
  412. u64 device_addr, u64 size)
  413. {
  414. return pvr_device_addr_is_valid(device_addr) &&
  415. drm_gpuvm_range_valid(&vm_ctx->gpuvm_mgr, device_addr, size) &&
  416. size != 0 && (size & ~PVR_DEVICE_PAGE_MASK) == 0 &&
  417. (device_addr + size <= PVR_PAGE_TABLE_ADDR_SPACE_SIZE);
  418. }
  419. static void pvr_gpuvm_free(struct drm_gpuvm *gpuvm)
  420. {
  421. kfree(to_pvr_vm_context(gpuvm));
  422. }
  423. static const struct drm_gpuvm_ops pvr_vm_gpuva_ops = {
  424. .vm_free = pvr_gpuvm_free,
  425. .sm_step_map = pvr_vm_gpuva_map,
  426. .sm_step_remap = pvr_vm_gpuva_remap,
  427. .sm_step_unmap = pvr_vm_gpuva_unmap,
  428. };
  429. static void
  430. fw_mem_context_init(void *cpu_ptr, void *priv)
  431. {
  432. struct rogue_fwif_fwmemcontext *fw_mem_ctx = cpu_ptr;
  433. struct pvr_vm_context *vm_ctx = priv;
  434. fw_mem_ctx->pc_dev_paddr = pvr_vm_get_page_table_root_addr(vm_ctx);
  435. fw_mem_ctx->page_cat_base_reg_set = ROGUE_FW_BIF_INVALID_PCSET;
  436. }
  437. /**
  438. * pvr_vm_create_context() - Create a new VM context.
  439. * @pvr_dev: Target PowerVR device.
  440. * @is_userspace_context: %true if this context is for userspace. This will
  441. * create a firmware memory context for the VM context
  442. * and disable warnings when tearing down mappings.
  443. *
  444. * Return:
  445. * * A handle to the newly-minted VM context on success,
  446. * * -%EINVAL if the feature "virtual address space bits" on @pvr_dev is
  447. * missing or has an unsupported value,
  448. * * -%ENOMEM if allocation of the structure behind the opaque handle fails,
  449. * or
  450. * * Any error encountered while setting up internal structures.
  451. */
  452. struct pvr_vm_context *
  453. pvr_vm_create_context(struct pvr_device *pvr_dev, bool is_userspace_context)
  454. {
  455. struct drm_device *drm_dev = from_pvr_device(pvr_dev);
  456. struct pvr_vm_context *vm_ctx;
  457. u16 device_addr_bits;
  458. int err;
  459. err = PVR_FEATURE_VALUE(pvr_dev, virtual_address_space_bits,
  460. &device_addr_bits);
  461. if (err) {
  462. drm_err(drm_dev,
  463. "Failed to get device virtual address space bits\n");
  464. return ERR_PTR(err);
  465. }
  466. if (device_addr_bits != PVR_PAGE_TABLE_ADDR_BITS) {
  467. drm_err(drm_dev,
  468. "Device has unsupported virtual address space size\n");
  469. return ERR_PTR(-EINVAL);
  470. }
  471. vm_ctx = kzalloc(sizeof(*vm_ctx), GFP_KERNEL);
  472. if (!vm_ctx)
  473. return ERR_PTR(-ENOMEM);
  474. vm_ctx->pvr_dev = pvr_dev;
  475. vm_ctx->mmu_ctx = pvr_mmu_context_create(pvr_dev);
  476. err = PTR_ERR_OR_ZERO(vm_ctx->mmu_ctx);
  477. if (err)
  478. goto err_free;
  479. if (is_userspace_context) {
  480. err = pvr_fw_object_create(pvr_dev, sizeof(struct rogue_fwif_fwmemcontext),
  481. PVR_BO_FW_FLAGS_DEVICE_UNCACHED,
  482. fw_mem_context_init, vm_ctx, &vm_ctx->fw_mem_ctx_obj);
  483. if (err)
  484. goto err_page_table_destroy;
  485. }
  486. drm_gem_private_object_init(&pvr_dev->base, &vm_ctx->dummy_gem, 0);
  487. drm_gpuvm_init(&vm_ctx->gpuvm_mgr,
  488. is_userspace_context ? "PowerVR-user-VM" : "PowerVR-FW-VM",
  489. 0, &pvr_dev->base, &vm_ctx->dummy_gem,
  490. 0, 1ULL << device_addr_bits, 0, 0, &pvr_vm_gpuva_ops);
  491. mutex_init(&vm_ctx->lock);
  492. kref_init(&vm_ctx->ref_count);
  493. return vm_ctx;
  494. err_page_table_destroy:
  495. pvr_mmu_context_destroy(vm_ctx->mmu_ctx);
  496. err_free:
  497. kfree(vm_ctx);
  498. return ERR_PTR(err);
  499. }
  500. /**
  501. * pvr_vm_context_release() - Teardown a VM context.
  502. * @ref_count: Pointer to reference counter of the VM context.
  503. *
  504. * This function also ensures that no mappings are left dangling by calling
  505. * pvr_vm_unmap_all.
  506. */
  507. static void
  508. pvr_vm_context_release(struct kref *ref_count)
  509. {
  510. struct pvr_vm_context *vm_ctx =
  511. container_of(ref_count, struct pvr_vm_context, ref_count);
  512. if (vm_ctx->fw_mem_ctx_obj)
  513. pvr_fw_object_destroy(vm_ctx->fw_mem_ctx_obj);
  514. pvr_vm_unmap_all(vm_ctx);
  515. pvr_mmu_context_destroy(vm_ctx->mmu_ctx);
  516. drm_gem_private_object_fini(&vm_ctx->dummy_gem);
  517. mutex_destroy(&vm_ctx->lock);
  518. drm_gpuvm_put(&vm_ctx->gpuvm_mgr);
  519. }
  520. /**
  521. * pvr_vm_context_lookup() - Look up VM context from handle
  522. * @pvr_file: Pointer to pvr_file structure.
  523. * @handle: Object handle.
  524. *
  525. * Takes reference on VM context object. Call pvr_vm_context_put() to release.
  526. *
  527. * Returns:
  528. * * The requested object on success, or
  529. * * %NULL on failure (object does not exist in list, or is not a VM context)
  530. */
  531. struct pvr_vm_context *
  532. pvr_vm_context_lookup(struct pvr_file *pvr_file, u32 handle)
  533. {
  534. struct pvr_vm_context *vm_ctx;
  535. xa_lock(&pvr_file->vm_ctx_handles);
  536. vm_ctx = xa_load(&pvr_file->vm_ctx_handles, handle);
  537. pvr_vm_context_get(vm_ctx);
  538. xa_unlock(&pvr_file->vm_ctx_handles);
  539. return vm_ctx;
  540. }
  541. /**
  542. * pvr_vm_context_put() - Release a reference on a VM context
  543. * @vm_ctx: Target VM context.
  544. *
  545. * Returns:
  546. * * %true if the VM context was destroyed, or
  547. * * %false if there are any references still remaining.
  548. */
  549. bool
  550. pvr_vm_context_put(struct pvr_vm_context *vm_ctx)
  551. {
  552. if (vm_ctx)
  553. return kref_put(&vm_ctx->ref_count, pvr_vm_context_release);
  554. return true;
  555. }
  556. /**
  557. * pvr_destroy_vm_contexts_for_file: Destroy any VM contexts associated with the
  558. * given file.
  559. * @pvr_file: Pointer to pvr_file structure.
  560. *
  561. * Removes all vm_contexts associated with @pvr_file from the device VM context
  562. * list and drops initial references. vm_contexts will then be destroyed once
  563. * all outstanding references are dropped.
  564. */
  565. void pvr_destroy_vm_contexts_for_file(struct pvr_file *pvr_file)
  566. {
  567. struct pvr_vm_context *vm_ctx;
  568. unsigned long handle;
  569. xa_for_each(&pvr_file->vm_ctx_handles, handle, vm_ctx) {
  570. /* vm_ctx is not used here because that would create a race with xa_erase */
  571. pvr_vm_context_put(xa_erase(&pvr_file->vm_ctx_handles, handle));
  572. }
  573. }
  574. static int
  575. pvr_vm_lock_extra(struct drm_gpuvm_exec *vm_exec)
  576. {
  577. struct pvr_vm_bind_op *bind_op = vm_exec->extra.priv;
  578. struct pvr_gem_object *pvr_obj = bind_op->pvr_obj;
  579. /* Acquire lock on the GEM object being mapped/unmapped. */
  580. return drm_exec_lock_obj(&vm_exec->exec, gem_from_pvr_gem(pvr_obj));
  581. }
  582. /**
  583. * pvr_vm_map() - Map a section of physical memory into a section of
  584. * device-virtual memory.
  585. * @vm_ctx: Target VM context.
  586. * @pvr_obj: Target PowerVR memory object.
  587. * @pvr_obj_offset: Offset into @pvr_obj to map from.
  588. * @device_addr: Virtual device address at the start of the requested mapping.
  589. * @size: Size of the requested mapping.
  590. *
  591. * No handle is returned to represent the mapping. Instead, callers should
  592. * remember @device_addr and use that as a handle.
  593. *
  594. * Return:
  595. * * 0 on success,
  596. * * -%EINVAL if @device_addr is not a valid page-aligned device-virtual
  597. * address; the region specified by @pvr_obj_offset and @size does not fall
  598. * entirely within @pvr_obj, or any part of the specified region of @pvr_obj
  599. * is not device-virtual page-aligned,
  600. * * Any error encountered while performing internal operations required to
  601. * destroy the mapping (returned from pvr_vm_gpuva_map or
  602. * pvr_vm_gpuva_remap).
  603. */
  604. int
  605. pvr_vm_map(struct pvr_vm_context *vm_ctx, struct pvr_gem_object *pvr_obj,
  606. u64 pvr_obj_offset, u64 device_addr, u64 size)
  607. {
  608. struct pvr_vm_bind_op bind_op = {0};
  609. struct drm_gpuvm_exec vm_exec = {
  610. .vm = &vm_ctx->gpuvm_mgr,
  611. .flags = DRM_EXEC_INTERRUPTIBLE_WAIT |
  612. DRM_EXEC_IGNORE_DUPLICATES,
  613. .extra = {
  614. .fn = pvr_vm_lock_extra,
  615. .priv = &bind_op,
  616. },
  617. };
  618. int err = pvr_vm_bind_op_map_init(&bind_op, vm_ctx, pvr_obj,
  619. pvr_obj_offset, device_addr,
  620. size);
  621. if (err)
  622. return err;
  623. pvr_gem_object_get(pvr_obj);
  624. err = drm_gpuvm_exec_lock(&vm_exec);
  625. if (err)
  626. goto err_cleanup;
  627. err = pvr_vm_bind_op_exec(&bind_op);
  628. drm_gpuvm_exec_unlock(&vm_exec);
  629. err_cleanup:
  630. pvr_vm_bind_op_fini(&bind_op);
  631. return err;
  632. }
  633. /**
  634. * pvr_vm_unmap_obj_locked() - Unmap an already mapped section of device-virtual
  635. * memory.
  636. * @vm_ctx: Target VM context.
  637. * @pvr_obj: Target PowerVR memory object.
  638. * @device_addr: Virtual device address at the start of the target mapping.
  639. * @size: Size of the target mapping.
  640. *
  641. * Return:
  642. * * 0 on success,
  643. * * -%EINVAL if @device_addr is not a valid page-aligned device-virtual
  644. * address,
  645. * * Any error encountered while performing internal operations required to
  646. * destroy the mapping (returned from pvr_vm_gpuva_unmap or
  647. * pvr_vm_gpuva_remap).
  648. *
  649. * The vm_ctx->lock must be held when calling this function.
  650. */
  651. static int
  652. pvr_vm_unmap_obj_locked(struct pvr_vm_context *vm_ctx,
  653. struct pvr_gem_object *pvr_obj,
  654. u64 device_addr, u64 size)
  655. {
  656. struct pvr_vm_bind_op bind_op = {0};
  657. struct drm_gpuvm_exec vm_exec = {
  658. .vm = &vm_ctx->gpuvm_mgr,
  659. .flags = DRM_EXEC_INTERRUPTIBLE_WAIT |
  660. DRM_EXEC_IGNORE_DUPLICATES,
  661. .extra = {
  662. .fn = pvr_vm_lock_extra,
  663. .priv = &bind_op,
  664. },
  665. };
  666. int err = pvr_vm_bind_op_unmap_init(&bind_op, vm_ctx, pvr_obj,
  667. device_addr, size);
  668. if (err)
  669. return err;
  670. pvr_gem_object_get(pvr_obj);
  671. err = drm_gpuvm_exec_lock(&vm_exec);
  672. if (err)
  673. goto err_cleanup;
  674. err = pvr_vm_bind_op_exec(&bind_op);
  675. drm_gpuvm_exec_unlock(&vm_exec);
  676. err_cleanup:
  677. pvr_vm_bind_op_fini(&bind_op);
  678. return err;
  679. }
  680. /**
  681. * pvr_vm_unmap_obj() - Unmap an already mapped section of device-virtual
  682. * memory.
  683. * @vm_ctx: Target VM context.
  684. * @pvr_obj: Target PowerVR memory object.
  685. * @device_addr: Virtual device address at the start of the target mapping.
  686. * @size: Size of the target mapping.
  687. *
  688. * Return:
  689. * * 0 on success,
  690. * * Any error encountered by pvr_vm_unmap_obj_locked.
  691. */
  692. int
  693. pvr_vm_unmap_obj(struct pvr_vm_context *vm_ctx, struct pvr_gem_object *pvr_obj,
  694. u64 device_addr, u64 size)
  695. {
  696. int err;
  697. mutex_lock(&vm_ctx->lock);
  698. err = pvr_vm_unmap_obj_locked(vm_ctx, pvr_obj, device_addr, size);
  699. mutex_unlock(&vm_ctx->lock);
  700. return err;
  701. }
  702. /**
  703. * pvr_vm_unmap() - Unmap an already mapped section of device-virtual memory.
  704. * @vm_ctx: Target VM context.
  705. * @device_addr: Virtual device address at the start of the target mapping.
  706. * @size: Size of the target mapping.
  707. *
  708. * Return:
  709. * * 0 on success,
  710. * * Any error encountered by drm_gpuva_find,
  711. * * Any error encountered by pvr_vm_unmap_obj_locked.
  712. */
  713. int
  714. pvr_vm_unmap(struct pvr_vm_context *vm_ctx, u64 device_addr, u64 size)
  715. {
  716. struct pvr_gem_object *pvr_obj;
  717. struct drm_gpuva *va;
  718. int err;
  719. mutex_lock(&vm_ctx->lock);
  720. va = drm_gpuva_find(&vm_ctx->gpuvm_mgr, device_addr, size);
  721. if (va) {
  722. pvr_obj = gem_to_pvr_gem(va->gem.obj);
  723. err = pvr_vm_unmap_obj_locked(vm_ctx, pvr_obj,
  724. va->va.addr, va->va.range);
  725. } else {
  726. err = -ENOENT;
  727. }
  728. mutex_unlock(&vm_ctx->lock);
  729. return err;
  730. }
  731. /**
  732. * pvr_vm_unmap_all() - Unmap all mappings associated with a VM context.
  733. * @vm_ctx: Target VM context.
  734. *
  735. * This function ensures that no mappings are left dangling by unmapping them
  736. * all in order of ascending device-virtual address.
  737. */
  738. void
  739. pvr_vm_unmap_all(struct pvr_vm_context *vm_ctx)
  740. {
  741. mutex_lock(&vm_ctx->lock);
  742. for (;;) {
  743. struct pvr_gem_object *pvr_obj;
  744. struct drm_gpuva *va;
  745. va = drm_gpuva_find_first(&vm_ctx->gpuvm_mgr,
  746. vm_ctx->gpuvm_mgr.mm_start,
  747. vm_ctx->gpuvm_mgr.mm_range);
  748. if (!va)
  749. break;
  750. pvr_obj = gem_to_pvr_gem(va->gem.obj);
  751. WARN_ON(pvr_vm_unmap_obj_locked(vm_ctx, pvr_obj,
  752. va->va.addr, va->va.range));
  753. }
  754. mutex_unlock(&vm_ctx->lock);
  755. }
  756. /* Static data areas are determined by firmware. */
  757. static const struct drm_pvr_static_data_area static_data_areas[] = {
  758. {
  759. .area_usage = DRM_PVR_STATIC_DATA_AREA_FENCE,
  760. .location_heap_id = DRM_PVR_HEAP_GENERAL,
  761. .offset = 0,
  762. .size = 128,
  763. },
  764. {
  765. .area_usage = DRM_PVR_STATIC_DATA_AREA_YUV_CSC,
  766. .location_heap_id = DRM_PVR_HEAP_GENERAL,
  767. .offset = 128,
  768. .size = 1024,
  769. },
  770. {
  771. .area_usage = DRM_PVR_STATIC_DATA_AREA_VDM_SYNC,
  772. .location_heap_id = DRM_PVR_HEAP_PDS_CODE_DATA,
  773. .offset = 0,
  774. .size = 128,
  775. },
  776. {
  777. .area_usage = DRM_PVR_STATIC_DATA_AREA_EOT,
  778. .location_heap_id = DRM_PVR_HEAP_PDS_CODE_DATA,
  779. .offset = 128,
  780. .size = 128,
  781. },
  782. {
  783. .area_usage = DRM_PVR_STATIC_DATA_AREA_VDM_SYNC,
  784. .location_heap_id = DRM_PVR_HEAP_USC_CODE,
  785. .offset = 0,
  786. .size = 128,
  787. },
  788. };
  789. #define GET_RESERVED_SIZE(last_offset, last_size) round_up((last_offset) + (last_size), PAGE_SIZE)
  790. /*
  791. * The values given to GET_RESERVED_SIZE() are taken from the last entry in the corresponding
  792. * static data area for each heap.
  793. */
  794. static const struct drm_pvr_heap pvr_heaps[] = {
  795. [DRM_PVR_HEAP_GENERAL] = {
  796. .base = ROGUE_GENERAL_HEAP_BASE,
  797. .size = ROGUE_GENERAL_HEAP_SIZE,
  798. .flags = 0,
  799. .page_size_log2 = PVR_DEVICE_PAGE_SHIFT,
  800. },
  801. [DRM_PVR_HEAP_PDS_CODE_DATA] = {
  802. .base = ROGUE_PDSCODEDATA_HEAP_BASE,
  803. .size = ROGUE_PDSCODEDATA_HEAP_SIZE,
  804. .flags = 0,
  805. .page_size_log2 = PVR_DEVICE_PAGE_SHIFT,
  806. },
  807. [DRM_PVR_HEAP_USC_CODE] = {
  808. .base = ROGUE_USCCODE_HEAP_BASE,
  809. .size = ROGUE_USCCODE_HEAP_SIZE,
  810. .flags = 0,
  811. .page_size_log2 = PVR_DEVICE_PAGE_SHIFT,
  812. },
  813. [DRM_PVR_HEAP_RGNHDR] = {
  814. .base = ROGUE_RGNHDR_HEAP_BASE,
  815. .size = ROGUE_RGNHDR_HEAP_SIZE,
  816. .flags = 0,
  817. .page_size_log2 = PVR_DEVICE_PAGE_SHIFT,
  818. },
  819. [DRM_PVR_HEAP_VIS_TEST] = {
  820. .base = ROGUE_VISTEST_HEAP_BASE,
  821. .size = ROGUE_VISTEST_HEAP_SIZE,
  822. .flags = 0,
  823. .page_size_log2 = PVR_DEVICE_PAGE_SHIFT,
  824. },
  825. [DRM_PVR_HEAP_TRANSFER_FRAG] = {
  826. .base = ROGUE_TRANSFER_FRAG_HEAP_BASE,
  827. .size = ROGUE_TRANSFER_FRAG_HEAP_SIZE,
  828. .flags = 0,
  829. .page_size_log2 = PVR_DEVICE_PAGE_SHIFT,
  830. },
  831. };
  832. int
  833. pvr_static_data_areas_get(const struct pvr_device *pvr_dev,
  834. struct drm_pvr_ioctl_dev_query_args *args)
  835. {
  836. struct drm_pvr_dev_query_static_data_areas query = {0};
  837. int err;
  838. if (!args->pointer) {
  839. args->size = sizeof(struct drm_pvr_dev_query_static_data_areas);
  840. return 0;
  841. }
  842. err = PVR_UOBJ_GET(query, args->size, args->pointer);
  843. if (err < 0)
  844. return err;
  845. if (!query.static_data_areas.array) {
  846. query.static_data_areas.count = ARRAY_SIZE(static_data_areas);
  847. query.static_data_areas.stride = sizeof(struct drm_pvr_static_data_area);
  848. goto copy_out;
  849. }
  850. if (query.static_data_areas.count > ARRAY_SIZE(static_data_areas))
  851. query.static_data_areas.count = ARRAY_SIZE(static_data_areas);
  852. err = PVR_UOBJ_SET_ARRAY(&query.static_data_areas, static_data_areas);
  853. if (err < 0)
  854. return err;
  855. copy_out:
  856. err = PVR_UOBJ_SET(args->pointer, args->size, query);
  857. if (err < 0)
  858. return err;
  859. args->size = sizeof(query);
  860. return 0;
  861. }
  862. int
  863. pvr_heap_info_get(const struct pvr_device *pvr_dev,
  864. struct drm_pvr_ioctl_dev_query_args *args)
  865. {
  866. struct drm_pvr_dev_query_heap_info query = {0};
  867. u64 dest;
  868. int err;
  869. if (!args->pointer) {
  870. args->size = sizeof(struct drm_pvr_dev_query_heap_info);
  871. return 0;
  872. }
  873. err = PVR_UOBJ_GET(query, args->size, args->pointer);
  874. if (err < 0)
  875. return err;
  876. if (!query.heaps.array) {
  877. query.heaps.count = ARRAY_SIZE(pvr_heaps);
  878. query.heaps.stride = sizeof(struct drm_pvr_heap);
  879. goto copy_out;
  880. }
  881. if (query.heaps.count > ARRAY_SIZE(pvr_heaps))
  882. query.heaps.count = ARRAY_SIZE(pvr_heaps);
  883. /* Region header heap is only present if BRN63142 is present. */
  884. dest = query.heaps.array;
  885. for (size_t i = 0; i < query.heaps.count; i++) {
  886. struct drm_pvr_heap heap = pvr_heaps[i];
  887. if (i == DRM_PVR_HEAP_RGNHDR && !PVR_HAS_QUIRK(pvr_dev, 63142))
  888. heap.size = 0;
  889. err = PVR_UOBJ_SET(dest, query.heaps.stride, heap);
  890. if (err < 0)
  891. return err;
  892. dest += query.heaps.stride;
  893. }
  894. copy_out:
  895. err = PVR_UOBJ_SET(args->pointer, args->size, query);
  896. if (err < 0)
  897. return err;
  898. args->size = sizeof(query);
  899. return 0;
  900. }
  901. /**
  902. * pvr_heap_contains_range() - Determine if a given heap contains the specified
  903. * device-virtual address range.
  904. * @pvr_heap: Target heap.
  905. * @start: Inclusive start of the target range.
  906. * @end: Inclusive end of the target range.
  907. *
  908. * It is an error to call this function with values of @start and @end that do
  909. * not satisfy the condition @start <= @end.
  910. */
  911. static __always_inline bool
  912. pvr_heap_contains_range(const struct drm_pvr_heap *pvr_heap, u64 start, u64 end)
  913. {
  914. return pvr_heap->base <= start && end < pvr_heap->base + pvr_heap->size;
  915. }
  916. /**
  917. * pvr_find_heap_containing() - Find a heap which contains the specified
  918. * device-virtual address range.
  919. * @pvr_dev: Target PowerVR device.
  920. * @start: Start of the target range.
  921. * @size: Size of the target range.
  922. *
  923. * Return:
  924. * * A pointer to a constant instance of struct drm_pvr_heap representing the
  925. * heap containing the entire range specified by @start and @size on
  926. * success, or
  927. * * %NULL if no such heap exists.
  928. */
  929. const struct drm_pvr_heap *
  930. pvr_find_heap_containing(struct pvr_device *pvr_dev, u64 start, u64 size)
  931. {
  932. u64 end;
  933. if (check_add_overflow(start, size - 1, &end))
  934. return NULL;
  935. /*
  936. * There are no guarantees about the order of address ranges in
  937. * &pvr_heaps, so iterate over the entire array for a heap whose
  938. * range completely encompasses the given range.
  939. */
  940. for (u32 heap_id = 0; heap_id < ARRAY_SIZE(pvr_heaps); heap_id++) {
  941. /* Filter heaps that present only with an associated quirk */
  942. if (heap_id == DRM_PVR_HEAP_RGNHDR &&
  943. !PVR_HAS_QUIRK(pvr_dev, 63142)) {
  944. continue;
  945. }
  946. if (pvr_heap_contains_range(&pvr_heaps[heap_id], start, end))
  947. return &pvr_heaps[heap_id];
  948. }
  949. return NULL;
  950. }
  951. /**
  952. * pvr_vm_find_gem_object() - Look up a buffer object from a given
  953. * device-virtual address.
  954. * @vm_ctx: [IN] Target VM context.
  955. * @device_addr: [IN] Virtual device address at the start of the required
  956. * object.
  957. * @mapped_offset_out: [OUT] Pointer to location to write offset of the start
  958. * of the mapped region within the buffer object. May be
  959. * %NULL if this information is not required.
  960. * @mapped_size_out: [OUT] Pointer to location to write size of the mapped
  961. * region. May be %NULL if this information is not required.
  962. *
  963. * If successful, a reference will be taken on the buffer object. The caller
  964. * must drop the reference with pvr_gem_object_put().
  965. *
  966. * Return:
  967. * * The PowerVR buffer object mapped at @device_addr if one exists, or
  968. * * %NULL otherwise.
  969. */
  970. struct pvr_gem_object *
  971. pvr_vm_find_gem_object(struct pvr_vm_context *vm_ctx, u64 device_addr,
  972. u64 *mapped_offset_out, u64 *mapped_size_out)
  973. {
  974. struct pvr_gem_object *pvr_obj;
  975. struct drm_gpuva *va;
  976. mutex_lock(&vm_ctx->lock);
  977. va = drm_gpuva_find_first(&vm_ctx->gpuvm_mgr, device_addr, 1);
  978. if (!va)
  979. goto err_unlock;
  980. pvr_obj = gem_to_pvr_gem(va->gem.obj);
  981. pvr_gem_object_get(pvr_obj);
  982. if (mapped_offset_out)
  983. *mapped_offset_out = va->gem.offset;
  984. if (mapped_size_out)
  985. *mapped_size_out = va->va.range;
  986. mutex_unlock(&vm_ctx->lock);
  987. return pvr_obj;
  988. err_unlock:
  989. mutex_unlock(&vm_ctx->lock);
  990. return NULL;
  991. }
  992. /**
  993. * pvr_vm_get_fw_mem_context: Get object representing firmware memory context
  994. * @vm_ctx: Target VM context.
  995. *
  996. * Returns:
  997. * * FW object representing firmware memory context, or
  998. * * %NULL if this VM context does not have a firmware memory context.
  999. */
  1000. struct pvr_fw_object *
  1001. pvr_vm_get_fw_mem_context(struct pvr_vm_context *vm_ctx)
  1002. {
  1003. return vm_ctx->fw_mem_ctx_obj;
  1004. }