amdgpu_gem.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  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. /* GDS allocations must be DW aligned */
  217. if (args->in.domains & AMDGPU_GEM_DOMAIN_GDS)
  218. size = ALIGN(size, 4);
  219. }
  220. if (flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID) {
  221. r = amdgpu_bo_reserve(vm->root.base.bo, false);
  222. if (r)
  223. return r;
  224. resv = vm->root.base.bo->tbo.resv;
  225. }
  226. r = amdgpu_gem_object_create(adev, size, args->in.alignment,
  227. (u32)(0xffffffff & args->in.domains),
  228. flags, ttm_bo_type_device, resv, &gobj);
  229. if (flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID) {
  230. if (!r) {
  231. struct amdgpu_bo *abo = gem_to_amdgpu_bo(gobj);
  232. abo->parent = amdgpu_bo_ref(vm->root.base.bo);
  233. }
  234. amdgpu_bo_unreserve(vm->root.base.bo);
  235. }
  236. if (r)
  237. return r;
  238. r = drm_gem_handle_create(filp, gobj, &handle);
  239. /* drop reference from allocate - handle holds it now */
  240. drm_gem_object_put_unlocked(gobj);
  241. if (r)
  242. return r;
  243. memset(args, 0, sizeof(*args));
  244. args->out.handle = handle;
  245. return 0;
  246. }
  247. int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data,
  248. struct drm_file *filp)
  249. {
  250. struct ttm_operation_ctx ctx = { true, false };
  251. struct amdgpu_device *adev = dev->dev_private;
  252. struct drm_amdgpu_gem_userptr *args = data;
  253. struct drm_gem_object *gobj;
  254. struct amdgpu_bo *bo;
  255. uint32_t handle;
  256. int r;
  257. if (offset_in_page(args->addr | args->size))
  258. return -EINVAL;
  259. /* reject unknown flag values */
  260. if (args->flags & ~(AMDGPU_GEM_USERPTR_READONLY |
  261. AMDGPU_GEM_USERPTR_ANONONLY | AMDGPU_GEM_USERPTR_VALIDATE |
  262. AMDGPU_GEM_USERPTR_REGISTER))
  263. return -EINVAL;
  264. if (!(args->flags & AMDGPU_GEM_USERPTR_READONLY) &&
  265. !(args->flags & AMDGPU_GEM_USERPTR_REGISTER)) {
  266. /* if we want to write to it we must install a MMU notifier */
  267. return -EACCES;
  268. }
  269. /* create a gem object to contain this object in */
  270. r = amdgpu_gem_object_create(adev, args->size, 0, AMDGPU_GEM_DOMAIN_CPU,
  271. 0, ttm_bo_type_device, NULL, &gobj);
  272. if (r)
  273. return r;
  274. bo = gem_to_amdgpu_bo(gobj);
  275. bo->preferred_domains = AMDGPU_GEM_DOMAIN_GTT;
  276. bo->allowed_domains = AMDGPU_GEM_DOMAIN_GTT;
  277. r = amdgpu_ttm_tt_set_userptr(bo->tbo.ttm, args->addr, args->flags);
  278. if (r)
  279. goto release_object;
  280. if (args->flags & AMDGPU_GEM_USERPTR_REGISTER) {
  281. r = amdgpu_mn_register(bo, args->addr);
  282. if (r)
  283. goto release_object;
  284. }
  285. if (args->flags & AMDGPU_GEM_USERPTR_VALIDATE) {
  286. r = amdgpu_ttm_tt_get_user_pages(bo->tbo.ttm,
  287. bo->tbo.ttm->pages);
  288. if (r)
  289. goto release_object;
  290. r = amdgpu_bo_reserve(bo, true);
  291. if (r)
  292. goto free_pages;
  293. amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_GTT);
  294. r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
  295. amdgpu_bo_unreserve(bo);
  296. if (r)
  297. goto free_pages;
  298. }
  299. r = drm_gem_handle_create(filp, gobj, &handle);
  300. /* drop reference from allocate - handle holds it now */
  301. drm_gem_object_put_unlocked(gobj);
  302. if (r)
  303. return r;
  304. args->handle = handle;
  305. return 0;
  306. free_pages:
  307. release_pages(bo->tbo.ttm->pages, bo->tbo.ttm->num_pages);
  308. release_object:
  309. drm_gem_object_put_unlocked(gobj);
  310. return r;
  311. }
  312. int amdgpu_mode_dumb_mmap(struct drm_file *filp,
  313. struct drm_device *dev,
  314. uint32_t handle, uint64_t *offset_p)
  315. {
  316. struct drm_gem_object *gobj;
  317. struct amdgpu_bo *robj;
  318. gobj = drm_gem_object_lookup(filp, handle);
  319. if (gobj == NULL) {
  320. return -ENOENT;
  321. }
  322. robj = gem_to_amdgpu_bo(gobj);
  323. if (amdgpu_ttm_tt_get_usermm(robj->tbo.ttm) ||
  324. (robj->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)) {
  325. drm_gem_object_put_unlocked(gobj);
  326. return -EPERM;
  327. }
  328. *offset_p = amdgpu_bo_mmap_offset(robj);
  329. drm_gem_object_put_unlocked(gobj);
  330. return 0;
  331. }
  332. int amdgpu_gem_mmap_ioctl(struct drm_device *dev, void *data,
  333. struct drm_file *filp)
  334. {
  335. union drm_amdgpu_gem_mmap *args = data;
  336. uint32_t handle = args->in.handle;
  337. memset(args, 0, sizeof(*args));
  338. return amdgpu_mode_dumb_mmap(filp, dev, handle, &args->out.addr_ptr);
  339. }
  340. /**
  341. * amdgpu_gem_timeout - calculate jiffies timeout from absolute value
  342. *
  343. * @timeout_ns: timeout in ns
  344. *
  345. * Calculate the timeout in jiffies from an absolute timeout in ns.
  346. */
  347. unsigned long amdgpu_gem_timeout(uint64_t timeout_ns)
  348. {
  349. unsigned long timeout_jiffies;
  350. ktime_t timeout;
  351. /* clamp timeout if it's to large */
  352. if (((int64_t)timeout_ns) < 0)
  353. return MAX_SCHEDULE_TIMEOUT;
  354. timeout = ktime_sub(ns_to_ktime(timeout_ns), ktime_get());
  355. if (ktime_to_ns(timeout) < 0)
  356. return 0;
  357. timeout_jiffies = nsecs_to_jiffies(ktime_to_ns(timeout));
  358. /* clamp timeout to avoid unsigned-> signed overflow */
  359. if (timeout_jiffies > MAX_SCHEDULE_TIMEOUT )
  360. return MAX_SCHEDULE_TIMEOUT - 1;
  361. return timeout_jiffies;
  362. }
  363. int amdgpu_gem_wait_idle_ioctl(struct drm_device *dev, void *data,
  364. struct drm_file *filp)
  365. {
  366. union drm_amdgpu_gem_wait_idle *args = data;
  367. struct drm_gem_object *gobj;
  368. struct amdgpu_bo *robj;
  369. uint32_t handle = args->in.handle;
  370. unsigned long timeout = amdgpu_gem_timeout(args->in.timeout);
  371. int r = 0;
  372. long ret;
  373. gobj = drm_gem_object_lookup(filp, handle);
  374. if (gobj == NULL) {
  375. return -ENOENT;
  376. }
  377. robj = gem_to_amdgpu_bo(gobj);
  378. ret = reservation_object_wait_timeout_rcu(robj->tbo.resv, true, true,
  379. timeout);
  380. /* ret == 0 means not signaled,
  381. * ret > 0 means signaled
  382. * ret < 0 means interrupted before timeout
  383. */
  384. if (ret >= 0) {
  385. memset(args, 0, sizeof(*args));
  386. args->out.status = (ret == 0);
  387. } else
  388. r = ret;
  389. drm_gem_object_put_unlocked(gobj);
  390. return r;
  391. }
  392. int amdgpu_gem_metadata_ioctl(struct drm_device *dev, void *data,
  393. struct drm_file *filp)
  394. {
  395. struct drm_amdgpu_gem_metadata *args = data;
  396. struct drm_gem_object *gobj;
  397. struct amdgpu_bo *robj;
  398. int r = -1;
  399. DRM_DEBUG("%d \n", args->handle);
  400. gobj = drm_gem_object_lookup(filp, args->handle);
  401. if (gobj == NULL)
  402. return -ENOENT;
  403. robj = gem_to_amdgpu_bo(gobj);
  404. r = amdgpu_bo_reserve(robj, false);
  405. if (unlikely(r != 0))
  406. goto out;
  407. if (args->op == AMDGPU_GEM_METADATA_OP_GET_METADATA) {
  408. amdgpu_bo_get_tiling_flags(robj, &args->data.tiling_info);
  409. r = amdgpu_bo_get_metadata(robj, args->data.data,
  410. sizeof(args->data.data),
  411. &args->data.data_size_bytes,
  412. &args->data.flags);
  413. } else if (args->op == AMDGPU_GEM_METADATA_OP_SET_METADATA) {
  414. if (args->data.data_size_bytes > sizeof(args->data.data)) {
  415. r = -EINVAL;
  416. goto unreserve;
  417. }
  418. r = amdgpu_bo_set_tiling_flags(robj, args->data.tiling_info);
  419. if (!r)
  420. r = amdgpu_bo_set_metadata(robj, args->data.data,
  421. args->data.data_size_bytes,
  422. args->data.flags);
  423. }
  424. unreserve:
  425. amdgpu_bo_unreserve(robj);
  426. out:
  427. drm_gem_object_put_unlocked(gobj);
  428. return r;
  429. }
  430. /**
  431. * amdgpu_gem_va_update_vm -update the bo_va in its VM
  432. *
  433. * @adev: amdgpu_device pointer
  434. * @vm: vm to update
  435. * @bo_va: bo_va to update
  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. 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_GMC_HOLE_START &&
  490. args->va_address < AMDGPU_GMC_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_GMC_HOLE_START,
  494. AMDGPU_GMC_HOLE_END);
  495. return -EINVAL;
  496. }
  497. args->va_address &= AMDGPU_GMC_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 = !!(abo->flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID);
  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,
  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. u32 domain;
  651. int r;
  652. args->pitch = amdgpu_align_pitch(adev, args->width,
  653. DIV_ROUND_UP(args->bpp, 8), 0);
  654. args->size = (u64)args->pitch * args->height;
  655. args->size = ALIGN(args->size, PAGE_SIZE);
  656. domain = amdgpu_bo_get_preferred_pin_domain(adev,
  657. amdgpu_display_supported_domains(adev));
  658. r = amdgpu_gem_object_create(adev, args->size, 0, domain,
  659. AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED,
  660. ttm_bo_type_device, NULL, &gobj);
  661. if (r)
  662. return -ENOMEM;
  663. r = drm_gem_handle_create(file_priv, gobj, &handle);
  664. /* drop reference from allocate - handle holds it now */
  665. drm_gem_object_put_unlocked(gobj);
  666. if (r) {
  667. return r;
  668. }
  669. args->handle = handle;
  670. return 0;
  671. }
  672. #if defined(CONFIG_DEBUG_FS)
  673. #define amdgpu_debugfs_gem_bo_print_flag(m, bo, flag) \
  674. if (bo->flags & (AMDGPU_GEM_CREATE_ ## flag)) { \
  675. seq_printf((m), " " #flag); \
  676. }
  677. static int amdgpu_debugfs_gem_bo_info(int id, void *ptr, void *data)
  678. {
  679. struct drm_gem_object *gobj = ptr;
  680. struct amdgpu_bo *bo = gem_to_amdgpu_bo(gobj);
  681. struct seq_file *m = data;
  682. struct dma_buf_attachment *attachment;
  683. struct dma_buf *dma_buf;
  684. unsigned domain;
  685. const char *placement;
  686. unsigned pin_count;
  687. domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
  688. switch (domain) {
  689. case AMDGPU_GEM_DOMAIN_VRAM:
  690. placement = "VRAM";
  691. break;
  692. case AMDGPU_GEM_DOMAIN_GTT:
  693. placement = " GTT";
  694. break;
  695. case AMDGPU_GEM_DOMAIN_CPU:
  696. default:
  697. placement = " CPU";
  698. break;
  699. }
  700. seq_printf(m, "\t0x%08x: %12ld byte %s",
  701. id, amdgpu_bo_size(bo), placement);
  702. pin_count = READ_ONCE(bo->pin_count);
  703. if (pin_count)
  704. seq_printf(m, " pin count %d", pin_count);
  705. dma_buf = READ_ONCE(bo->gem_base.dma_buf);
  706. attachment = READ_ONCE(bo->gem_base.import_attach);
  707. if (attachment)
  708. seq_printf(m, " imported from %p", dma_buf);
  709. else if (dma_buf)
  710. seq_printf(m, " exported as %p", dma_buf);
  711. amdgpu_debugfs_gem_bo_print_flag(m, bo, CPU_ACCESS_REQUIRED);
  712. amdgpu_debugfs_gem_bo_print_flag(m, bo, NO_CPU_ACCESS);
  713. amdgpu_debugfs_gem_bo_print_flag(m, bo, CPU_GTT_USWC);
  714. amdgpu_debugfs_gem_bo_print_flag(m, bo, VRAM_CLEARED);
  715. amdgpu_debugfs_gem_bo_print_flag(m, bo, SHADOW);
  716. amdgpu_debugfs_gem_bo_print_flag(m, bo, VRAM_CONTIGUOUS);
  717. amdgpu_debugfs_gem_bo_print_flag(m, bo, VM_ALWAYS_VALID);
  718. amdgpu_debugfs_gem_bo_print_flag(m, bo, EXPLICIT_SYNC);
  719. seq_printf(m, "\n");
  720. return 0;
  721. }
  722. static int amdgpu_debugfs_gem_info(struct seq_file *m, void *data)
  723. {
  724. struct drm_info_node *node = (struct drm_info_node *)m->private;
  725. struct drm_device *dev = node->minor->dev;
  726. struct drm_file *file;
  727. int r;
  728. r = mutex_lock_interruptible(&dev->filelist_mutex);
  729. if (r)
  730. return r;
  731. list_for_each_entry(file, &dev->filelist, lhead) {
  732. struct task_struct *task;
  733. /*
  734. * Although we have a valid reference on file->pid, that does
  735. * not guarantee that the task_struct who called get_pid() is
  736. * still alive (e.g. get_pid(current) => fork() => exit()).
  737. * Therefore, we need to protect this ->comm access using RCU.
  738. */
  739. rcu_read_lock();
  740. task = pid_task(file->pid, PIDTYPE_PID);
  741. seq_printf(m, "pid %8d command %s:\n", pid_nr(file->pid),
  742. task ? task->comm : "<unknown>");
  743. rcu_read_unlock();
  744. spin_lock(&file->table_lock);
  745. idr_for_each(&file->object_idr, amdgpu_debugfs_gem_bo_info, m);
  746. spin_unlock(&file->table_lock);
  747. }
  748. mutex_unlock(&dev->filelist_mutex);
  749. return 0;
  750. }
  751. static const struct drm_info_list amdgpu_debugfs_gem_list[] = {
  752. {"amdgpu_gem_info", &amdgpu_debugfs_gem_info, 0, NULL},
  753. };
  754. #endif
  755. int amdgpu_debugfs_gem_init(struct amdgpu_device *adev)
  756. {
  757. #if defined(CONFIG_DEBUG_FS)
  758. return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_gem_list, 1);
  759. #endif
  760. return 0;
  761. }