amdgpu_gem.c 23 KB

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