amdgpu_gem.c 22 KB

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