amdgpu_gem.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  1. /*
  2. * Copyright 2008 Advanced Micro Devices, Inc.
  3. * Copyright 2008 Red Hat Inc.
  4. * Copyright 2009 Jerome Glisse.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22. * OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. * Authors: Dave Airlie
  25. * Alex Deucher
  26. * Jerome Glisse
  27. */
  28. #include <linux/ktime.h>
  29. #include <linux/pagemap.h>
  30. #include <drm/drmP.h>
  31. #include <drm/amdgpu_drm.h>
  32. #include "amdgpu.h"
  33. #include "amdgpu_display.h"
  34. void amdgpu_gem_object_free(struct drm_gem_object *gobj)
  35. {
  36. struct amdgpu_bo *robj = gem_to_amdgpu_bo(gobj);
  37. if (robj) {
  38. amdgpu_mn_unregister(robj);
  39. amdgpu_bo_unref(&robj);
  40. }
  41. }
  42. int amdgpu_gem_object_create(struct amdgpu_device *adev, unsigned long size,
  43. int alignment, u32 initial_domain,
  44. u64 flags, enum ttm_bo_type type,
  45. struct reservation_object *resv,
  46. struct drm_gem_object **obj)
  47. {
  48. struct amdgpu_bo *bo;
  49. struct amdgpu_bo_param bp;
  50. int r;
  51. memset(&bp, 0, sizeof(bp));
  52. *obj = NULL;
  53. /* At least align on page size */
  54. if (alignment < PAGE_SIZE) {
  55. alignment = PAGE_SIZE;
  56. }
  57. bp.size = size;
  58. bp.byte_align = alignment;
  59. bp.type = type;
  60. bp.resv = resv;
  61. bp.preferred_domain = initial_domain;
  62. retry:
  63. bp.flags = flags;
  64. bp.domain = initial_domain;
  65. r = amdgpu_bo_create(adev, &bp, &bo);
  66. if (r) {
  67. if (r != -ERESTARTSYS) {
  68. if (flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED) {
  69. flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
  70. goto retry;
  71. }
  72. if (initial_domain == AMDGPU_GEM_DOMAIN_VRAM) {
  73. initial_domain |= AMDGPU_GEM_DOMAIN_GTT;
  74. goto retry;
  75. }
  76. DRM_DEBUG("Failed to allocate GEM object (%ld, %d, %u, %d)\n",
  77. size, initial_domain, alignment, r);
  78. }
  79. return r;
  80. }
  81. *obj = &bo->gem_base;
  82. return 0;
  83. }
  84. void amdgpu_gem_force_release(struct amdgpu_device *adev)
  85. {
  86. struct drm_device *ddev = adev->ddev;
  87. struct drm_file *file;
  88. mutex_lock(&ddev->filelist_mutex);
  89. list_for_each_entry(file, &ddev->filelist, lhead) {
  90. struct drm_gem_object *gobj;
  91. int handle;
  92. WARN_ONCE(1, "Still active user space clients!\n");
  93. spin_lock(&file->table_lock);
  94. idr_for_each_entry(&file->object_idr, gobj, handle) {
  95. WARN_ONCE(1, "And also active allocations!\n");
  96. drm_gem_object_put_unlocked(gobj);
  97. }
  98. idr_destroy(&file->object_idr);
  99. spin_unlock(&file->table_lock);
  100. }
  101. mutex_unlock(&ddev->filelist_mutex);
  102. }
  103. /*
  104. * Call from drm_gem_handle_create which appear in both new and open ioctl
  105. * case.
  106. */
  107. int amdgpu_gem_object_open(struct drm_gem_object *obj,
  108. struct drm_file *file_priv)
  109. {
  110. struct amdgpu_bo *abo = gem_to_amdgpu_bo(obj);
  111. struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
  112. struct amdgpu_fpriv *fpriv = file_priv->driver_priv;
  113. struct amdgpu_vm *vm = &fpriv->vm;
  114. struct amdgpu_bo_va *bo_va;
  115. struct mm_struct *mm;
  116. int r;
  117. mm = amdgpu_ttm_tt_get_usermm(abo->tbo.ttm);
  118. if (mm && mm != current->mm)
  119. return -EPERM;
  120. if (abo->flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID &&
  121. abo->tbo.resv != vm->root.base.bo->tbo.resv)
  122. return -EPERM;
  123. r = amdgpu_bo_reserve(abo, false);
  124. if (r)
  125. return r;
  126. bo_va = amdgpu_vm_bo_find(vm, abo);
  127. if (!bo_va) {
  128. bo_va = amdgpu_vm_bo_add(adev, vm, abo);
  129. } else {
  130. ++bo_va->ref_count;
  131. }
  132. amdgpu_bo_unreserve(abo);
  133. return 0;
  134. }
  135. void amdgpu_gem_object_close(struct drm_gem_object *obj,
  136. struct drm_file *file_priv)
  137. {
  138. struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
  139. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
  140. struct amdgpu_fpriv *fpriv = file_priv->driver_priv;
  141. struct amdgpu_vm *vm = &fpriv->vm;
  142. struct amdgpu_bo_list_entry vm_pd;
  143. struct list_head list, duplicates;
  144. struct ttm_validate_buffer tv;
  145. struct ww_acquire_ctx ticket;
  146. struct amdgpu_bo_va *bo_va;
  147. int r;
  148. INIT_LIST_HEAD(&list);
  149. INIT_LIST_HEAD(&duplicates);
  150. tv.bo = &bo->tbo;
  151. tv.shared = true;
  152. list_add(&tv.head, &list);
  153. amdgpu_vm_get_pd_bo(vm, &list, &vm_pd);
  154. r = ttm_eu_reserve_buffers(&ticket, &list, false, &duplicates);
  155. if (r) {
  156. dev_err(adev->dev, "leaking bo va because "
  157. "we fail to reserve bo (%d)\n", r);
  158. return;
  159. }
  160. bo_va = amdgpu_vm_bo_find(vm, bo);
  161. if (bo_va && --bo_va->ref_count == 0) {
  162. amdgpu_vm_bo_rmv(adev, bo_va);
  163. if (amdgpu_vm_ready(vm)) {
  164. struct dma_fence *fence = NULL;
  165. r = amdgpu_vm_clear_freed(adev, vm, &fence);
  166. if (unlikely(r)) {
  167. dev_err(adev->dev, "failed to clear page "
  168. "tables on GEM object close (%d)\n", r);
  169. }
  170. if (fence) {
  171. amdgpu_bo_fence(bo, fence, true);
  172. dma_fence_put(fence);
  173. }
  174. }
  175. }
  176. ttm_eu_backoff_reservation(&ticket, &list);
  177. }
  178. /*
  179. * GEM ioctls.
  180. */
  181. int amdgpu_gem_create_ioctl(struct drm_device *dev, void *data,
  182. struct drm_file *filp)
  183. {
  184. struct amdgpu_device *adev = dev->dev_private;
  185. struct amdgpu_fpriv *fpriv = filp->driver_priv;
  186. struct amdgpu_vm *vm = &fpriv->vm;
  187. union drm_amdgpu_gem_create *args = data;
  188. uint64_t flags = args->in.domain_flags;
  189. uint64_t size = args->in.bo_size;
  190. struct reservation_object *resv = NULL;
  191. struct drm_gem_object *gobj;
  192. uint32_t handle;
  193. int r;
  194. /* reject invalid gem flags */
  195. if (flags & ~(AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
  196. AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
  197. AMDGPU_GEM_CREATE_CPU_GTT_USWC |
  198. AMDGPU_GEM_CREATE_VRAM_CLEARED |
  199. AMDGPU_GEM_CREATE_VM_ALWAYS_VALID |
  200. AMDGPU_GEM_CREATE_EXPLICIT_SYNC))
  201. return -EINVAL;
  202. /* reject invalid gem domains */
  203. if (args->in.domains & ~AMDGPU_GEM_DOMAIN_MASK)
  204. return -EINVAL;
  205. /* create a gem object to contain this object in */
  206. if (args->in.domains & (AMDGPU_GEM_DOMAIN_GDS |
  207. AMDGPU_GEM_DOMAIN_GWS | AMDGPU_GEM_DOMAIN_OA)) {
  208. if (flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID) {
  209. /* if gds bo is created from user space, it must be
  210. * passed to bo list
  211. */
  212. DRM_ERROR("GDS bo cannot be per-vm-bo\n");
  213. return -EINVAL;
  214. }
  215. flags |= AMDGPU_GEM_CREATE_NO_CPU_ACCESS;
  216. if (args->in.domains == AMDGPU_GEM_DOMAIN_GDS)
  217. size = size << AMDGPU_GDS_SHIFT;
  218. else if (args->in.domains == AMDGPU_GEM_DOMAIN_GWS)
  219. size = size << AMDGPU_GWS_SHIFT;
  220. else if (args->in.domains == AMDGPU_GEM_DOMAIN_OA)
  221. size = size << AMDGPU_OA_SHIFT;
  222. else
  223. return -EINVAL;
  224. }
  225. size = roundup(size, PAGE_SIZE);
  226. if (flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID) {
  227. r = amdgpu_bo_reserve(vm->root.base.bo, false);
  228. if (r)
  229. return r;
  230. resv = vm->root.base.bo->tbo.resv;
  231. }
  232. r = amdgpu_gem_object_create(adev, size, args->in.alignment,
  233. (u32)(0xffffffff & args->in.domains),
  234. flags, false, resv, &gobj);
  235. if (flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID) {
  236. if (!r) {
  237. struct amdgpu_bo *abo = gem_to_amdgpu_bo(gobj);
  238. abo->parent = amdgpu_bo_ref(vm->root.base.bo);
  239. }
  240. amdgpu_bo_unreserve(vm->root.base.bo);
  241. }
  242. if (r)
  243. return r;
  244. r = drm_gem_handle_create(filp, gobj, &handle);
  245. /* drop reference from allocate - handle holds it now */
  246. drm_gem_object_put_unlocked(gobj);
  247. if (r)
  248. return r;
  249. memset(args, 0, sizeof(*args));
  250. args->out.handle = handle;
  251. return 0;
  252. }
  253. int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data,
  254. struct drm_file *filp)
  255. {
  256. struct ttm_operation_ctx ctx = { true, false };
  257. struct amdgpu_device *adev = dev->dev_private;
  258. struct drm_amdgpu_gem_userptr *args = data;
  259. struct drm_gem_object *gobj;
  260. struct amdgpu_bo *bo;
  261. uint32_t handle;
  262. int r;
  263. if (offset_in_page(args->addr | args->size))
  264. return -EINVAL;
  265. /* reject unknown flag values */
  266. if (args->flags & ~(AMDGPU_GEM_USERPTR_READONLY |
  267. AMDGPU_GEM_USERPTR_ANONONLY | AMDGPU_GEM_USERPTR_VALIDATE |
  268. AMDGPU_GEM_USERPTR_REGISTER))
  269. return -EINVAL;
  270. if (!(args->flags & AMDGPU_GEM_USERPTR_READONLY) &&
  271. !(args->flags & AMDGPU_GEM_USERPTR_REGISTER)) {
  272. /* if we want to write to it we must install a MMU notifier */
  273. return -EACCES;
  274. }
  275. /* create a gem object to contain this object in */
  276. r = amdgpu_gem_object_create(adev, args->size, 0, AMDGPU_GEM_DOMAIN_CPU,
  277. 0, 0, NULL, &gobj);
  278. if (r)
  279. return r;
  280. bo = gem_to_amdgpu_bo(gobj);
  281. bo->preferred_domains = AMDGPU_GEM_DOMAIN_GTT;
  282. bo->allowed_domains = AMDGPU_GEM_DOMAIN_GTT;
  283. r = amdgpu_ttm_tt_set_userptr(bo->tbo.ttm, args->addr, args->flags);
  284. if (r)
  285. goto release_object;
  286. if (args->flags & AMDGPU_GEM_USERPTR_REGISTER) {
  287. r = amdgpu_mn_register(bo, args->addr);
  288. if (r)
  289. goto release_object;
  290. }
  291. if (args->flags & AMDGPU_GEM_USERPTR_VALIDATE) {
  292. r = amdgpu_ttm_tt_get_user_pages(bo->tbo.ttm,
  293. bo->tbo.ttm->pages);
  294. if (r)
  295. goto release_object;
  296. r = amdgpu_bo_reserve(bo, true);
  297. if (r)
  298. goto free_pages;
  299. amdgpu_ttm_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_GTT);
  300. r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
  301. amdgpu_bo_unreserve(bo);
  302. if (r)
  303. goto free_pages;
  304. }
  305. r = drm_gem_handle_create(filp, gobj, &handle);
  306. /* drop reference from allocate - handle holds it now */
  307. drm_gem_object_put_unlocked(gobj);
  308. if (r)
  309. return r;
  310. args->handle = handle;
  311. return 0;
  312. free_pages:
  313. release_pages(bo->tbo.ttm->pages, bo->tbo.ttm->num_pages);
  314. release_object:
  315. drm_gem_object_put_unlocked(gobj);
  316. return r;
  317. }
  318. int amdgpu_mode_dumb_mmap(struct drm_file *filp,
  319. struct drm_device *dev,
  320. uint32_t handle, uint64_t *offset_p)
  321. {
  322. struct drm_gem_object *gobj;
  323. struct amdgpu_bo *robj;
  324. gobj = drm_gem_object_lookup(filp, handle);
  325. if (gobj == NULL) {
  326. return -ENOENT;
  327. }
  328. robj = gem_to_amdgpu_bo(gobj);
  329. if (amdgpu_ttm_tt_get_usermm(robj->tbo.ttm) ||
  330. (robj->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)) {
  331. drm_gem_object_put_unlocked(gobj);
  332. return -EPERM;
  333. }
  334. *offset_p = amdgpu_bo_mmap_offset(robj);
  335. drm_gem_object_put_unlocked(gobj);
  336. return 0;
  337. }
  338. int amdgpu_gem_mmap_ioctl(struct drm_device *dev, void *data,
  339. struct drm_file *filp)
  340. {
  341. union drm_amdgpu_gem_mmap *args = data;
  342. uint32_t handle = args->in.handle;
  343. memset(args, 0, sizeof(*args));
  344. return amdgpu_mode_dumb_mmap(filp, dev, handle, &args->out.addr_ptr);
  345. }
  346. /**
  347. * amdgpu_gem_timeout - calculate jiffies timeout from absolute value
  348. *
  349. * @timeout_ns: timeout in ns
  350. *
  351. * Calculate the timeout in jiffies from an absolute timeout in ns.
  352. */
  353. unsigned long amdgpu_gem_timeout(uint64_t timeout_ns)
  354. {
  355. unsigned long timeout_jiffies;
  356. ktime_t timeout;
  357. /* clamp timeout if it's to large */
  358. if (((int64_t)timeout_ns) < 0)
  359. return MAX_SCHEDULE_TIMEOUT;
  360. timeout = ktime_sub(ns_to_ktime(timeout_ns), ktime_get());
  361. if (ktime_to_ns(timeout) < 0)
  362. return 0;
  363. timeout_jiffies = nsecs_to_jiffies(ktime_to_ns(timeout));
  364. /* clamp timeout to avoid unsigned-> signed overflow */
  365. if (timeout_jiffies > MAX_SCHEDULE_TIMEOUT )
  366. return MAX_SCHEDULE_TIMEOUT - 1;
  367. return timeout_jiffies;
  368. }
  369. int amdgpu_gem_wait_idle_ioctl(struct drm_device *dev, void *data,
  370. struct drm_file *filp)
  371. {
  372. union drm_amdgpu_gem_wait_idle *args = data;
  373. struct drm_gem_object *gobj;
  374. struct amdgpu_bo *robj;
  375. uint32_t handle = args->in.handle;
  376. unsigned long timeout = amdgpu_gem_timeout(args->in.timeout);
  377. int r = 0;
  378. long ret;
  379. gobj = drm_gem_object_lookup(filp, handle);
  380. if (gobj == NULL) {
  381. return -ENOENT;
  382. }
  383. robj = gem_to_amdgpu_bo(gobj);
  384. ret = reservation_object_wait_timeout_rcu(robj->tbo.resv, true, true,
  385. timeout);
  386. /* ret == 0 means not signaled,
  387. * ret > 0 means signaled
  388. * ret < 0 means interrupted before timeout
  389. */
  390. if (ret >= 0) {
  391. memset(args, 0, sizeof(*args));
  392. args->out.status = (ret == 0);
  393. } else
  394. r = ret;
  395. drm_gem_object_put_unlocked(gobj);
  396. return r;
  397. }
  398. int amdgpu_gem_metadata_ioctl(struct drm_device *dev, void *data,
  399. struct drm_file *filp)
  400. {
  401. struct drm_amdgpu_gem_metadata *args = data;
  402. struct drm_gem_object *gobj;
  403. struct amdgpu_bo *robj;
  404. int r = -1;
  405. DRM_DEBUG("%d \n", args->handle);
  406. gobj = drm_gem_object_lookup(filp, args->handle);
  407. if (gobj == NULL)
  408. return -ENOENT;
  409. robj = gem_to_amdgpu_bo(gobj);
  410. r = amdgpu_bo_reserve(robj, false);
  411. if (unlikely(r != 0))
  412. goto out;
  413. if (args->op == AMDGPU_GEM_METADATA_OP_GET_METADATA) {
  414. amdgpu_bo_get_tiling_flags(robj, &args->data.tiling_info);
  415. r = amdgpu_bo_get_metadata(robj, args->data.data,
  416. sizeof(args->data.data),
  417. &args->data.data_size_bytes,
  418. &args->data.flags);
  419. } else if (args->op == AMDGPU_GEM_METADATA_OP_SET_METADATA) {
  420. if (args->data.data_size_bytes > sizeof(args->data.data)) {
  421. r = -EINVAL;
  422. goto unreserve;
  423. }
  424. r = amdgpu_bo_set_tiling_flags(robj, args->data.tiling_info);
  425. if (!r)
  426. r = amdgpu_bo_set_metadata(robj, args->data.data,
  427. args->data.data_size_bytes,
  428. args->data.flags);
  429. }
  430. unreserve:
  431. amdgpu_bo_unreserve(robj);
  432. out:
  433. drm_gem_object_put_unlocked(gobj);
  434. return r;
  435. }
  436. /**
  437. * amdgpu_gem_va_update_vm -update the bo_va in its VM
  438. *
  439. * @adev: amdgpu_device pointer
  440. * @vm: vm to update
  441. * @bo_va: bo_va to update
  442. * @list: validation list
  443. * @operation: map, unmap or clear
  444. *
  445. * Update the bo_va directly after setting its address. Errors are not
  446. * vital here, so they are not reported back to userspace.
  447. */
  448. static void amdgpu_gem_va_update_vm(struct amdgpu_device *adev,
  449. struct amdgpu_vm *vm,
  450. struct amdgpu_bo_va *bo_va,
  451. struct list_head *list,
  452. uint32_t operation)
  453. {
  454. int r;
  455. if (!amdgpu_vm_ready(vm))
  456. return;
  457. r = amdgpu_vm_clear_freed(adev, vm, NULL);
  458. if (r)
  459. goto error;
  460. if (operation == AMDGPU_VA_OP_MAP ||
  461. operation == AMDGPU_VA_OP_REPLACE) {
  462. r = amdgpu_vm_bo_update(adev, bo_va, false);
  463. if (r)
  464. goto error;
  465. }
  466. r = amdgpu_vm_update_directories(adev, vm);
  467. error:
  468. if (r && r != -ERESTARTSYS)
  469. DRM_ERROR("Couldn't update BO_VA (%d)\n", r);
  470. }
  471. int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data,
  472. struct drm_file *filp)
  473. {
  474. const uint32_t valid_flags = AMDGPU_VM_DELAY_UPDATE |
  475. AMDGPU_VM_PAGE_READABLE | AMDGPU_VM_PAGE_WRITEABLE |
  476. AMDGPU_VM_PAGE_EXECUTABLE | AMDGPU_VM_MTYPE_MASK;
  477. const uint32_t prt_flags = AMDGPU_VM_DELAY_UPDATE |
  478. AMDGPU_VM_PAGE_PRT;
  479. struct drm_amdgpu_gem_va *args = data;
  480. struct drm_gem_object *gobj;
  481. struct amdgpu_device *adev = dev->dev_private;
  482. struct amdgpu_fpriv *fpriv = filp->driver_priv;
  483. struct amdgpu_bo *abo;
  484. struct amdgpu_bo_va *bo_va;
  485. struct amdgpu_bo_list_entry vm_pd;
  486. struct ttm_validate_buffer tv;
  487. struct ww_acquire_ctx ticket;
  488. struct list_head list, duplicates;
  489. uint64_t va_flags;
  490. int r = 0;
  491. if (args->va_address < AMDGPU_VA_RESERVED_SIZE) {
  492. dev_dbg(&dev->pdev->dev,
  493. "va_address 0x%LX is in reserved area 0x%LX\n",
  494. args->va_address, AMDGPU_VA_RESERVED_SIZE);
  495. return -EINVAL;
  496. }
  497. if (args->va_address >= AMDGPU_VA_HOLE_START &&
  498. args->va_address < AMDGPU_VA_HOLE_END) {
  499. dev_dbg(&dev->pdev->dev,
  500. "va_address 0x%LX is in VA hole 0x%LX-0x%LX\n",
  501. args->va_address, AMDGPU_VA_HOLE_START,
  502. AMDGPU_VA_HOLE_END);
  503. return -EINVAL;
  504. }
  505. args->va_address &= AMDGPU_VA_HOLE_MASK;
  506. if ((args->flags & ~valid_flags) && (args->flags & ~prt_flags)) {
  507. dev_dbg(&dev->pdev->dev, "invalid flags combination 0x%08X\n",
  508. args->flags);
  509. return -EINVAL;
  510. }
  511. switch (args->operation) {
  512. case AMDGPU_VA_OP_MAP:
  513. case AMDGPU_VA_OP_UNMAP:
  514. case AMDGPU_VA_OP_CLEAR:
  515. case AMDGPU_VA_OP_REPLACE:
  516. break;
  517. default:
  518. dev_dbg(&dev->pdev->dev, "unsupported operation %d\n",
  519. args->operation);
  520. return -EINVAL;
  521. }
  522. INIT_LIST_HEAD(&list);
  523. INIT_LIST_HEAD(&duplicates);
  524. if ((args->operation != AMDGPU_VA_OP_CLEAR) &&
  525. !(args->flags & AMDGPU_VM_PAGE_PRT)) {
  526. gobj = drm_gem_object_lookup(filp, args->handle);
  527. if (gobj == NULL)
  528. return -ENOENT;
  529. abo = gem_to_amdgpu_bo(gobj);
  530. tv.bo = &abo->tbo;
  531. tv.shared = false;
  532. list_add(&tv.head, &list);
  533. } else {
  534. gobj = NULL;
  535. abo = NULL;
  536. }
  537. amdgpu_vm_get_pd_bo(&fpriv->vm, &list, &vm_pd);
  538. r = ttm_eu_reserve_buffers(&ticket, &list, true, &duplicates);
  539. if (r)
  540. goto error_unref;
  541. if (abo) {
  542. bo_va = amdgpu_vm_bo_find(&fpriv->vm, abo);
  543. if (!bo_va) {
  544. r = -ENOENT;
  545. goto error_backoff;
  546. }
  547. } else if (args->operation != AMDGPU_VA_OP_CLEAR) {
  548. bo_va = fpriv->prt_va;
  549. } else {
  550. bo_va = NULL;
  551. }
  552. switch (args->operation) {
  553. case AMDGPU_VA_OP_MAP:
  554. r = amdgpu_vm_alloc_pts(adev, bo_va->base.vm, args->va_address,
  555. args->map_size);
  556. if (r)
  557. goto error_backoff;
  558. va_flags = amdgpu_gmc_get_pte_flags(adev, args->flags);
  559. r = amdgpu_vm_bo_map(adev, bo_va, args->va_address,
  560. args->offset_in_bo, args->map_size,
  561. va_flags);
  562. break;
  563. case AMDGPU_VA_OP_UNMAP:
  564. r = amdgpu_vm_bo_unmap(adev, bo_va, args->va_address);
  565. break;
  566. case AMDGPU_VA_OP_CLEAR:
  567. r = amdgpu_vm_bo_clear_mappings(adev, &fpriv->vm,
  568. args->va_address,
  569. args->map_size);
  570. break;
  571. case AMDGPU_VA_OP_REPLACE:
  572. r = amdgpu_vm_alloc_pts(adev, bo_va->base.vm, args->va_address,
  573. args->map_size);
  574. if (r)
  575. goto error_backoff;
  576. va_flags = amdgpu_gmc_get_pte_flags(adev, args->flags);
  577. r = amdgpu_vm_bo_replace_map(adev, bo_va, args->va_address,
  578. args->offset_in_bo, args->map_size,
  579. va_flags);
  580. break;
  581. default:
  582. break;
  583. }
  584. if (!r && !(args->flags & AMDGPU_VM_DELAY_UPDATE) && !amdgpu_vm_debug)
  585. amdgpu_gem_va_update_vm(adev, &fpriv->vm, bo_va, &list,
  586. args->operation);
  587. error_backoff:
  588. ttm_eu_backoff_reservation(&ticket, &list);
  589. error_unref:
  590. drm_gem_object_put_unlocked(gobj);
  591. return r;
  592. }
  593. int amdgpu_gem_op_ioctl(struct drm_device *dev, void *data,
  594. struct drm_file *filp)
  595. {
  596. struct amdgpu_device *adev = dev->dev_private;
  597. struct drm_amdgpu_gem_op *args = data;
  598. struct drm_gem_object *gobj;
  599. struct amdgpu_bo *robj;
  600. int r;
  601. gobj = drm_gem_object_lookup(filp, args->handle);
  602. if (gobj == NULL) {
  603. return -ENOENT;
  604. }
  605. robj = gem_to_amdgpu_bo(gobj);
  606. r = amdgpu_bo_reserve(robj, false);
  607. if (unlikely(r))
  608. goto out;
  609. switch (args->op) {
  610. case AMDGPU_GEM_OP_GET_GEM_CREATE_INFO: {
  611. struct drm_amdgpu_gem_create_in info;
  612. void __user *out = u64_to_user_ptr(args->value);
  613. info.bo_size = robj->gem_base.size;
  614. info.alignment = robj->tbo.mem.page_alignment << PAGE_SHIFT;
  615. info.domains = robj->preferred_domains;
  616. info.domain_flags = robj->flags;
  617. amdgpu_bo_unreserve(robj);
  618. if (copy_to_user(out, &info, sizeof(info)))
  619. r = -EFAULT;
  620. break;
  621. }
  622. case AMDGPU_GEM_OP_SET_PLACEMENT:
  623. if (robj->prime_shared_count && (args->value & AMDGPU_GEM_DOMAIN_VRAM)) {
  624. r = -EINVAL;
  625. amdgpu_bo_unreserve(robj);
  626. break;
  627. }
  628. if (amdgpu_ttm_tt_get_usermm(robj->tbo.ttm)) {
  629. r = -EPERM;
  630. amdgpu_bo_unreserve(robj);
  631. break;
  632. }
  633. robj->preferred_domains = args->value & (AMDGPU_GEM_DOMAIN_VRAM |
  634. AMDGPU_GEM_DOMAIN_GTT |
  635. AMDGPU_GEM_DOMAIN_CPU);
  636. robj->allowed_domains = robj->preferred_domains;
  637. if (robj->allowed_domains == AMDGPU_GEM_DOMAIN_VRAM)
  638. robj->allowed_domains |= AMDGPU_GEM_DOMAIN_GTT;
  639. if (robj->flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID)
  640. amdgpu_vm_bo_invalidate(adev, robj, true);
  641. amdgpu_bo_unreserve(robj);
  642. break;
  643. default:
  644. amdgpu_bo_unreserve(robj);
  645. r = -EINVAL;
  646. }
  647. out:
  648. drm_gem_object_put_unlocked(gobj);
  649. return r;
  650. }
  651. int amdgpu_mode_dumb_create(struct drm_file *file_priv,
  652. struct drm_device *dev,
  653. struct drm_mode_create_dumb *args)
  654. {
  655. struct amdgpu_device *adev = dev->dev_private;
  656. struct drm_gem_object *gobj;
  657. uint32_t handle;
  658. u32 domain;
  659. int r;
  660. args->pitch = amdgpu_align_pitch(adev, args->width,
  661. DIV_ROUND_UP(args->bpp, 8), 0);
  662. args->size = (u64)args->pitch * args->height;
  663. args->size = ALIGN(args->size, PAGE_SIZE);
  664. domain = amdgpu_bo_get_preferred_pin_domain(adev,
  665. amdgpu_display_supported_domains(adev));
  666. r = amdgpu_gem_object_create(adev, args->size, 0, domain,
  667. AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED,
  668. false, NULL, &gobj);
  669. if (r)
  670. return -ENOMEM;
  671. r = drm_gem_handle_create(file_priv, gobj, &handle);
  672. /* drop reference from allocate - handle holds it now */
  673. drm_gem_object_put_unlocked(gobj);
  674. if (r) {
  675. return r;
  676. }
  677. args->handle = handle;
  678. return 0;
  679. }
  680. #if defined(CONFIG_DEBUG_FS)
  681. #define amdgpu_debugfs_gem_bo_print_flag(m, bo, flag) \
  682. if (bo->flags & (AMDGPU_GEM_CREATE_ ## flag)) { \
  683. seq_printf((m), " " #flag); \
  684. }
  685. static int amdgpu_debugfs_gem_bo_info(int id, void *ptr, void *data)
  686. {
  687. struct drm_gem_object *gobj = ptr;
  688. struct amdgpu_bo *bo = gem_to_amdgpu_bo(gobj);
  689. struct seq_file *m = data;
  690. struct dma_buf_attachment *attachment;
  691. struct dma_buf *dma_buf;
  692. unsigned domain;
  693. const char *placement;
  694. unsigned pin_count;
  695. domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
  696. switch (domain) {
  697. case AMDGPU_GEM_DOMAIN_VRAM:
  698. placement = "VRAM";
  699. break;
  700. case AMDGPU_GEM_DOMAIN_GTT:
  701. placement = " GTT";
  702. break;
  703. case AMDGPU_GEM_DOMAIN_CPU:
  704. default:
  705. placement = " CPU";
  706. break;
  707. }
  708. seq_printf(m, "\t0x%08x: %12ld byte %s",
  709. id, amdgpu_bo_size(bo), placement);
  710. pin_count = READ_ONCE(bo->pin_count);
  711. if (pin_count)
  712. seq_printf(m, " pin count %d", pin_count);
  713. dma_buf = READ_ONCE(bo->gem_base.dma_buf);
  714. attachment = READ_ONCE(bo->gem_base.import_attach);
  715. if (attachment)
  716. seq_printf(m, " imported from %p", dma_buf);
  717. else if (dma_buf)
  718. seq_printf(m, " exported as %p", dma_buf);
  719. amdgpu_debugfs_gem_bo_print_flag(m, bo, CPU_ACCESS_REQUIRED);
  720. amdgpu_debugfs_gem_bo_print_flag(m, bo, NO_CPU_ACCESS);
  721. amdgpu_debugfs_gem_bo_print_flag(m, bo, CPU_GTT_USWC);
  722. amdgpu_debugfs_gem_bo_print_flag(m, bo, VRAM_CLEARED);
  723. amdgpu_debugfs_gem_bo_print_flag(m, bo, SHADOW);
  724. amdgpu_debugfs_gem_bo_print_flag(m, bo, VRAM_CONTIGUOUS);
  725. amdgpu_debugfs_gem_bo_print_flag(m, bo, VM_ALWAYS_VALID);
  726. amdgpu_debugfs_gem_bo_print_flag(m, bo, EXPLICIT_SYNC);
  727. seq_printf(m, "\n");
  728. return 0;
  729. }
  730. static int amdgpu_debugfs_gem_info(struct seq_file *m, void *data)
  731. {
  732. struct drm_info_node *node = (struct drm_info_node *)m->private;
  733. struct drm_device *dev = node->minor->dev;
  734. struct drm_file *file;
  735. int r;
  736. r = mutex_lock_interruptible(&dev->filelist_mutex);
  737. if (r)
  738. return r;
  739. list_for_each_entry(file, &dev->filelist, lhead) {
  740. struct task_struct *task;
  741. /*
  742. * Although we have a valid reference on file->pid, that does
  743. * not guarantee that the task_struct who called get_pid() is
  744. * still alive (e.g. get_pid(current) => fork() => exit()).
  745. * Therefore, we need to protect this ->comm access using RCU.
  746. */
  747. rcu_read_lock();
  748. task = pid_task(file->pid, PIDTYPE_PID);
  749. seq_printf(m, "pid %8d command %s:\n", pid_nr(file->pid),
  750. task ? task->comm : "<unknown>");
  751. rcu_read_unlock();
  752. spin_lock(&file->table_lock);
  753. idr_for_each(&file->object_idr, amdgpu_debugfs_gem_bo_info, m);
  754. spin_unlock(&file->table_lock);
  755. }
  756. mutex_unlock(&dev->filelist_mutex);
  757. return 0;
  758. }
  759. static const struct drm_info_list amdgpu_debugfs_gem_list[] = {
  760. {"amdgpu_gem_info", &amdgpu_debugfs_gem_info, 0, NULL},
  761. };
  762. #endif
  763. int amdgpu_debugfs_gem_init(struct amdgpu_device *adev)
  764. {
  765. #if defined(CONFIG_DEBUG_FS)
  766. return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_gem_list, 1);
  767. #endif
  768. return 0;
  769. }