amdgpu_gem.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  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. flags |= AMDGPU_GEM_CREATE_NO_CPU_ACCESS;
  209. if (args->in.domains == AMDGPU_GEM_DOMAIN_GDS)
  210. size = size << AMDGPU_GDS_SHIFT;
  211. else if (args->in.domains == AMDGPU_GEM_DOMAIN_GWS)
  212. size = size << AMDGPU_GWS_SHIFT;
  213. else if (args->in.domains == AMDGPU_GEM_DOMAIN_OA)
  214. size = size << AMDGPU_OA_SHIFT;
  215. else
  216. return -EINVAL;
  217. }
  218. size = roundup(size, PAGE_SIZE);
  219. if (flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID) {
  220. r = amdgpu_bo_reserve(vm->root.base.bo, false);
  221. if (r)
  222. return r;
  223. resv = vm->root.base.bo->tbo.resv;
  224. }
  225. r = amdgpu_gem_object_create(adev, size, args->in.alignment,
  226. (u32)(0xffffffff & args->in.domains),
  227. flags, false, resv, &gobj);
  228. if (flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID) {
  229. if (!r) {
  230. struct amdgpu_bo *abo = gem_to_amdgpu_bo(gobj);
  231. abo->parent = amdgpu_bo_ref(vm->root.base.bo);
  232. }
  233. amdgpu_bo_unreserve(vm->root.base.bo);
  234. }
  235. if (r)
  236. return r;
  237. r = drm_gem_handle_create(filp, gobj, &handle);
  238. /* drop reference from allocate - handle holds it now */
  239. drm_gem_object_put_unlocked(gobj);
  240. if (r)
  241. return r;
  242. memset(args, 0, sizeof(*args));
  243. args->out.handle = handle;
  244. return 0;
  245. }
  246. int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data,
  247. struct drm_file *filp)
  248. {
  249. struct ttm_operation_ctx ctx = { true, false };
  250. struct amdgpu_device *adev = dev->dev_private;
  251. struct drm_amdgpu_gem_userptr *args = data;
  252. struct drm_gem_object *gobj;
  253. struct amdgpu_bo *bo;
  254. uint32_t handle;
  255. int r;
  256. if (offset_in_page(args->addr | args->size))
  257. return -EINVAL;
  258. /* reject unknown flag values */
  259. if (args->flags & ~(AMDGPU_GEM_USERPTR_READONLY |
  260. AMDGPU_GEM_USERPTR_ANONONLY | AMDGPU_GEM_USERPTR_VALIDATE |
  261. AMDGPU_GEM_USERPTR_REGISTER))
  262. return -EINVAL;
  263. if (!(args->flags & AMDGPU_GEM_USERPTR_READONLY) &&
  264. !(args->flags & AMDGPU_GEM_USERPTR_REGISTER)) {
  265. /* if we want to write to it we must install a MMU notifier */
  266. return -EACCES;
  267. }
  268. /* create a gem object to contain this object in */
  269. r = amdgpu_gem_object_create(adev, args->size, 0, AMDGPU_GEM_DOMAIN_CPU,
  270. 0, 0, NULL, &gobj);
  271. if (r)
  272. return r;
  273. bo = gem_to_amdgpu_bo(gobj);
  274. bo->preferred_domains = AMDGPU_GEM_DOMAIN_GTT;
  275. bo->allowed_domains = AMDGPU_GEM_DOMAIN_GTT;
  276. r = amdgpu_ttm_tt_set_userptr(bo->tbo.ttm, args->addr, args->flags);
  277. if (r)
  278. goto release_object;
  279. if (args->flags & AMDGPU_GEM_USERPTR_REGISTER) {
  280. r = amdgpu_mn_register(bo, args->addr);
  281. if (r)
  282. goto release_object;
  283. }
  284. if (args->flags & AMDGPU_GEM_USERPTR_VALIDATE) {
  285. r = amdgpu_ttm_tt_get_user_pages(bo->tbo.ttm,
  286. bo->tbo.ttm->pages);
  287. if (r)
  288. goto release_object;
  289. r = amdgpu_bo_reserve(bo, true);
  290. if (r)
  291. goto free_pages;
  292. amdgpu_ttm_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_GTT);
  293. r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
  294. amdgpu_bo_unreserve(bo);
  295. if (r)
  296. goto free_pages;
  297. }
  298. r = drm_gem_handle_create(filp, gobj, &handle);
  299. /* drop reference from allocate - handle holds it now */
  300. drm_gem_object_put_unlocked(gobj);
  301. if (r)
  302. return r;
  303. args->handle = handle;
  304. return 0;
  305. free_pages:
  306. release_pages(bo->tbo.ttm->pages, bo->tbo.ttm->num_pages);
  307. release_object:
  308. drm_gem_object_put_unlocked(gobj);
  309. return r;
  310. }
  311. int amdgpu_mode_dumb_mmap(struct drm_file *filp,
  312. struct drm_device *dev,
  313. uint32_t handle, uint64_t *offset_p)
  314. {
  315. struct drm_gem_object *gobj;
  316. struct amdgpu_bo *robj;
  317. gobj = drm_gem_object_lookup(filp, handle);
  318. if (gobj == NULL) {
  319. return -ENOENT;
  320. }
  321. robj = gem_to_amdgpu_bo(gobj);
  322. if (amdgpu_ttm_tt_get_usermm(robj->tbo.ttm) ||
  323. (robj->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)) {
  324. drm_gem_object_put_unlocked(gobj);
  325. return -EPERM;
  326. }
  327. *offset_p = amdgpu_bo_mmap_offset(robj);
  328. drm_gem_object_put_unlocked(gobj);
  329. return 0;
  330. }
  331. int amdgpu_gem_mmap_ioctl(struct drm_device *dev, void *data,
  332. struct drm_file *filp)
  333. {
  334. union drm_amdgpu_gem_mmap *args = data;
  335. uint32_t handle = args->in.handle;
  336. memset(args, 0, sizeof(*args));
  337. return amdgpu_mode_dumb_mmap(filp, dev, handle, &args->out.addr_ptr);
  338. }
  339. /**
  340. * amdgpu_gem_timeout - calculate jiffies timeout from absolute value
  341. *
  342. * @timeout_ns: timeout in ns
  343. *
  344. * Calculate the timeout in jiffies from an absolute timeout in ns.
  345. */
  346. unsigned long amdgpu_gem_timeout(uint64_t timeout_ns)
  347. {
  348. unsigned long timeout_jiffies;
  349. ktime_t timeout;
  350. /* clamp timeout if it's to large */
  351. if (((int64_t)timeout_ns) < 0)
  352. return MAX_SCHEDULE_TIMEOUT;
  353. timeout = ktime_sub(ns_to_ktime(timeout_ns), ktime_get());
  354. if (ktime_to_ns(timeout) < 0)
  355. return 0;
  356. timeout_jiffies = nsecs_to_jiffies(ktime_to_ns(timeout));
  357. /* clamp timeout to avoid unsigned-> signed overflow */
  358. if (timeout_jiffies > MAX_SCHEDULE_TIMEOUT )
  359. return MAX_SCHEDULE_TIMEOUT - 1;
  360. return timeout_jiffies;
  361. }
  362. int amdgpu_gem_wait_idle_ioctl(struct drm_device *dev, void *data,
  363. struct drm_file *filp)
  364. {
  365. union drm_amdgpu_gem_wait_idle *args = data;
  366. struct drm_gem_object *gobj;
  367. struct amdgpu_bo *robj;
  368. uint32_t handle = args->in.handle;
  369. unsigned long timeout = amdgpu_gem_timeout(args->in.timeout);
  370. int r = 0;
  371. long ret;
  372. gobj = drm_gem_object_lookup(filp, handle);
  373. if (gobj == NULL) {
  374. return -ENOENT;
  375. }
  376. robj = gem_to_amdgpu_bo(gobj);
  377. ret = reservation_object_wait_timeout_rcu(robj->tbo.resv, true, true,
  378. timeout);
  379. /* ret == 0 means not signaled,
  380. * ret > 0 means signaled
  381. * ret < 0 means interrupted before timeout
  382. */
  383. if (ret >= 0) {
  384. memset(args, 0, sizeof(*args));
  385. args->out.status = (ret == 0);
  386. } else
  387. r = ret;
  388. drm_gem_object_put_unlocked(gobj);
  389. return r;
  390. }
  391. int amdgpu_gem_metadata_ioctl(struct drm_device *dev, void *data,
  392. struct drm_file *filp)
  393. {
  394. struct drm_amdgpu_gem_metadata *args = data;
  395. struct drm_gem_object *gobj;
  396. struct amdgpu_bo *robj;
  397. int r = -1;
  398. DRM_DEBUG("%d \n", args->handle);
  399. gobj = drm_gem_object_lookup(filp, args->handle);
  400. if (gobj == NULL)
  401. return -ENOENT;
  402. robj = gem_to_amdgpu_bo(gobj);
  403. r = amdgpu_bo_reserve(robj, false);
  404. if (unlikely(r != 0))
  405. goto out;
  406. if (args->op == AMDGPU_GEM_METADATA_OP_GET_METADATA) {
  407. amdgpu_bo_get_tiling_flags(robj, &args->data.tiling_info);
  408. r = amdgpu_bo_get_metadata(robj, args->data.data,
  409. sizeof(args->data.data),
  410. &args->data.data_size_bytes,
  411. &args->data.flags);
  412. } else if (args->op == AMDGPU_GEM_METADATA_OP_SET_METADATA) {
  413. if (args->data.data_size_bytes > sizeof(args->data.data)) {
  414. r = -EINVAL;
  415. goto unreserve;
  416. }
  417. r = amdgpu_bo_set_tiling_flags(robj, args->data.tiling_info);
  418. if (!r)
  419. r = amdgpu_bo_set_metadata(robj, args->data.data,
  420. args->data.data_size_bytes,
  421. args->data.flags);
  422. }
  423. unreserve:
  424. amdgpu_bo_unreserve(robj);
  425. out:
  426. drm_gem_object_put_unlocked(gobj);
  427. return r;
  428. }
  429. /**
  430. * amdgpu_gem_va_update_vm -update the bo_va in its VM
  431. *
  432. * @adev: amdgpu_device pointer
  433. * @vm: vm to update
  434. * @bo_va: bo_va to update
  435. * @list: validation list
  436. * @operation: map, unmap or clear
  437. *
  438. * Update the bo_va directly after setting its address. Errors are not
  439. * vital here, so they are not reported back to userspace.
  440. */
  441. static void amdgpu_gem_va_update_vm(struct amdgpu_device *adev,
  442. struct amdgpu_vm *vm,
  443. struct amdgpu_bo_va *bo_va,
  444. struct list_head *list,
  445. uint32_t operation)
  446. {
  447. int r;
  448. if (!amdgpu_vm_ready(vm))
  449. return;
  450. r = amdgpu_vm_clear_freed(adev, vm, NULL);
  451. if (r)
  452. goto error;
  453. if (operation == AMDGPU_VA_OP_MAP ||
  454. operation == AMDGPU_VA_OP_REPLACE) {
  455. r = amdgpu_vm_bo_update(adev, bo_va, false);
  456. if (r)
  457. goto error;
  458. }
  459. r = amdgpu_vm_update_directories(adev, vm);
  460. error:
  461. if (r && r != -ERESTARTSYS)
  462. DRM_ERROR("Couldn't update BO_VA (%d)\n", r);
  463. }
  464. int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data,
  465. struct drm_file *filp)
  466. {
  467. const uint32_t valid_flags = AMDGPU_VM_DELAY_UPDATE |
  468. AMDGPU_VM_PAGE_READABLE | AMDGPU_VM_PAGE_WRITEABLE |
  469. AMDGPU_VM_PAGE_EXECUTABLE | AMDGPU_VM_MTYPE_MASK;
  470. const uint32_t prt_flags = AMDGPU_VM_DELAY_UPDATE |
  471. AMDGPU_VM_PAGE_PRT;
  472. struct drm_amdgpu_gem_va *args = data;
  473. struct drm_gem_object *gobj;
  474. struct amdgpu_device *adev = dev->dev_private;
  475. struct amdgpu_fpriv *fpriv = filp->driver_priv;
  476. struct amdgpu_bo *abo;
  477. struct amdgpu_bo_va *bo_va;
  478. struct amdgpu_bo_list_entry vm_pd;
  479. struct ttm_validate_buffer tv;
  480. struct ww_acquire_ctx ticket;
  481. struct list_head list, duplicates;
  482. uint64_t va_flags;
  483. int r = 0;
  484. if (args->va_address < AMDGPU_VA_RESERVED_SIZE) {
  485. dev_dbg(&dev->pdev->dev,
  486. "va_address 0x%LX is in reserved area 0x%LX\n",
  487. args->va_address, AMDGPU_VA_RESERVED_SIZE);
  488. return -EINVAL;
  489. }
  490. if (args->va_address >= AMDGPU_VA_HOLE_START &&
  491. args->va_address < AMDGPU_VA_HOLE_END) {
  492. dev_dbg(&dev->pdev->dev,
  493. "va_address 0x%LX is in VA hole 0x%LX-0x%LX\n",
  494. args->va_address, AMDGPU_VA_HOLE_START,
  495. AMDGPU_VA_HOLE_END);
  496. return -EINVAL;
  497. }
  498. args->va_address &= AMDGPU_VA_HOLE_MASK;
  499. if ((args->flags & ~valid_flags) && (args->flags & ~prt_flags)) {
  500. dev_dbg(&dev->pdev->dev, "invalid flags combination 0x%08X\n",
  501. args->flags);
  502. return -EINVAL;
  503. }
  504. switch (args->operation) {
  505. case AMDGPU_VA_OP_MAP:
  506. case AMDGPU_VA_OP_UNMAP:
  507. case AMDGPU_VA_OP_CLEAR:
  508. case AMDGPU_VA_OP_REPLACE:
  509. break;
  510. default:
  511. dev_dbg(&dev->pdev->dev, "unsupported operation %d\n",
  512. args->operation);
  513. return -EINVAL;
  514. }
  515. INIT_LIST_HEAD(&list);
  516. INIT_LIST_HEAD(&duplicates);
  517. if ((args->operation != AMDGPU_VA_OP_CLEAR) &&
  518. !(args->flags & AMDGPU_VM_PAGE_PRT)) {
  519. gobj = drm_gem_object_lookup(filp, args->handle);
  520. if (gobj == NULL)
  521. return -ENOENT;
  522. abo = gem_to_amdgpu_bo(gobj);
  523. tv.bo = &abo->tbo;
  524. tv.shared = false;
  525. list_add(&tv.head, &list);
  526. } else {
  527. gobj = NULL;
  528. abo = NULL;
  529. }
  530. amdgpu_vm_get_pd_bo(&fpriv->vm, &list, &vm_pd);
  531. r = ttm_eu_reserve_buffers(&ticket, &list, true, &duplicates);
  532. if (r)
  533. goto error_unref;
  534. if (abo) {
  535. bo_va = amdgpu_vm_bo_find(&fpriv->vm, abo);
  536. if (!bo_va) {
  537. r = -ENOENT;
  538. goto error_backoff;
  539. }
  540. } else if (args->operation != AMDGPU_VA_OP_CLEAR) {
  541. bo_va = fpriv->prt_va;
  542. } else {
  543. bo_va = NULL;
  544. }
  545. switch (args->operation) {
  546. case AMDGPU_VA_OP_MAP:
  547. r = amdgpu_vm_alloc_pts(adev, bo_va->base.vm, args->va_address,
  548. args->map_size);
  549. if (r)
  550. goto error_backoff;
  551. va_flags = amdgpu_gmc_get_pte_flags(adev, args->flags);
  552. r = amdgpu_vm_bo_map(adev, bo_va, args->va_address,
  553. args->offset_in_bo, args->map_size,
  554. va_flags);
  555. break;
  556. case AMDGPU_VA_OP_UNMAP:
  557. r = amdgpu_vm_bo_unmap(adev, bo_va, args->va_address);
  558. break;
  559. case AMDGPU_VA_OP_CLEAR:
  560. r = amdgpu_vm_bo_clear_mappings(adev, &fpriv->vm,
  561. args->va_address,
  562. args->map_size);
  563. break;
  564. case AMDGPU_VA_OP_REPLACE:
  565. r = amdgpu_vm_alloc_pts(adev, bo_va->base.vm, args->va_address,
  566. args->map_size);
  567. if (r)
  568. goto error_backoff;
  569. va_flags = amdgpu_gmc_get_pte_flags(adev, args->flags);
  570. r = amdgpu_vm_bo_replace_map(adev, bo_va, args->va_address,
  571. args->offset_in_bo, args->map_size,
  572. va_flags);
  573. break;
  574. default:
  575. break;
  576. }
  577. if (!r && !(args->flags & AMDGPU_VM_DELAY_UPDATE) && !amdgpu_vm_debug)
  578. amdgpu_gem_va_update_vm(adev, &fpriv->vm, bo_va, &list,
  579. args->operation);
  580. error_backoff:
  581. ttm_eu_backoff_reservation(&ticket, &list);
  582. error_unref:
  583. drm_gem_object_put_unlocked(gobj);
  584. return r;
  585. }
  586. int amdgpu_gem_op_ioctl(struct drm_device *dev, void *data,
  587. struct drm_file *filp)
  588. {
  589. struct amdgpu_device *adev = dev->dev_private;
  590. struct drm_amdgpu_gem_op *args = data;
  591. struct drm_gem_object *gobj;
  592. struct amdgpu_bo *robj;
  593. int r;
  594. gobj = drm_gem_object_lookup(filp, args->handle);
  595. if (gobj == NULL) {
  596. return -ENOENT;
  597. }
  598. robj = gem_to_amdgpu_bo(gobj);
  599. r = amdgpu_bo_reserve(robj, false);
  600. if (unlikely(r))
  601. goto out;
  602. switch (args->op) {
  603. case AMDGPU_GEM_OP_GET_GEM_CREATE_INFO: {
  604. struct drm_amdgpu_gem_create_in info;
  605. void __user *out = u64_to_user_ptr(args->value);
  606. info.bo_size = robj->gem_base.size;
  607. info.alignment = robj->tbo.mem.page_alignment << PAGE_SHIFT;
  608. info.domains = robj->preferred_domains;
  609. info.domain_flags = robj->flags;
  610. amdgpu_bo_unreserve(robj);
  611. if (copy_to_user(out, &info, sizeof(info)))
  612. r = -EFAULT;
  613. break;
  614. }
  615. case AMDGPU_GEM_OP_SET_PLACEMENT:
  616. if (robj->prime_shared_count && (args->value & AMDGPU_GEM_DOMAIN_VRAM)) {
  617. r = -EINVAL;
  618. amdgpu_bo_unreserve(robj);
  619. break;
  620. }
  621. if (amdgpu_ttm_tt_get_usermm(robj->tbo.ttm)) {
  622. r = -EPERM;
  623. amdgpu_bo_unreserve(robj);
  624. break;
  625. }
  626. robj->preferred_domains = args->value & (AMDGPU_GEM_DOMAIN_VRAM |
  627. AMDGPU_GEM_DOMAIN_GTT |
  628. AMDGPU_GEM_DOMAIN_CPU);
  629. robj->allowed_domains = robj->preferred_domains;
  630. if (robj->allowed_domains == AMDGPU_GEM_DOMAIN_VRAM)
  631. robj->allowed_domains |= AMDGPU_GEM_DOMAIN_GTT;
  632. if (robj->flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID)
  633. amdgpu_vm_bo_invalidate(adev, robj, true);
  634. amdgpu_bo_unreserve(robj);
  635. break;
  636. default:
  637. amdgpu_bo_unreserve(robj);
  638. r = -EINVAL;
  639. }
  640. out:
  641. drm_gem_object_put_unlocked(gobj);
  642. return r;
  643. }
  644. int amdgpu_mode_dumb_create(struct drm_file *file_priv,
  645. struct drm_device *dev,
  646. struct drm_mode_create_dumb *args)
  647. {
  648. struct amdgpu_device *adev = dev->dev_private;
  649. struct drm_gem_object *gobj;
  650. uint32_t handle;
  651. u32 domain;
  652. int r;
  653. args->pitch = amdgpu_align_pitch(adev, args->width,
  654. DIV_ROUND_UP(args->bpp, 8), 0);
  655. args->size = (u64)args->pitch * args->height;
  656. args->size = ALIGN(args->size, PAGE_SIZE);
  657. domain = amdgpu_bo_get_preferred_pin_domain(adev,
  658. amdgpu_display_supported_domains(adev));
  659. r = amdgpu_gem_object_create(adev, args->size, 0, domain,
  660. AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED,
  661. false, NULL, &gobj);
  662. if (r)
  663. return -ENOMEM;
  664. r = drm_gem_handle_create(file_priv, gobj, &handle);
  665. /* drop reference from allocate - handle holds it now */
  666. drm_gem_object_put_unlocked(gobj);
  667. if (r) {
  668. return r;
  669. }
  670. args->handle = handle;
  671. return 0;
  672. }
  673. #if defined(CONFIG_DEBUG_FS)
  674. #define amdgpu_debugfs_gem_bo_print_flag(m, bo, flag) \
  675. if (bo->flags & (AMDGPU_GEM_CREATE_ ## flag)) { \
  676. seq_printf((m), " " #flag); \
  677. }
  678. static int amdgpu_debugfs_gem_bo_info(int id, void *ptr, void *data)
  679. {
  680. struct drm_gem_object *gobj = ptr;
  681. struct amdgpu_bo *bo = gem_to_amdgpu_bo(gobj);
  682. struct seq_file *m = data;
  683. struct dma_buf_attachment *attachment;
  684. struct dma_buf *dma_buf;
  685. unsigned domain;
  686. const char *placement;
  687. unsigned pin_count;
  688. domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
  689. switch (domain) {
  690. case AMDGPU_GEM_DOMAIN_VRAM:
  691. placement = "VRAM";
  692. break;
  693. case AMDGPU_GEM_DOMAIN_GTT:
  694. placement = " GTT";
  695. break;
  696. case AMDGPU_GEM_DOMAIN_CPU:
  697. default:
  698. placement = " CPU";
  699. break;
  700. }
  701. seq_printf(m, "\t0x%08x: %12ld byte %s",
  702. id, amdgpu_bo_size(bo), placement);
  703. pin_count = READ_ONCE(bo->pin_count);
  704. if (pin_count)
  705. seq_printf(m, " pin count %d", pin_count);
  706. dma_buf = READ_ONCE(bo->gem_base.dma_buf);
  707. attachment = READ_ONCE(bo->gem_base.import_attach);
  708. if (attachment)
  709. seq_printf(m, " imported from %p", dma_buf);
  710. else if (dma_buf)
  711. seq_printf(m, " exported as %p", dma_buf);
  712. amdgpu_debugfs_gem_bo_print_flag(m, bo, CPU_ACCESS_REQUIRED);
  713. amdgpu_debugfs_gem_bo_print_flag(m, bo, NO_CPU_ACCESS);
  714. amdgpu_debugfs_gem_bo_print_flag(m, bo, CPU_GTT_USWC);
  715. amdgpu_debugfs_gem_bo_print_flag(m, bo, VRAM_CLEARED);
  716. amdgpu_debugfs_gem_bo_print_flag(m, bo, SHADOW);
  717. amdgpu_debugfs_gem_bo_print_flag(m, bo, VRAM_CONTIGUOUS);
  718. amdgpu_debugfs_gem_bo_print_flag(m, bo, VM_ALWAYS_VALID);
  719. amdgpu_debugfs_gem_bo_print_flag(m, bo, EXPLICIT_SYNC);
  720. seq_printf(m, "\n");
  721. return 0;
  722. }
  723. static int amdgpu_debugfs_gem_info(struct seq_file *m, void *data)
  724. {
  725. struct drm_info_node *node = (struct drm_info_node *)m->private;
  726. struct drm_device *dev = node->minor->dev;
  727. struct drm_file *file;
  728. int r;
  729. r = mutex_lock_interruptible(&dev->filelist_mutex);
  730. if (r)
  731. return r;
  732. list_for_each_entry(file, &dev->filelist, lhead) {
  733. struct task_struct *task;
  734. /*
  735. * Although we have a valid reference on file->pid, that does
  736. * not guarantee that the task_struct who called get_pid() is
  737. * still alive (e.g. get_pid(current) => fork() => exit()).
  738. * Therefore, we need to protect this ->comm access using RCU.
  739. */
  740. rcu_read_lock();
  741. task = pid_task(file->pid, PIDTYPE_PID);
  742. seq_printf(m, "pid %8d command %s:\n", pid_nr(file->pid),
  743. task ? task->comm : "<unknown>");
  744. rcu_read_unlock();
  745. spin_lock(&file->table_lock);
  746. idr_for_each(&file->object_idr, amdgpu_debugfs_gem_bo_info, m);
  747. spin_unlock(&file->table_lock);
  748. }
  749. mutex_unlock(&dev->filelist_mutex);
  750. return 0;
  751. }
  752. static const struct drm_info_list amdgpu_debugfs_gem_list[] = {
  753. {"amdgpu_gem_info", &amdgpu_debugfs_gem_info, 0, NULL},
  754. };
  755. #endif
  756. int amdgpu_debugfs_gem_init(struct amdgpu_device *adev)
  757. {
  758. #if defined(CONFIG_DEBUG_FS)
  759. return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_gem_list, 1);
  760. #endif
  761. return 0;
  762. }