amdgpu_gem.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  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 <drm/drmP.h>
  30. #include <drm/amdgpu_drm.h>
  31. #include "amdgpu.h"
  32. void amdgpu_gem_object_free(struct drm_gem_object *gobj)
  33. {
  34. struct amdgpu_bo *robj = gem_to_amdgpu_bo(gobj);
  35. if (robj) {
  36. if (robj->gem_base.import_attach)
  37. drm_prime_gem_destroy(&robj->gem_base, robj->tbo.sg);
  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, bool kernel,
  45. struct drm_gem_object **obj)
  46. {
  47. struct amdgpu_bo *robj;
  48. unsigned long max_size;
  49. int r;
  50. *obj = NULL;
  51. /* At least align on page size */
  52. if (alignment < PAGE_SIZE) {
  53. alignment = PAGE_SIZE;
  54. }
  55. if (!(initial_domain & (AMDGPU_GEM_DOMAIN_GDS | AMDGPU_GEM_DOMAIN_GWS | AMDGPU_GEM_DOMAIN_OA))) {
  56. /* Maximum bo size is the unpinned gtt size since we use the gtt to
  57. * handle vram to system pool migrations.
  58. */
  59. max_size = adev->mc.gtt_size - adev->gart_pin_size;
  60. if (size > max_size) {
  61. DRM_DEBUG("Allocation size %ldMb bigger than %ldMb limit\n",
  62. size >> 20, max_size >> 20);
  63. return -ENOMEM;
  64. }
  65. }
  66. retry:
  67. r = amdgpu_bo_create(adev, size, alignment, kernel, initial_domain,
  68. flags, NULL, NULL, &robj);
  69. if (r) {
  70. if (r != -ERESTARTSYS) {
  71. if (initial_domain == AMDGPU_GEM_DOMAIN_VRAM) {
  72. initial_domain |= AMDGPU_GEM_DOMAIN_GTT;
  73. goto retry;
  74. }
  75. DRM_ERROR("Failed to allocate GEM object (%ld, %d, %u, %d)\n",
  76. size, initial_domain, alignment, r);
  77. }
  78. return r;
  79. }
  80. *obj = &robj->gem_base;
  81. robj->pid = task_pid_nr(current);
  82. mutex_lock(&adev->gem.mutex);
  83. list_add_tail(&robj->list, &adev->gem.objects);
  84. mutex_unlock(&adev->gem.mutex);
  85. return 0;
  86. }
  87. int amdgpu_gem_init(struct amdgpu_device *adev)
  88. {
  89. INIT_LIST_HEAD(&adev->gem.objects);
  90. return 0;
  91. }
  92. void amdgpu_gem_fini(struct amdgpu_device *adev)
  93. {
  94. amdgpu_bo_force_delete(adev);
  95. }
  96. /*
  97. * Call from drm_gem_handle_create which appear in both new and open ioctl
  98. * case.
  99. */
  100. int amdgpu_gem_object_open(struct drm_gem_object *obj, struct drm_file *file_priv)
  101. {
  102. struct amdgpu_bo *rbo = gem_to_amdgpu_bo(obj);
  103. struct amdgpu_device *adev = rbo->adev;
  104. struct amdgpu_fpriv *fpriv = file_priv->driver_priv;
  105. struct amdgpu_vm *vm = &fpriv->vm;
  106. struct amdgpu_bo_va *bo_va;
  107. int r;
  108. r = amdgpu_bo_reserve(rbo, false);
  109. if (r)
  110. return r;
  111. bo_va = amdgpu_vm_bo_find(vm, rbo);
  112. if (!bo_va) {
  113. bo_va = amdgpu_vm_bo_add(adev, vm, rbo);
  114. } else {
  115. ++bo_va->ref_count;
  116. }
  117. amdgpu_bo_unreserve(rbo);
  118. return 0;
  119. }
  120. void amdgpu_gem_object_close(struct drm_gem_object *obj,
  121. struct drm_file *file_priv)
  122. {
  123. struct amdgpu_bo *rbo = gem_to_amdgpu_bo(obj);
  124. struct amdgpu_device *adev = rbo->adev;
  125. struct amdgpu_fpriv *fpriv = file_priv->driver_priv;
  126. struct amdgpu_vm *vm = &fpriv->vm;
  127. struct amdgpu_bo_va *bo_va;
  128. int r;
  129. r = amdgpu_bo_reserve(rbo, true);
  130. if (r) {
  131. dev_err(adev->dev, "leaking bo va because "
  132. "we fail to reserve bo (%d)\n", r);
  133. return;
  134. }
  135. bo_va = amdgpu_vm_bo_find(vm, rbo);
  136. if (bo_va) {
  137. if (--bo_va->ref_count == 0) {
  138. amdgpu_vm_bo_rmv(adev, bo_va);
  139. }
  140. }
  141. amdgpu_bo_unreserve(rbo);
  142. }
  143. static int amdgpu_gem_handle_lockup(struct amdgpu_device *adev, int r)
  144. {
  145. if (r == -EDEADLK) {
  146. r = amdgpu_gpu_reset(adev);
  147. if (!r)
  148. r = -EAGAIN;
  149. }
  150. return r;
  151. }
  152. /*
  153. * GEM ioctls.
  154. */
  155. int amdgpu_gem_create_ioctl(struct drm_device *dev, void *data,
  156. struct drm_file *filp)
  157. {
  158. struct amdgpu_device *adev = dev->dev_private;
  159. union drm_amdgpu_gem_create *args = data;
  160. uint64_t size = args->in.bo_size;
  161. struct drm_gem_object *gobj;
  162. uint32_t handle;
  163. bool kernel = false;
  164. int r;
  165. /* create a gem object to contain this object in */
  166. if (args->in.domains & (AMDGPU_GEM_DOMAIN_GDS |
  167. AMDGPU_GEM_DOMAIN_GWS | AMDGPU_GEM_DOMAIN_OA)) {
  168. kernel = true;
  169. if (args->in.domains == AMDGPU_GEM_DOMAIN_GDS)
  170. size = size << AMDGPU_GDS_SHIFT;
  171. else if (args->in.domains == AMDGPU_GEM_DOMAIN_GWS)
  172. size = size << AMDGPU_GWS_SHIFT;
  173. else if (args->in.domains == AMDGPU_GEM_DOMAIN_OA)
  174. size = size << AMDGPU_OA_SHIFT;
  175. else {
  176. r = -EINVAL;
  177. goto error_unlock;
  178. }
  179. }
  180. size = roundup(size, PAGE_SIZE);
  181. r = amdgpu_gem_object_create(adev, size, args->in.alignment,
  182. (u32)(0xffffffff & args->in.domains),
  183. args->in.domain_flags,
  184. kernel, &gobj);
  185. if (r)
  186. goto error_unlock;
  187. r = drm_gem_handle_create(filp, gobj, &handle);
  188. /* drop reference from allocate - handle holds it now */
  189. drm_gem_object_unreference_unlocked(gobj);
  190. if (r)
  191. goto error_unlock;
  192. memset(args, 0, sizeof(*args));
  193. args->out.handle = handle;
  194. return 0;
  195. error_unlock:
  196. r = amdgpu_gem_handle_lockup(adev, r);
  197. return r;
  198. }
  199. int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data,
  200. struct drm_file *filp)
  201. {
  202. struct amdgpu_device *adev = dev->dev_private;
  203. struct drm_amdgpu_gem_userptr *args = data;
  204. struct drm_gem_object *gobj;
  205. struct amdgpu_bo *bo;
  206. uint32_t handle;
  207. int r;
  208. if (offset_in_page(args->addr | args->size))
  209. return -EINVAL;
  210. /* reject unknown flag values */
  211. if (args->flags & ~(AMDGPU_GEM_USERPTR_READONLY |
  212. AMDGPU_GEM_USERPTR_ANONONLY | AMDGPU_GEM_USERPTR_VALIDATE |
  213. AMDGPU_GEM_USERPTR_REGISTER))
  214. return -EINVAL;
  215. if (!(args->flags & AMDGPU_GEM_USERPTR_READONLY) && (
  216. !(args->flags & AMDGPU_GEM_USERPTR_ANONONLY) ||
  217. !(args->flags & AMDGPU_GEM_USERPTR_REGISTER))) {
  218. /* if we want to write to it we must require anonymous
  219. memory and install a MMU notifier */
  220. return -EACCES;
  221. }
  222. /* create a gem object to contain this object in */
  223. r = amdgpu_gem_object_create(adev, args->size, 0,
  224. AMDGPU_GEM_DOMAIN_CPU, 0,
  225. 0, &gobj);
  226. if (r)
  227. goto handle_lockup;
  228. bo = gem_to_amdgpu_bo(gobj);
  229. r = amdgpu_ttm_tt_set_userptr(bo->tbo.ttm, args->addr, args->flags);
  230. if (r)
  231. goto release_object;
  232. if (args->flags & AMDGPU_GEM_USERPTR_REGISTER) {
  233. r = amdgpu_mn_register(bo, args->addr);
  234. if (r)
  235. goto release_object;
  236. }
  237. if (args->flags & AMDGPU_GEM_USERPTR_VALIDATE) {
  238. down_read(&current->mm->mmap_sem);
  239. r = amdgpu_bo_reserve(bo, true);
  240. if (r) {
  241. up_read(&current->mm->mmap_sem);
  242. goto release_object;
  243. }
  244. amdgpu_ttm_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_GTT);
  245. r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
  246. amdgpu_bo_unreserve(bo);
  247. up_read(&current->mm->mmap_sem);
  248. if (r)
  249. goto release_object;
  250. }
  251. r = drm_gem_handle_create(filp, gobj, &handle);
  252. /* drop reference from allocate - handle holds it now */
  253. drm_gem_object_unreference_unlocked(gobj);
  254. if (r)
  255. goto handle_lockup;
  256. args->handle = handle;
  257. return 0;
  258. release_object:
  259. drm_gem_object_unreference_unlocked(gobj);
  260. handle_lockup:
  261. r = amdgpu_gem_handle_lockup(adev, r);
  262. return r;
  263. }
  264. int amdgpu_mode_dumb_mmap(struct drm_file *filp,
  265. struct drm_device *dev,
  266. uint32_t handle, uint64_t *offset_p)
  267. {
  268. struct drm_gem_object *gobj;
  269. struct amdgpu_bo *robj;
  270. gobj = drm_gem_object_lookup(dev, filp, handle);
  271. if (gobj == NULL) {
  272. return -ENOENT;
  273. }
  274. robj = gem_to_amdgpu_bo(gobj);
  275. if (amdgpu_ttm_tt_has_userptr(robj->tbo.ttm) ||
  276. (robj->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)) {
  277. drm_gem_object_unreference_unlocked(gobj);
  278. return -EPERM;
  279. }
  280. *offset_p = amdgpu_bo_mmap_offset(robj);
  281. drm_gem_object_unreference_unlocked(gobj);
  282. return 0;
  283. }
  284. int amdgpu_gem_mmap_ioctl(struct drm_device *dev, void *data,
  285. struct drm_file *filp)
  286. {
  287. union drm_amdgpu_gem_mmap *args = data;
  288. uint32_t handle = args->in.handle;
  289. memset(args, 0, sizeof(*args));
  290. return amdgpu_mode_dumb_mmap(filp, dev, handle, &args->out.addr_ptr);
  291. }
  292. /**
  293. * amdgpu_gem_timeout - calculate jiffies timeout from absolute value
  294. *
  295. * @timeout_ns: timeout in ns
  296. *
  297. * Calculate the timeout in jiffies from an absolute timeout in ns.
  298. */
  299. unsigned long amdgpu_gem_timeout(uint64_t timeout_ns)
  300. {
  301. unsigned long timeout_jiffies;
  302. ktime_t timeout;
  303. /* clamp timeout if it's to large */
  304. if (((int64_t)timeout_ns) < 0)
  305. return MAX_SCHEDULE_TIMEOUT;
  306. timeout = ktime_sub(ns_to_ktime(timeout_ns), ktime_get());
  307. if (ktime_to_ns(timeout) < 0)
  308. return 0;
  309. timeout_jiffies = nsecs_to_jiffies(ktime_to_ns(timeout));
  310. /* clamp timeout to avoid unsigned-> signed overflow */
  311. if (timeout_jiffies > MAX_SCHEDULE_TIMEOUT )
  312. return MAX_SCHEDULE_TIMEOUT - 1;
  313. return timeout_jiffies;
  314. }
  315. int amdgpu_gem_wait_idle_ioctl(struct drm_device *dev, void *data,
  316. struct drm_file *filp)
  317. {
  318. struct amdgpu_device *adev = dev->dev_private;
  319. union drm_amdgpu_gem_wait_idle *args = data;
  320. struct drm_gem_object *gobj;
  321. struct amdgpu_bo *robj;
  322. uint32_t handle = args->in.handle;
  323. unsigned long timeout = amdgpu_gem_timeout(args->in.timeout);
  324. int r = 0;
  325. long ret;
  326. gobj = drm_gem_object_lookup(dev, filp, handle);
  327. if (gobj == NULL) {
  328. return -ENOENT;
  329. }
  330. robj = gem_to_amdgpu_bo(gobj);
  331. if (timeout == 0)
  332. ret = reservation_object_test_signaled_rcu(robj->tbo.resv, true);
  333. else
  334. ret = reservation_object_wait_timeout_rcu(robj->tbo.resv, true, true, timeout);
  335. /* ret == 0 means not signaled,
  336. * ret > 0 means signaled
  337. * ret < 0 means interrupted before timeout
  338. */
  339. if (ret >= 0) {
  340. memset(args, 0, sizeof(*args));
  341. args->out.status = (ret == 0);
  342. } else
  343. r = ret;
  344. drm_gem_object_unreference_unlocked(gobj);
  345. r = amdgpu_gem_handle_lockup(adev, r);
  346. return r;
  347. }
  348. int amdgpu_gem_metadata_ioctl(struct drm_device *dev, void *data,
  349. struct drm_file *filp)
  350. {
  351. struct drm_amdgpu_gem_metadata *args = data;
  352. struct drm_gem_object *gobj;
  353. struct amdgpu_bo *robj;
  354. int r = -1;
  355. DRM_DEBUG("%d \n", args->handle);
  356. gobj = drm_gem_object_lookup(dev, filp, args->handle);
  357. if (gobj == NULL)
  358. return -ENOENT;
  359. robj = gem_to_amdgpu_bo(gobj);
  360. r = amdgpu_bo_reserve(robj, false);
  361. if (unlikely(r != 0))
  362. goto out;
  363. if (args->op == AMDGPU_GEM_METADATA_OP_GET_METADATA) {
  364. amdgpu_bo_get_tiling_flags(robj, &args->data.tiling_info);
  365. r = amdgpu_bo_get_metadata(robj, args->data.data,
  366. sizeof(args->data.data),
  367. &args->data.data_size_bytes,
  368. &args->data.flags);
  369. } else if (args->op == AMDGPU_GEM_METADATA_OP_SET_METADATA) {
  370. if (args->data.data_size_bytes > sizeof(args->data.data)) {
  371. r = -EINVAL;
  372. goto unreserve;
  373. }
  374. r = amdgpu_bo_set_tiling_flags(robj, args->data.tiling_info);
  375. if (!r)
  376. r = amdgpu_bo_set_metadata(robj, args->data.data,
  377. args->data.data_size_bytes,
  378. args->data.flags);
  379. }
  380. unreserve:
  381. amdgpu_bo_unreserve(robj);
  382. out:
  383. drm_gem_object_unreference_unlocked(gobj);
  384. return r;
  385. }
  386. /**
  387. * amdgpu_gem_va_update_vm -update the bo_va in its VM
  388. *
  389. * @adev: amdgpu_device pointer
  390. * @bo_va: bo_va to update
  391. *
  392. * Update the bo_va directly after setting it's address. Errors are not
  393. * vital here, so they are not reported back to userspace.
  394. */
  395. static void amdgpu_gem_va_update_vm(struct amdgpu_device *adev,
  396. struct amdgpu_bo_va *bo_va, uint32_t operation)
  397. {
  398. struct ttm_validate_buffer tv, *entry;
  399. struct amdgpu_bo_list_entry *vm_bos;
  400. struct ww_acquire_ctx ticket;
  401. struct list_head list, duplicates;
  402. unsigned domain;
  403. int r;
  404. INIT_LIST_HEAD(&list);
  405. INIT_LIST_HEAD(&duplicates);
  406. tv.bo = &bo_va->bo->tbo;
  407. tv.shared = true;
  408. list_add(&tv.head, &list);
  409. vm_bos = amdgpu_vm_get_bos(adev, bo_va->vm, &list);
  410. if (!vm_bos)
  411. return;
  412. /* Provide duplicates to avoid -EALREADY */
  413. r = ttm_eu_reserve_buffers(&ticket, &list, true, &duplicates);
  414. if (r)
  415. goto error_free;
  416. list_for_each_entry(entry, &list, head) {
  417. domain = amdgpu_mem_type_to_domain(entry->bo->mem.mem_type);
  418. /* if anything is swapped out don't swap it in here,
  419. just abort and wait for the next CS */
  420. if (domain == AMDGPU_GEM_DOMAIN_CPU)
  421. goto error_unreserve;
  422. }
  423. list_for_each_entry(entry, &duplicates, head) {
  424. domain = amdgpu_mem_type_to_domain(entry->bo->mem.mem_type);
  425. /* if anything is swapped out don't swap it in here,
  426. just abort and wait for the next CS */
  427. if (domain == AMDGPU_GEM_DOMAIN_CPU)
  428. goto error_unreserve;
  429. }
  430. r = amdgpu_vm_update_page_directory(adev, bo_va->vm);
  431. if (r)
  432. goto error_unreserve;
  433. r = amdgpu_vm_clear_freed(adev, bo_va->vm);
  434. if (r)
  435. goto error_unreserve;
  436. if (operation == AMDGPU_VA_OP_MAP)
  437. r = amdgpu_vm_bo_update(adev, bo_va, &bo_va->bo->tbo.mem);
  438. error_unreserve:
  439. ttm_eu_backoff_reservation(&ticket, &list);
  440. error_free:
  441. drm_free_large(vm_bos);
  442. if (r && r != -ERESTARTSYS)
  443. DRM_ERROR("Couldn't update BO_VA (%d)\n", r);
  444. }
  445. int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data,
  446. struct drm_file *filp)
  447. {
  448. struct drm_amdgpu_gem_va *args = data;
  449. struct drm_gem_object *gobj;
  450. struct amdgpu_device *adev = dev->dev_private;
  451. struct amdgpu_fpriv *fpriv = filp->driver_priv;
  452. struct amdgpu_bo *rbo;
  453. struct amdgpu_bo_va *bo_va;
  454. struct ttm_validate_buffer tv, tv_pd;
  455. struct ww_acquire_ctx ticket;
  456. struct list_head list, duplicates;
  457. uint32_t invalid_flags, va_flags = 0;
  458. int r = 0;
  459. if (!adev->vm_manager.enabled)
  460. return -ENOTTY;
  461. if (args->va_address < AMDGPU_VA_RESERVED_SIZE) {
  462. dev_err(&dev->pdev->dev,
  463. "va_address 0x%lX is in reserved area 0x%X\n",
  464. (unsigned long)args->va_address,
  465. AMDGPU_VA_RESERVED_SIZE);
  466. return -EINVAL;
  467. }
  468. invalid_flags = ~(AMDGPU_VM_DELAY_UPDATE | AMDGPU_VM_PAGE_READABLE |
  469. AMDGPU_VM_PAGE_WRITEABLE | AMDGPU_VM_PAGE_EXECUTABLE);
  470. if ((args->flags & invalid_flags)) {
  471. dev_err(&dev->pdev->dev, "invalid flags 0x%08X vs 0x%08X\n",
  472. args->flags, invalid_flags);
  473. return -EINVAL;
  474. }
  475. switch (args->operation) {
  476. case AMDGPU_VA_OP_MAP:
  477. case AMDGPU_VA_OP_UNMAP:
  478. break;
  479. default:
  480. dev_err(&dev->pdev->dev, "unsupported operation %d\n",
  481. args->operation);
  482. return -EINVAL;
  483. }
  484. gobj = drm_gem_object_lookup(dev, filp, args->handle);
  485. if (gobj == NULL)
  486. return -ENOENT;
  487. rbo = gem_to_amdgpu_bo(gobj);
  488. INIT_LIST_HEAD(&list);
  489. INIT_LIST_HEAD(&duplicates);
  490. tv.bo = &rbo->tbo;
  491. tv.shared = true;
  492. list_add(&tv.head, &list);
  493. if (args->operation == AMDGPU_VA_OP_MAP) {
  494. tv_pd.bo = &fpriv->vm.page_directory->tbo;
  495. tv_pd.shared = true;
  496. list_add(&tv_pd.head, &list);
  497. }
  498. r = ttm_eu_reserve_buffers(&ticket, &list, true, &duplicates);
  499. if (r) {
  500. drm_gem_object_unreference_unlocked(gobj);
  501. return r;
  502. }
  503. bo_va = amdgpu_vm_bo_find(&fpriv->vm, rbo);
  504. if (!bo_va) {
  505. ttm_eu_backoff_reservation(&ticket, &list);
  506. drm_gem_object_unreference_unlocked(gobj);
  507. return -ENOENT;
  508. }
  509. switch (args->operation) {
  510. case AMDGPU_VA_OP_MAP:
  511. if (args->flags & AMDGPU_VM_PAGE_READABLE)
  512. va_flags |= AMDGPU_PTE_READABLE;
  513. if (args->flags & AMDGPU_VM_PAGE_WRITEABLE)
  514. va_flags |= AMDGPU_PTE_WRITEABLE;
  515. if (args->flags & AMDGPU_VM_PAGE_EXECUTABLE)
  516. va_flags |= AMDGPU_PTE_EXECUTABLE;
  517. r = amdgpu_vm_bo_map(adev, bo_va, args->va_address,
  518. args->offset_in_bo, args->map_size,
  519. va_flags);
  520. break;
  521. case AMDGPU_VA_OP_UNMAP:
  522. r = amdgpu_vm_bo_unmap(adev, bo_va, args->va_address);
  523. break;
  524. default:
  525. break;
  526. }
  527. ttm_eu_backoff_reservation(&ticket, &list);
  528. if (!r && !(args->flags & AMDGPU_VM_DELAY_UPDATE))
  529. amdgpu_gem_va_update_vm(adev, bo_va, args->operation);
  530. drm_gem_object_unreference_unlocked(gobj);
  531. return r;
  532. }
  533. int amdgpu_gem_op_ioctl(struct drm_device *dev, void *data,
  534. struct drm_file *filp)
  535. {
  536. struct drm_amdgpu_gem_op *args = data;
  537. struct drm_gem_object *gobj;
  538. struct amdgpu_bo *robj;
  539. int r;
  540. gobj = drm_gem_object_lookup(dev, filp, args->handle);
  541. if (gobj == NULL) {
  542. return -ENOENT;
  543. }
  544. robj = gem_to_amdgpu_bo(gobj);
  545. r = amdgpu_bo_reserve(robj, false);
  546. if (unlikely(r))
  547. goto out;
  548. switch (args->op) {
  549. case AMDGPU_GEM_OP_GET_GEM_CREATE_INFO: {
  550. struct drm_amdgpu_gem_create_in info;
  551. void __user *out = (void __user *)(long)args->value;
  552. info.bo_size = robj->gem_base.size;
  553. info.alignment = robj->tbo.mem.page_alignment << PAGE_SHIFT;
  554. info.domains = robj->initial_domain;
  555. info.domain_flags = robj->flags;
  556. amdgpu_bo_unreserve(robj);
  557. if (copy_to_user(out, &info, sizeof(info)))
  558. r = -EFAULT;
  559. break;
  560. }
  561. case AMDGPU_GEM_OP_SET_PLACEMENT:
  562. if (amdgpu_ttm_tt_has_userptr(robj->tbo.ttm)) {
  563. r = -EPERM;
  564. amdgpu_bo_unreserve(robj);
  565. break;
  566. }
  567. robj->initial_domain = args->value & (AMDGPU_GEM_DOMAIN_VRAM |
  568. AMDGPU_GEM_DOMAIN_GTT |
  569. AMDGPU_GEM_DOMAIN_CPU);
  570. amdgpu_bo_unreserve(robj);
  571. break;
  572. default:
  573. amdgpu_bo_unreserve(robj);
  574. r = -EINVAL;
  575. }
  576. out:
  577. drm_gem_object_unreference_unlocked(gobj);
  578. return r;
  579. }
  580. int amdgpu_mode_dumb_create(struct drm_file *file_priv,
  581. struct drm_device *dev,
  582. struct drm_mode_create_dumb *args)
  583. {
  584. struct amdgpu_device *adev = dev->dev_private;
  585. struct drm_gem_object *gobj;
  586. uint32_t handle;
  587. int r;
  588. args->pitch = amdgpu_align_pitch(adev, args->width, args->bpp, 0) * ((args->bpp + 1) / 8);
  589. args->size = (u64)args->pitch * args->height;
  590. args->size = ALIGN(args->size, PAGE_SIZE);
  591. r = amdgpu_gem_object_create(adev, args->size, 0,
  592. AMDGPU_GEM_DOMAIN_VRAM,
  593. AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED,
  594. ttm_bo_type_device,
  595. &gobj);
  596. if (r)
  597. return -ENOMEM;
  598. r = drm_gem_handle_create(file_priv, gobj, &handle);
  599. /* drop reference from allocate - handle holds it now */
  600. drm_gem_object_unreference_unlocked(gobj);
  601. if (r) {
  602. return r;
  603. }
  604. args->handle = handle;
  605. return 0;
  606. }
  607. #if defined(CONFIG_DEBUG_FS)
  608. static int amdgpu_debugfs_gem_info(struct seq_file *m, void *data)
  609. {
  610. struct drm_info_node *node = (struct drm_info_node *)m->private;
  611. struct drm_device *dev = node->minor->dev;
  612. struct amdgpu_device *adev = dev->dev_private;
  613. struct amdgpu_bo *rbo;
  614. unsigned i = 0;
  615. mutex_lock(&adev->gem.mutex);
  616. list_for_each_entry(rbo, &adev->gem.objects, list) {
  617. unsigned domain;
  618. const char *placement;
  619. domain = amdgpu_mem_type_to_domain(rbo->tbo.mem.mem_type);
  620. switch (domain) {
  621. case AMDGPU_GEM_DOMAIN_VRAM:
  622. placement = "VRAM";
  623. break;
  624. case AMDGPU_GEM_DOMAIN_GTT:
  625. placement = " GTT";
  626. break;
  627. case AMDGPU_GEM_DOMAIN_CPU:
  628. default:
  629. placement = " CPU";
  630. break;
  631. }
  632. seq_printf(m, "bo[0x%08x] %8ldkB %8ldMB %s pid %8ld\n",
  633. i, amdgpu_bo_size(rbo) >> 10, amdgpu_bo_size(rbo) >> 20,
  634. placement, (unsigned long)rbo->pid);
  635. i++;
  636. }
  637. mutex_unlock(&adev->gem.mutex);
  638. return 0;
  639. }
  640. static struct drm_info_list amdgpu_debugfs_gem_list[] = {
  641. {"amdgpu_gem_info", &amdgpu_debugfs_gem_info, 0, NULL},
  642. };
  643. #endif
  644. int amdgpu_gem_debugfs_init(struct amdgpu_device *adev)
  645. {
  646. #if defined(CONFIG_DEBUG_FS)
  647. return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_gem_list, 1);
  648. #endif
  649. return 0;
  650. }