amdgpu_gem.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  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, ttm_bo_type_device, 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, ttm_bo_type_device, 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_bo_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. * @operation: map, unmap or clear
  443. *
  444. * Update the bo_va directly after setting its address. Errors are not
  445. * vital here, so they are not reported back to userspace.
  446. */
  447. static void amdgpu_gem_va_update_vm(struct amdgpu_device *adev,
  448. struct amdgpu_vm *vm,
  449. struct amdgpu_bo_va *bo_va,
  450. uint32_t operation)
  451. {
  452. int r;
  453. if (!amdgpu_vm_ready(vm))
  454. return;
  455. r = amdgpu_vm_clear_freed(adev, vm, NULL);
  456. if (r)
  457. goto error;
  458. if (operation == AMDGPU_VA_OP_MAP ||
  459. operation == AMDGPU_VA_OP_REPLACE) {
  460. r = amdgpu_vm_bo_update(adev, bo_va, false);
  461. if (r)
  462. goto error;
  463. }
  464. r = amdgpu_vm_update_directories(adev, vm);
  465. error:
  466. if (r && r != -ERESTARTSYS)
  467. DRM_ERROR("Couldn't update BO_VA (%d)\n", r);
  468. }
  469. int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data,
  470. struct drm_file *filp)
  471. {
  472. const uint32_t valid_flags = AMDGPU_VM_DELAY_UPDATE |
  473. AMDGPU_VM_PAGE_READABLE | AMDGPU_VM_PAGE_WRITEABLE |
  474. AMDGPU_VM_PAGE_EXECUTABLE | AMDGPU_VM_MTYPE_MASK;
  475. const uint32_t prt_flags = AMDGPU_VM_DELAY_UPDATE |
  476. AMDGPU_VM_PAGE_PRT;
  477. struct drm_amdgpu_gem_va *args = data;
  478. struct drm_gem_object *gobj;
  479. struct amdgpu_device *adev = dev->dev_private;
  480. struct amdgpu_fpriv *fpriv = filp->driver_priv;
  481. struct amdgpu_bo *abo;
  482. struct amdgpu_bo_va *bo_va;
  483. struct amdgpu_bo_list_entry vm_pd;
  484. struct ttm_validate_buffer tv;
  485. struct ww_acquire_ctx ticket;
  486. struct list_head list, duplicates;
  487. uint64_t va_flags;
  488. int r = 0;
  489. if (args->va_address < AMDGPU_VA_RESERVED_SIZE) {
  490. dev_dbg(&dev->pdev->dev,
  491. "va_address 0x%LX is in reserved area 0x%LX\n",
  492. args->va_address, AMDGPU_VA_RESERVED_SIZE);
  493. return -EINVAL;
  494. }
  495. if (args->va_address >= AMDGPU_VA_HOLE_START &&
  496. args->va_address < AMDGPU_VA_HOLE_END) {
  497. dev_dbg(&dev->pdev->dev,
  498. "va_address 0x%LX is in VA hole 0x%LX-0x%LX\n",
  499. args->va_address, AMDGPU_VA_HOLE_START,
  500. AMDGPU_VA_HOLE_END);
  501. return -EINVAL;
  502. }
  503. args->va_address &= AMDGPU_VA_HOLE_MASK;
  504. if ((args->flags & ~valid_flags) && (args->flags & ~prt_flags)) {
  505. dev_dbg(&dev->pdev->dev, "invalid flags combination 0x%08X\n",
  506. args->flags);
  507. return -EINVAL;
  508. }
  509. switch (args->operation) {
  510. case AMDGPU_VA_OP_MAP:
  511. case AMDGPU_VA_OP_UNMAP:
  512. case AMDGPU_VA_OP_CLEAR:
  513. case AMDGPU_VA_OP_REPLACE:
  514. break;
  515. default:
  516. dev_dbg(&dev->pdev->dev, "unsupported operation %d\n",
  517. args->operation);
  518. return -EINVAL;
  519. }
  520. INIT_LIST_HEAD(&list);
  521. INIT_LIST_HEAD(&duplicates);
  522. if ((args->operation != AMDGPU_VA_OP_CLEAR) &&
  523. !(args->flags & AMDGPU_VM_PAGE_PRT)) {
  524. gobj = drm_gem_object_lookup(filp, args->handle);
  525. if (gobj == NULL)
  526. return -ENOENT;
  527. abo = gem_to_amdgpu_bo(gobj);
  528. tv.bo = &abo->tbo;
  529. tv.shared = !!(abo->flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID);
  530. list_add(&tv.head, &list);
  531. } else {
  532. gobj = NULL;
  533. abo = NULL;
  534. }
  535. amdgpu_vm_get_pd_bo(&fpriv->vm, &list, &vm_pd);
  536. r = ttm_eu_reserve_buffers(&ticket, &list, true, &duplicates);
  537. if (r)
  538. goto error_unref;
  539. if (abo) {
  540. bo_va = amdgpu_vm_bo_find(&fpriv->vm, abo);
  541. if (!bo_va) {
  542. r = -ENOENT;
  543. goto error_backoff;
  544. }
  545. } else if (args->operation != AMDGPU_VA_OP_CLEAR) {
  546. bo_va = fpriv->prt_va;
  547. } else {
  548. bo_va = NULL;
  549. }
  550. switch (args->operation) {
  551. case AMDGPU_VA_OP_MAP:
  552. r = amdgpu_vm_alloc_pts(adev, bo_va->base.vm, args->va_address,
  553. args->map_size);
  554. if (r)
  555. goto error_backoff;
  556. va_flags = amdgpu_gmc_get_pte_flags(adev, args->flags);
  557. r = amdgpu_vm_bo_map(adev, bo_va, args->va_address,
  558. args->offset_in_bo, args->map_size,
  559. va_flags);
  560. break;
  561. case AMDGPU_VA_OP_UNMAP:
  562. r = amdgpu_vm_bo_unmap(adev, bo_va, args->va_address);
  563. break;
  564. case AMDGPU_VA_OP_CLEAR:
  565. r = amdgpu_vm_bo_clear_mappings(adev, &fpriv->vm,
  566. args->va_address,
  567. args->map_size);
  568. break;
  569. case AMDGPU_VA_OP_REPLACE:
  570. r = amdgpu_vm_alloc_pts(adev, bo_va->base.vm, args->va_address,
  571. args->map_size);
  572. if (r)
  573. goto error_backoff;
  574. va_flags = amdgpu_gmc_get_pte_flags(adev, args->flags);
  575. r = amdgpu_vm_bo_replace_map(adev, bo_va, args->va_address,
  576. args->offset_in_bo, args->map_size,
  577. va_flags);
  578. break;
  579. default:
  580. break;
  581. }
  582. if (!r && !(args->flags & AMDGPU_VM_DELAY_UPDATE) && !amdgpu_vm_debug)
  583. amdgpu_gem_va_update_vm(adev, &fpriv->vm, bo_va,
  584. args->operation);
  585. error_backoff:
  586. ttm_eu_backoff_reservation(&ticket, &list);
  587. error_unref:
  588. drm_gem_object_put_unlocked(gobj);
  589. return r;
  590. }
  591. int amdgpu_gem_op_ioctl(struct drm_device *dev, void *data,
  592. struct drm_file *filp)
  593. {
  594. struct amdgpu_device *adev = dev->dev_private;
  595. struct drm_amdgpu_gem_op *args = data;
  596. struct drm_gem_object *gobj;
  597. struct amdgpu_bo *robj;
  598. int r;
  599. gobj = drm_gem_object_lookup(filp, args->handle);
  600. if (gobj == NULL) {
  601. return -ENOENT;
  602. }
  603. robj = gem_to_amdgpu_bo(gobj);
  604. r = amdgpu_bo_reserve(robj, false);
  605. if (unlikely(r))
  606. goto out;
  607. switch (args->op) {
  608. case AMDGPU_GEM_OP_GET_GEM_CREATE_INFO: {
  609. struct drm_amdgpu_gem_create_in info;
  610. void __user *out = u64_to_user_ptr(args->value);
  611. info.bo_size = robj->gem_base.size;
  612. info.alignment = robj->tbo.mem.page_alignment << PAGE_SHIFT;
  613. info.domains = robj->preferred_domains;
  614. info.domain_flags = robj->flags;
  615. amdgpu_bo_unreserve(robj);
  616. if (copy_to_user(out, &info, sizeof(info)))
  617. r = -EFAULT;
  618. break;
  619. }
  620. case AMDGPU_GEM_OP_SET_PLACEMENT:
  621. if (robj->prime_shared_count && (args->value & AMDGPU_GEM_DOMAIN_VRAM)) {
  622. r = -EINVAL;
  623. amdgpu_bo_unreserve(robj);
  624. break;
  625. }
  626. if (amdgpu_ttm_tt_get_usermm(robj->tbo.ttm)) {
  627. r = -EPERM;
  628. amdgpu_bo_unreserve(robj);
  629. break;
  630. }
  631. robj->preferred_domains = args->value & (AMDGPU_GEM_DOMAIN_VRAM |
  632. AMDGPU_GEM_DOMAIN_GTT |
  633. AMDGPU_GEM_DOMAIN_CPU);
  634. robj->allowed_domains = robj->preferred_domains;
  635. if (robj->allowed_domains == AMDGPU_GEM_DOMAIN_VRAM)
  636. robj->allowed_domains |= AMDGPU_GEM_DOMAIN_GTT;
  637. if (robj->flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID)
  638. amdgpu_vm_bo_invalidate(adev, robj, true);
  639. amdgpu_bo_unreserve(robj);
  640. break;
  641. default:
  642. amdgpu_bo_unreserve(robj);
  643. r = -EINVAL;
  644. }
  645. out:
  646. drm_gem_object_put_unlocked(gobj);
  647. return r;
  648. }
  649. int amdgpu_mode_dumb_create(struct drm_file *file_priv,
  650. struct drm_device *dev,
  651. struct drm_mode_create_dumb *args)
  652. {
  653. struct amdgpu_device *adev = dev->dev_private;
  654. struct drm_gem_object *gobj;
  655. uint32_t handle;
  656. u32 domain;
  657. int r;
  658. args->pitch = amdgpu_align_pitch(adev, args->width,
  659. DIV_ROUND_UP(args->bpp, 8), 0);
  660. args->size = (u64)args->pitch * args->height;
  661. args->size = ALIGN(args->size, PAGE_SIZE);
  662. domain = amdgpu_bo_get_preferred_pin_domain(adev,
  663. amdgpu_display_supported_domains(adev));
  664. r = amdgpu_gem_object_create(adev, args->size, 0, domain,
  665. AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED,
  666. ttm_bo_type_device, NULL, &gobj);
  667. if (r)
  668. return -ENOMEM;
  669. r = drm_gem_handle_create(file_priv, gobj, &handle);
  670. /* drop reference from allocate - handle holds it now */
  671. drm_gem_object_put_unlocked(gobj);
  672. if (r) {
  673. return r;
  674. }
  675. args->handle = handle;
  676. return 0;
  677. }
  678. #if defined(CONFIG_DEBUG_FS)
  679. #define amdgpu_debugfs_gem_bo_print_flag(m, bo, flag) \
  680. if (bo->flags & (AMDGPU_GEM_CREATE_ ## flag)) { \
  681. seq_printf((m), " " #flag); \
  682. }
  683. static int amdgpu_debugfs_gem_bo_info(int id, void *ptr, void *data)
  684. {
  685. struct drm_gem_object *gobj = ptr;
  686. struct amdgpu_bo *bo = gem_to_amdgpu_bo(gobj);
  687. struct seq_file *m = data;
  688. struct dma_buf_attachment *attachment;
  689. struct dma_buf *dma_buf;
  690. unsigned domain;
  691. const char *placement;
  692. unsigned pin_count;
  693. domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
  694. switch (domain) {
  695. case AMDGPU_GEM_DOMAIN_VRAM:
  696. placement = "VRAM";
  697. break;
  698. case AMDGPU_GEM_DOMAIN_GTT:
  699. placement = " GTT";
  700. break;
  701. case AMDGPU_GEM_DOMAIN_CPU:
  702. default:
  703. placement = " CPU";
  704. break;
  705. }
  706. seq_printf(m, "\t0x%08x: %12ld byte %s",
  707. id, amdgpu_bo_size(bo), placement);
  708. pin_count = READ_ONCE(bo->pin_count);
  709. if (pin_count)
  710. seq_printf(m, " pin count %d", pin_count);
  711. dma_buf = READ_ONCE(bo->gem_base.dma_buf);
  712. attachment = READ_ONCE(bo->gem_base.import_attach);
  713. if (attachment)
  714. seq_printf(m, " imported from %p", dma_buf);
  715. else if (dma_buf)
  716. seq_printf(m, " exported as %p", dma_buf);
  717. amdgpu_debugfs_gem_bo_print_flag(m, bo, CPU_ACCESS_REQUIRED);
  718. amdgpu_debugfs_gem_bo_print_flag(m, bo, NO_CPU_ACCESS);
  719. amdgpu_debugfs_gem_bo_print_flag(m, bo, CPU_GTT_USWC);
  720. amdgpu_debugfs_gem_bo_print_flag(m, bo, VRAM_CLEARED);
  721. amdgpu_debugfs_gem_bo_print_flag(m, bo, SHADOW);
  722. amdgpu_debugfs_gem_bo_print_flag(m, bo, VRAM_CONTIGUOUS);
  723. amdgpu_debugfs_gem_bo_print_flag(m, bo, VM_ALWAYS_VALID);
  724. amdgpu_debugfs_gem_bo_print_flag(m, bo, EXPLICIT_SYNC);
  725. seq_printf(m, "\n");
  726. return 0;
  727. }
  728. static int amdgpu_debugfs_gem_info(struct seq_file *m, void *data)
  729. {
  730. struct drm_info_node *node = (struct drm_info_node *)m->private;
  731. struct drm_device *dev = node->minor->dev;
  732. struct drm_file *file;
  733. int r;
  734. r = mutex_lock_interruptible(&dev->filelist_mutex);
  735. if (r)
  736. return r;
  737. list_for_each_entry(file, &dev->filelist, lhead) {
  738. struct task_struct *task;
  739. /*
  740. * Although we have a valid reference on file->pid, that does
  741. * not guarantee that the task_struct who called get_pid() is
  742. * still alive (e.g. get_pid(current) => fork() => exit()).
  743. * Therefore, we need to protect this ->comm access using RCU.
  744. */
  745. rcu_read_lock();
  746. task = pid_task(file->pid, PIDTYPE_PID);
  747. seq_printf(m, "pid %8d command %s:\n", pid_nr(file->pid),
  748. task ? task->comm : "<unknown>");
  749. rcu_read_unlock();
  750. spin_lock(&file->table_lock);
  751. idr_for_each(&file->object_idr, amdgpu_debugfs_gem_bo_info, m);
  752. spin_unlock(&file->table_lock);
  753. }
  754. mutex_unlock(&dev->filelist_mutex);
  755. return 0;
  756. }
  757. static const struct drm_info_list amdgpu_debugfs_gem_list[] = {
  758. {"amdgpu_gem_info", &amdgpu_debugfs_gem_info, 0, NULL},
  759. };
  760. #endif
  761. int amdgpu_debugfs_gem_init(struct amdgpu_device *adev)
  762. {
  763. #if defined(CONFIG_DEBUG_FS)
  764. return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_gem_list, 1);
  765. #endif
  766. return 0;
  767. }