radeon_gem.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  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 <drm/drmP.h>
  29. #include <drm/radeon_drm.h>
  30. #include "radeon.h"
  31. void radeon_gem_object_free(struct drm_gem_object *gobj)
  32. {
  33. struct radeon_bo *robj = gem_to_radeon_bo(gobj);
  34. if (robj) {
  35. if (robj->gem_base.import_attach)
  36. drm_prime_gem_destroy(&robj->gem_base, robj->tbo.sg);
  37. radeon_mn_unregister(robj);
  38. radeon_bo_unref(&robj);
  39. }
  40. }
  41. int radeon_gem_object_create(struct radeon_device *rdev, unsigned long size,
  42. int alignment, int initial_domain,
  43. u32 flags, bool kernel,
  44. struct drm_gem_object **obj)
  45. {
  46. struct radeon_bo *robj;
  47. unsigned long max_size;
  48. int r;
  49. *obj = NULL;
  50. /* At least align on page size */
  51. if (alignment < PAGE_SIZE) {
  52. alignment = PAGE_SIZE;
  53. }
  54. /* Maximum bo size is the unpinned gtt size since we use the gtt to
  55. * handle vram to system pool migrations.
  56. */
  57. max_size = rdev->mc.gtt_size - rdev->gart_pin_size;
  58. if (size > max_size) {
  59. DRM_DEBUG("Allocation size %ldMb bigger than %ldMb limit\n",
  60. size >> 20, max_size >> 20);
  61. return -ENOMEM;
  62. }
  63. retry:
  64. r = radeon_bo_create(rdev, size, alignment, kernel, initial_domain,
  65. flags, NULL, NULL, &robj);
  66. if (r) {
  67. if (r != -ERESTARTSYS) {
  68. if (initial_domain == RADEON_GEM_DOMAIN_VRAM) {
  69. initial_domain |= RADEON_GEM_DOMAIN_GTT;
  70. goto retry;
  71. }
  72. DRM_ERROR("Failed to allocate GEM object (%ld, %d, %u, %d)\n",
  73. size, initial_domain, alignment, r);
  74. }
  75. return r;
  76. }
  77. *obj = &robj->gem_base;
  78. robj->pid = task_pid_nr(current);
  79. mutex_lock(&rdev->gem.mutex);
  80. list_add_tail(&robj->list, &rdev->gem.objects);
  81. mutex_unlock(&rdev->gem.mutex);
  82. return 0;
  83. }
  84. static int radeon_gem_set_domain(struct drm_gem_object *gobj,
  85. uint32_t rdomain, uint32_t wdomain)
  86. {
  87. struct radeon_bo *robj;
  88. uint32_t domain;
  89. long r;
  90. /* FIXME: reeimplement */
  91. robj = gem_to_radeon_bo(gobj);
  92. /* work out where to validate the buffer to */
  93. domain = wdomain;
  94. if (!domain) {
  95. domain = rdomain;
  96. }
  97. if (!domain) {
  98. /* Do nothings */
  99. printk(KERN_WARNING "Set domain without domain !\n");
  100. return 0;
  101. }
  102. if (domain == RADEON_GEM_DOMAIN_CPU) {
  103. /* Asking for cpu access wait for object idle */
  104. r = reservation_object_wait_timeout_rcu(robj->tbo.resv, true, true, 30 * HZ);
  105. if (!r)
  106. r = -EBUSY;
  107. if (r < 0 && r != -EINTR) {
  108. printk(KERN_ERR "Failed to wait for object: %li\n", r);
  109. return r;
  110. }
  111. }
  112. return 0;
  113. }
  114. int radeon_gem_init(struct radeon_device *rdev)
  115. {
  116. INIT_LIST_HEAD(&rdev->gem.objects);
  117. return 0;
  118. }
  119. void radeon_gem_fini(struct radeon_device *rdev)
  120. {
  121. radeon_bo_force_delete(rdev);
  122. }
  123. /*
  124. * Call from drm_gem_handle_create which appear in both new and open ioctl
  125. * case.
  126. */
  127. int radeon_gem_object_open(struct drm_gem_object *obj, struct drm_file *file_priv)
  128. {
  129. struct radeon_bo *rbo = gem_to_radeon_bo(obj);
  130. struct radeon_device *rdev = rbo->rdev;
  131. struct radeon_fpriv *fpriv = file_priv->driver_priv;
  132. struct radeon_vm *vm = &fpriv->vm;
  133. struct radeon_bo_va *bo_va;
  134. int r;
  135. if ((rdev->family < CHIP_CAYMAN) ||
  136. (!rdev->accel_working)) {
  137. return 0;
  138. }
  139. r = radeon_bo_reserve(rbo, false);
  140. if (r) {
  141. return r;
  142. }
  143. bo_va = radeon_vm_bo_find(vm, rbo);
  144. if (!bo_va) {
  145. bo_va = radeon_vm_bo_add(rdev, vm, rbo);
  146. } else {
  147. ++bo_va->ref_count;
  148. }
  149. radeon_bo_unreserve(rbo);
  150. return 0;
  151. }
  152. void radeon_gem_object_close(struct drm_gem_object *obj,
  153. struct drm_file *file_priv)
  154. {
  155. struct radeon_bo *rbo = gem_to_radeon_bo(obj);
  156. struct radeon_device *rdev = rbo->rdev;
  157. struct radeon_fpriv *fpriv = file_priv->driver_priv;
  158. struct radeon_vm *vm = &fpriv->vm;
  159. struct radeon_bo_va *bo_va;
  160. int r;
  161. if ((rdev->family < CHIP_CAYMAN) ||
  162. (!rdev->accel_working)) {
  163. return;
  164. }
  165. r = radeon_bo_reserve(rbo, true);
  166. if (r) {
  167. dev_err(rdev->dev, "leaking bo va because "
  168. "we fail to reserve bo (%d)\n", r);
  169. return;
  170. }
  171. bo_va = radeon_vm_bo_find(vm, rbo);
  172. if (bo_va) {
  173. if (--bo_va->ref_count == 0) {
  174. radeon_vm_bo_rmv(rdev, bo_va);
  175. }
  176. }
  177. radeon_bo_unreserve(rbo);
  178. }
  179. static int radeon_gem_handle_lockup(struct radeon_device *rdev, int r)
  180. {
  181. if (r == -EDEADLK) {
  182. r = radeon_gpu_reset(rdev);
  183. if (!r)
  184. r = -EAGAIN;
  185. }
  186. return r;
  187. }
  188. /*
  189. * GEM ioctls.
  190. */
  191. int radeon_gem_info_ioctl(struct drm_device *dev, void *data,
  192. struct drm_file *filp)
  193. {
  194. struct radeon_device *rdev = dev->dev_private;
  195. struct drm_radeon_gem_info *args = data;
  196. struct ttm_mem_type_manager *man;
  197. man = &rdev->mman.bdev.man[TTM_PL_VRAM];
  198. args->vram_size = rdev->mc.real_vram_size;
  199. args->vram_visible = (u64)man->size << PAGE_SHIFT;
  200. args->vram_visible -= rdev->vram_pin_size;
  201. args->gart_size = rdev->mc.gtt_size;
  202. args->gart_size -= rdev->gart_pin_size;
  203. return 0;
  204. }
  205. int radeon_gem_pread_ioctl(struct drm_device *dev, void *data,
  206. struct drm_file *filp)
  207. {
  208. /* TODO: implement */
  209. DRM_ERROR("unimplemented %s\n", __func__);
  210. return -ENOSYS;
  211. }
  212. int radeon_gem_pwrite_ioctl(struct drm_device *dev, void *data,
  213. struct drm_file *filp)
  214. {
  215. /* TODO: implement */
  216. DRM_ERROR("unimplemented %s\n", __func__);
  217. return -ENOSYS;
  218. }
  219. int radeon_gem_create_ioctl(struct drm_device *dev, void *data,
  220. struct drm_file *filp)
  221. {
  222. struct radeon_device *rdev = dev->dev_private;
  223. struct drm_radeon_gem_create *args = data;
  224. struct drm_gem_object *gobj;
  225. uint32_t handle;
  226. int r;
  227. down_read(&rdev->exclusive_lock);
  228. /* create a gem object to contain this object in */
  229. args->size = roundup(args->size, PAGE_SIZE);
  230. r = radeon_gem_object_create(rdev, args->size, args->alignment,
  231. args->initial_domain, args->flags,
  232. false, &gobj);
  233. if (r) {
  234. up_read(&rdev->exclusive_lock);
  235. r = radeon_gem_handle_lockup(rdev, r);
  236. return r;
  237. }
  238. r = drm_gem_handle_create(filp, gobj, &handle);
  239. /* drop reference from allocate - handle holds it now */
  240. drm_gem_object_unreference_unlocked(gobj);
  241. if (r) {
  242. up_read(&rdev->exclusive_lock);
  243. r = radeon_gem_handle_lockup(rdev, r);
  244. return r;
  245. }
  246. args->handle = handle;
  247. up_read(&rdev->exclusive_lock);
  248. return 0;
  249. }
  250. int radeon_gem_userptr_ioctl(struct drm_device *dev, void *data,
  251. struct drm_file *filp)
  252. {
  253. struct radeon_device *rdev = dev->dev_private;
  254. struct drm_radeon_gem_userptr *args = data;
  255. struct drm_gem_object *gobj;
  256. struct radeon_bo *bo;
  257. uint32_t handle;
  258. int r;
  259. if (offset_in_page(args->addr | args->size))
  260. return -EINVAL;
  261. /* reject unknown flag values */
  262. if (args->flags & ~(RADEON_GEM_USERPTR_READONLY |
  263. RADEON_GEM_USERPTR_ANONONLY | RADEON_GEM_USERPTR_VALIDATE |
  264. RADEON_GEM_USERPTR_REGISTER))
  265. return -EINVAL;
  266. if (args->flags & RADEON_GEM_USERPTR_READONLY) {
  267. /* readonly pages not tested on older hardware */
  268. if (rdev->family < CHIP_R600)
  269. return -EINVAL;
  270. } else if (!(args->flags & RADEON_GEM_USERPTR_ANONONLY) ||
  271. !(args->flags & RADEON_GEM_USERPTR_REGISTER)) {
  272. /* if we want to write to it we must require anonymous
  273. memory and install a MMU notifier */
  274. return -EACCES;
  275. }
  276. down_read(&rdev->exclusive_lock);
  277. /* create a gem object to contain this object in */
  278. r = radeon_gem_object_create(rdev, args->size, 0,
  279. RADEON_GEM_DOMAIN_CPU, 0,
  280. false, &gobj);
  281. if (r)
  282. goto handle_lockup;
  283. bo = gem_to_radeon_bo(gobj);
  284. r = radeon_ttm_tt_set_userptr(bo->tbo.ttm, args->addr, args->flags);
  285. if (r)
  286. goto release_object;
  287. if (args->flags & RADEON_GEM_USERPTR_REGISTER) {
  288. r = radeon_mn_register(bo, args->addr);
  289. if (r)
  290. goto release_object;
  291. }
  292. if (args->flags & RADEON_GEM_USERPTR_VALIDATE) {
  293. down_read(&current->mm->mmap_sem);
  294. r = radeon_bo_reserve(bo, true);
  295. if (r) {
  296. up_read(&current->mm->mmap_sem);
  297. goto release_object;
  298. }
  299. radeon_ttm_placement_from_domain(bo, RADEON_GEM_DOMAIN_GTT);
  300. r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
  301. radeon_bo_unreserve(bo);
  302. up_read(&current->mm->mmap_sem);
  303. if (r)
  304. goto release_object;
  305. }
  306. r = drm_gem_handle_create(filp, gobj, &handle);
  307. /* drop reference from allocate - handle holds it now */
  308. drm_gem_object_unreference_unlocked(gobj);
  309. if (r)
  310. goto handle_lockup;
  311. args->handle = handle;
  312. up_read(&rdev->exclusive_lock);
  313. return 0;
  314. release_object:
  315. drm_gem_object_unreference_unlocked(gobj);
  316. handle_lockup:
  317. up_read(&rdev->exclusive_lock);
  318. r = radeon_gem_handle_lockup(rdev, r);
  319. return r;
  320. }
  321. int radeon_gem_set_domain_ioctl(struct drm_device *dev, void *data,
  322. struct drm_file *filp)
  323. {
  324. /* transition the BO to a domain -
  325. * just validate the BO into a certain domain */
  326. struct radeon_device *rdev = dev->dev_private;
  327. struct drm_radeon_gem_set_domain *args = data;
  328. struct drm_gem_object *gobj;
  329. struct radeon_bo *robj;
  330. int r;
  331. /* for now if someone requests domain CPU -
  332. * just make sure the buffer is finished with */
  333. down_read(&rdev->exclusive_lock);
  334. /* just do a BO wait for now */
  335. gobj = drm_gem_object_lookup(dev, filp, args->handle);
  336. if (gobj == NULL) {
  337. up_read(&rdev->exclusive_lock);
  338. return -ENOENT;
  339. }
  340. robj = gem_to_radeon_bo(gobj);
  341. r = radeon_gem_set_domain(gobj, args->read_domains, args->write_domain);
  342. drm_gem_object_unreference_unlocked(gobj);
  343. up_read(&rdev->exclusive_lock);
  344. r = radeon_gem_handle_lockup(robj->rdev, r);
  345. return r;
  346. }
  347. int radeon_mode_dumb_mmap(struct drm_file *filp,
  348. struct drm_device *dev,
  349. uint32_t handle, uint64_t *offset_p)
  350. {
  351. struct drm_gem_object *gobj;
  352. struct radeon_bo *robj;
  353. gobj = drm_gem_object_lookup(dev, filp, handle);
  354. if (gobj == NULL) {
  355. return -ENOENT;
  356. }
  357. robj = gem_to_radeon_bo(gobj);
  358. if (radeon_ttm_tt_has_userptr(robj->tbo.ttm)) {
  359. drm_gem_object_unreference_unlocked(gobj);
  360. return -EPERM;
  361. }
  362. *offset_p = radeon_bo_mmap_offset(robj);
  363. drm_gem_object_unreference_unlocked(gobj);
  364. return 0;
  365. }
  366. int radeon_gem_mmap_ioctl(struct drm_device *dev, void *data,
  367. struct drm_file *filp)
  368. {
  369. struct drm_radeon_gem_mmap *args = data;
  370. return radeon_mode_dumb_mmap(filp, dev, args->handle, &args->addr_ptr);
  371. }
  372. int radeon_gem_busy_ioctl(struct drm_device *dev, void *data,
  373. struct drm_file *filp)
  374. {
  375. struct drm_radeon_gem_busy *args = data;
  376. struct drm_gem_object *gobj;
  377. struct radeon_bo *robj;
  378. int r;
  379. uint32_t cur_placement = 0;
  380. gobj = drm_gem_object_lookup(dev, filp, args->handle);
  381. if (gobj == NULL) {
  382. return -ENOENT;
  383. }
  384. robj = gem_to_radeon_bo(gobj);
  385. r = reservation_object_test_signaled_rcu(robj->tbo.resv, true);
  386. if (r == 0)
  387. r = -EBUSY;
  388. else
  389. r = 0;
  390. cur_placement = ACCESS_ONCE(robj->tbo.mem.mem_type);
  391. args->domain = radeon_mem_type_to_domain(cur_placement);
  392. drm_gem_object_unreference_unlocked(gobj);
  393. return r;
  394. }
  395. int radeon_gem_wait_idle_ioctl(struct drm_device *dev, void *data,
  396. struct drm_file *filp)
  397. {
  398. struct radeon_device *rdev = dev->dev_private;
  399. struct drm_radeon_gem_wait_idle *args = data;
  400. struct drm_gem_object *gobj;
  401. struct radeon_bo *robj;
  402. int r = 0;
  403. uint32_t cur_placement = 0;
  404. long ret;
  405. gobj = drm_gem_object_lookup(dev, filp, args->handle);
  406. if (gobj == NULL) {
  407. return -ENOENT;
  408. }
  409. robj = gem_to_radeon_bo(gobj);
  410. ret = reservation_object_wait_timeout_rcu(robj->tbo.resv, true, true, 30 * HZ);
  411. if (ret == 0)
  412. r = -EBUSY;
  413. else if (ret < 0)
  414. r = ret;
  415. /* Flush HDP cache via MMIO if necessary */
  416. cur_placement = ACCESS_ONCE(robj->tbo.mem.mem_type);
  417. if (rdev->asic->mmio_hdp_flush &&
  418. radeon_mem_type_to_domain(cur_placement) == RADEON_GEM_DOMAIN_VRAM)
  419. robj->rdev->asic->mmio_hdp_flush(rdev);
  420. drm_gem_object_unreference_unlocked(gobj);
  421. r = radeon_gem_handle_lockup(rdev, r);
  422. return r;
  423. }
  424. int radeon_gem_set_tiling_ioctl(struct drm_device *dev, void *data,
  425. struct drm_file *filp)
  426. {
  427. struct drm_radeon_gem_set_tiling *args = data;
  428. struct drm_gem_object *gobj;
  429. struct radeon_bo *robj;
  430. int r = 0;
  431. DRM_DEBUG("%d \n", args->handle);
  432. gobj = drm_gem_object_lookup(dev, filp, args->handle);
  433. if (gobj == NULL)
  434. return -ENOENT;
  435. robj = gem_to_radeon_bo(gobj);
  436. r = radeon_bo_set_tiling_flags(robj, args->tiling_flags, args->pitch);
  437. drm_gem_object_unreference_unlocked(gobj);
  438. return r;
  439. }
  440. int radeon_gem_get_tiling_ioctl(struct drm_device *dev, void *data,
  441. struct drm_file *filp)
  442. {
  443. struct drm_radeon_gem_get_tiling *args = data;
  444. struct drm_gem_object *gobj;
  445. struct radeon_bo *rbo;
  446. int r = 0;
  447. DRM_DEBUG("\n");
  448. gobj = drm_gem_object_lookup(dev, filp, args->handle);
  449. if (gobj == NULL)
  450. return -ENOENT;
  451. rbo = gem_to_radeon_bo(gobj);
  452. r = radeon_bo_reserve(rbo, false);
  453. if (unlikely(r != 0))
  454. goto out;
  455. radeon_bo_get_tiling_flags(rbo, &args->tiling_flags, &args->pitch);
  456. radeon_bo_unreserve(rbo);
  457. out:
  458. drm_gem_object_unreference_unlocked(gobj);
  459. return r;
  460. }
  461. /**
  462. * radeon_gem_va_update_vm -update the bo_va in its VM
  463. *
  464. * @rdev: radeon_device pointer
  465. * @bo_va: bo_va to update
  466. *
  467. * Update the bo_va directly after setting it's address. Errors are not
  468. * vital here, so they are not reported back to userspace.
  469. */
  470. static void radeon_gem_va_update_vm(struct radeon_device *rdev,
  471. struct radeon_bo_va *bo_va)
  472. {
  473. struct ttm_validate_buffer tv, *entry;
  474. struct radeon_bo_list *vm_bos;
  475. struct ww_acquire_ctx ticket;
  476. struct list_head list;
  477. unsigned domain;
  478. int r;
  479. INIT_LIST_HEAD(&list);
  480. tv.bo = &bo_va->bo->tbo;
  481. tv.shared = true;
  482. list_add(&tv.head, &list);
  483. vm_bos = radeon_vm_get_bos(rdev, bo_va->vm, &list);
  484. if (!vm_bos)
  485. return;
  486. r = ttm_eu_reserve_buffers(&ticket, &list, true, NULL);
  487. if (r)
  488. goto error_free;
  489. list_for_each_entry(entry, &list, head) {
  490. domain = radeon_mem_type_to_domain(entry->bo->mem.mem_type);
  491. /* if anything is swapped out don't swap it in here,
  492. just abort and wait for the next CS */
  493. if (domain == RADEON_GEM_DOMAIN_CPU)
  494. goto error_unreserve;
  495. }
  496. mutex_lock(&bo_va->vm->mutex);
  497. r = radeon_vm_clear_freed(rdev, bo_va->vm);
  498. if (r)
  499. goto error_unlock;
  500. if (bo_va->it.start)
  501. r = radeon_vm_bo_update(rdev, bo_va, &bo_va->bo->tbo.mem);
  502. error_unlock:
  503. mutex_unlock(&bo_va->vm->mutex);
  504. error_unreserve:
  505. ttm_eu_backoff_reservation(&ticket, &list);
  506. error_free:
  507. drm_free_large(vm_bos);
  508. if (r && r != -ERESTARTSYS)
  509. DRM_ERROR("Couldn't update BO_VA (%d)\n", r);
  510. }
  511. int radeon_gem_va_ioctl(struct drm_device *dev, void *data,
  512. struct drm_file *filp)
  513. {
  514. struct drm_radeon_gem_va *args = data;
  515. struct drm_gem_object *gobj;
  516. struct radeon_device *rdev = dev->dev_private;
  517. struct radeon_fpriv *fpriv = filp->driver_priv;
  518. struct radeon_bo *rbo;
  519. struct radeon_bo_va *bo_va;
  520. u32 invalid_flags;
  521. int r = 0;
  522. if (!rdev->vm_manager.enabled) {
  523. args->operation = RADEON_VA_RESULT_ERROR;
  524. return -ENOTTY;
  525. }
  526. /* !! DONT REMOVE !!
  527. * We don't support vm_id yet, to be sure we don't have have broken
  528. * userspace, reject anyone trying to use non 0 value thus moving
  529. * forward we can use those fields without breaking existant userspace
  530. */
  531. if (args->vm_id) {
  532. args->operation = RADEON_VA_RESULT_ERROR;
  533. return -EINVAL;
  534. }
  535. if (args->offset < RADEON_VA_RESERVED_SIZE) {
  536. dev_err(&dev->pdev->dev,
  537. "offset 0x%lX is in reserved area 0x%X\n",
  538. (unsigned long)args->offset,
  539. RADEON_VA_RESERVED_SIZE);
  540. args->operation = RADEON_VA_RESULT_ERROR;
  541. return -EINVAL;
  542. }
  543. /* don't remove, we need to enforce userspace to set the snooped flag
  544. * otherwise we will endup with broken userspace and we won't be able
  545. * to enable this feature without adding new interface
  546. */
  547. invalid_flags = RADEON_VM_PAGE_VALID | RADEON_VM_PAGE_SYSTEM;
  548. if ((args->flags & invalid_flags)) {
  549. dev_err(&dev->pdev->dev, "invalid flags 0x%08X vs 0x%08X\n",
  550. args->flags, invalid_flags);
  551. args->operation = RADEON_VA_RESULT_ERROR;
  552. return -EINVAL;
  553. }
  554. switch (args->operation) {
  555. case RADEON_VA_MAP:
  556. case RADEON_VA_UNMAP:
  557. break;
  558. default:
  559. dev_err(&dev->pdev->dev, "unsupported operation %d\n",
  560. args->operation);
  561. args->operation = RADEON_VA_RESULT_ERROR;
  562. return -EINVAL;
  563. }
  564. gobj = drm_gem_object_lookup(dev, filp, args->handle);
  565. if (gobj == NULL) {
  566. args->operation = RADEON_VA_RESULT_ERROR;
  567. return -ENOENT;
  568. }
  569. rbo = gem_to_radeon_bo(gobj);
  570. r = radeon_bo_reserve(rbo, false);
  571. if (r) {
  572. args->operation = RADEON_VA_RESULT_ERROR;
  573. drm_gem_object_unreference_unlocked(gobj);
  574. return r;
  575. }
  576. bo_va = radeon_vm_bo_find(&fpriv->vm, rbo);
  577. if (!bo_va) {
  578. args->operation = RADEON_VA_RESULT_ERROR;
  579. drm_gem_object_unreference_unlocked(gobj);
  580. return -ENOENT;
  581. }
  582. switch (args->operation) {
  583. case RADEON_VA_MAP:
  584. if (bo_va->it.start) {
  585. args->operation = RADEON_VA_RESULT_VA_EXIST;
  586. args->offset = bo_va->it.start * RADEON_GPU_PAGE_SIZE;
  587. radeon_bo_unreserve(rbo);
  588. goto out;
  589. }
  590. r = radeon_vm_bo_set_addr(rdev, bo_va, args->offset, args->flags);
  591. break;
  592. case RADEON_VA_UNMAP:
  593. r = radeon_vm_bo_set_addr(rdev, bo_va, 0, 0);
  594. break;
  595. default:
  596. break;
  597. }
  598. if (!r)
  599. radeon_gem_va_update_vm(rdev, bo_va);
  600. args->operation = RADEON_VA_RESULT_OK;
  601. if (r) {
  602. args->operation = RADEON_VA_RESULT_ERROR;
  603. }
  604. out:
  605. drm_gem_object_unreference_unlocked(gobj);
  606. return r;
  607. }
  608. int radeon_gem_op_ioctl(struct drm_device *dev, void *data,
  609. struct drm_file *filp)
  610. {
  611. struct drm_radeon_gem_op *args = data;
  612. struct drm_gem_object *gobj;
  613. struct radeon_bo *robj;
  614. int r;
  615. gobj = drm_gem_object_lookup(dev, filp, args->handle);
  616. if (gobj == NULL) {
  617. return -ENOENT;
  618. }
  619. robj = gem_to_radeon_bo(gobj);
  620. r = -EPERM;
  621. if (radeon_ttm_tt_has_userptr(robj->tbo.ttm))
  622. goto out;
  623. r = radeon_bo_reserve(robj, false);
  624. if (unlikely(r))
  625. goto out;
  626. switch (args->op) {
  627. case RADEON_GEM_OP_GET_INITIAL_DOMAIN:
  628. args->value = robj->initial_domain;
  629. break;
  630. case RADEON_GEM_OP_SET_INITIAL_DOMAIN:
  631. robj->initial_domain = args->value & (RADEON_GEM_DOMAIN_VRAM |
  632. RADEON_GEM_DOMAIN_GTT |
  633. RADEON_GEM_DOMAIN_CPU);
  634. break;
  635. default:
  636. r = -EINVAL;
  637. }
  638. radeon_bo_unreserve(robj);
  639. out:
  640. drm_gem_object_unreference_unlocked(gobj);
  641. return r;
  642. }
  643. int radeon_mode_dumb_create(struct drm_file *file_priv,
  644. struct drm_device *dev,
  645. struct drm_mode_create_dumb *args)
  646. {
  647. struct radeon_device *rdev = dev->dev_private;
  648. struct drm_gem_object *gobj;
  649. uint32_t handle;
  650. int r;
  651. args->pitch = radeon_align_pitch(rdev, args->width, args->bpp, 0) * ((args->bpp + 1) / 8);
  652. args->size = args->pitch * args->height;
  653. args->size = ALIGN(args->size, PAGE_SIZE);
  654. r = radeon_gem_object_create(rdev, args->size, 0,
  655. RADEON_GEM_DOMAIN_VRAM, 0,
  656. false, &gobj);
  657. if (r)
  658. return -ENOMEM;
  659. r = drm_gem_handle_create(file_priv, gobj, &handle);
  660. /* drop reference from allocate - handle holds it now */
  661. drm_gem_object_unreference_unlocked(gobj);
  662. if (r) {
  663. return r;
  664. }
  665. args->handle = handle;
  666. return 0;
  667. }
  668. #if defined(CONFIG_DEBUG_FS)
  669. static int radeon_debugfs_gem_info(struct seq_file *m, void *data)
  670. {
  671. struct drm_info_node *node = (struct drm_info_node *)m->private;
  672. struct drm_device *dev = node->minor->dev;
  673. struct radeon_device *rdev = dev->dev_private;
  674. struct radeon_bo *rbo;
  675. unsigned i = 0;
  676. mutex_lock(&rdev->gem.mutex);
  677. list_for_each_entry(rbo, &rdev->gem.objects, list) {
  678. unsigned domain;
  679. const char *placement;
  680. domain = radeon_mem_type_to_domain(rbo->tbo.mem.mem_type);
  681. switch (domain) {
  682. case RADEON_GEM_DOMAIN_VRAM:
  683. placement = "VRAM";
  684. break;
  685. case RADEON_GEM_DOMAIN_GTT:
  686. placement = " GTT";
  687. break;
  688. case RADEON_GEM_DOMAIN_CPU:
  689. default:
  690. placement = " CPU";
  691. break;
  692. }
  693. seq_printf(m, "bo[0x%08x] %8ldkB %8ldMB %s pid %8ld\n",
  694. i, radeon_bo_size(rbo) >> 10, radeon_bo_size(rbo) >> 20,
  695. placement, (unsigned long)rbo->pid);
  696. i++;
  697. }
  698. mutex_unlock(&rdev->gem.mutex);
  699. return 0;
  700. }
  701. static struct drm_info_list radeon_debugfs_gem_list[] = {
  702. {"radeon_gem_info", &radeon_debugfs_gem_info, 0, NULL},
  703. };
  704. #endif
  705. int radeon_gem_debugfs_init(struct radeon_device *rdev)
  706. {
  707. #if defined(CONFIG_DEBUG_FS)
  708. return radeon_debugfs_add_files(rdev, radeon_debugfs_gem_list, 1);
  709. #endif
  710. return 0;
  711. }