radeon_object.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. /*
  2. * Copyright 2009 Jerome Glisse.
  3. * All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sub license, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  16. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  17. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  18. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  19. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. *
  21. * The above copyright notice and this permission notice (including the
  22. * next paragraph) shall be included in all copies or substantial portions
  23. * of the Software.
  24. *
  25. */
  26. /*
  27. * Authors:
  28. * Jerome Glisse <glisse@freedesktop.org>
  29. * Thomas Hellstrom <thomas-at-tungstengraphics-dot-com>
  30. * Dave Airlie
  31. */
  32. #include <linux/list.h>
  33. #include <drm/drmP.h>
  34. #include "radeon_drm.h"
  35. #include "radeon.h"
  36. struct radeon_object {
  37. struct ttm_buffer_object tobj;
  38. struct list_head list;
  39. struct radeon_device *rdev;
  40. struct drm_gem_object *gobj;
  41. struct ttm_bo_kmap_obj kmap;
  42. unsigned pin_count;
  43. uint64_t gpu_addr;
  44. void *kptr;
  45. bool is_iomem;
  46. uint32_t tiling_flags;
  47. uint32_t pitch;
  48. int surface_reg;
  49. };
  50. int radeon_ttm_init(struct radeon_device *rdev);
  51. void radeon_ttm_fini(struct radeon_device *rdev);
  52. /*
  53. * To exclude mutual BO access we rely on bo_reserve exclusion, as all
  54. * function are calling it.
  55. */
  56. static int radeon_object_reserve(struct radeon_object *robj, bool interruptible)
  57. {
  58. return ttm_bo_reserve(&robj->tobj, interruptible, false, false, 0);
  59. }
  60. static void radeon_object_unreserve(struct radeon_object *robj)
  61. {
  62. ttm_bo_unreserve(&robj->tobj);
  63. }
  64. static void radeon_ttm_object_object_destroy(struct ttm_buffer_object *tobj)
  65. {
  66. struct radeon_object *robj;
  67. robj = container_of(tobj, struct radeon_object, tobj);
  68. list_del_init(&robj->list);
  69. radeon_object_clear_surface_reg(robj);
  70. kfree(robj);
  71. }
  72. static inline void radeon_object_gpu_addr(struct radeon_object *robj)
  73. {
  74. /* Default gpu address */
  75. robj->gpu_addr = 0xFFFFFFFFFFFFFFFFULL;
  76. if (robj->tobj.mem.mm_node == NULL) {
  77. return;
  78. }
  79. robj->gpu_addr = ((u64)robj->tobj.mem.mm_node->start) << PAGE_SHIFT;
  80. switch (robj->tobj.mem.mem_type) {
  81. case TTM_PL_VRAM:
  82. robj->gpu_addr += (u64)robj->rdev->mc.vram_location;
  83. break;
  84. case TTM_PL_TT:
  85. robj->gpu_addr += (u64)robj->rdev->mc.gtt_location;
  86. break;
  87. default:
  88. DRM_ERROR("Unknown placement %d\n", robj->tobj.mem.mem_type);
  89. robj->gpu_addr = 0xFFFFFFFFFFFFFFFFULL;
  90. return;
  91. }
  92. }
  93. static inline uint32_t radeon_object_flags_from_domain(uint32_t domain)
  94. {
  95. uint32_t flags = 0;
  96. if (domain & RADEON_GEM_DOMAIN_VRAM) {
  97. flags |= TTM_PL_FLAG_VRAM;
  98. }
  99. if (domain & RADEON_GEM_DOMAIN_GTT) {
  100. flags |= TTM_PL_FLAG_TT;
  101. }
  102. if (domain & RADEON_GEM_DOMAIN_CPU) {
  103. flags |= TTM_PL_FLAG_SYSTEM;
  104. }
  105. if (!flags) {
  106. flags |= TTM_PL_FLAG_SYSTEM;
  107. }
  108. return flags;
  109. }
  110. int radeon_object_create(struct radeon_device *rdev,
  111. struct drm_gem_object *gobj,
  112. unsigned long size,
  113. bool kernel,
  114. uint32_t domain,
  115. bool interruptible,
  116. struct radeon_object **robj_ptr)
  117. {
  118. struct radeon_object *robj;
  119. enum ttm_bo_type type;
  120. uint32_t flags;
  121. int r;
  122. if (unlikely(rdev->mman.bdev.dev_mapping == NULL)) {
  123. rdev->mman.bdev.dev_mapping = rdev->ddev->dev_mapping;
  124. }
  125. if (kernel) {
  126. type = ttm_bo_type_kernel;
  127. } else {
  128. type = ttm_bo_type_device;
  129. }
  130. *robj_ptr = NULL;
  131. robj = kzalloc(sizeof(struct radeon_object), GFP_KERNEL);
  132. if (robj == NULL) {
  133. return -ENOMEM;
  134. }
  135. robj->rdev = rdev;
  136. robj->gobj = gobj;
  137. robj->surface_reg = -1;
  138. INIT_LIST_HEAD(&robj->list);
  139. flags = radeon_object_flags_from_domain(domain);
  140. r = ttm_buffer_object_init(&rdev->mman.bdev, &robj->tobj, size, type, flags,
  141. 0, 0, false, NULL, size,
  142. &radeon_ttm_object_object_destroy);
  143. if (unlikely(r != 0)) {
  144. /* ttm call radeon_ttm_object_object_destroy if error happen */
  145. DRM_ERROR("Failed to allocate TTM object (%ld, 0x%08X, %u)\n",
  146. size, flags, 0);
  147. return r;
  148. }
  149. *robj_ptr = robj;
  150. if (gobj) {
  151. list_add_tail(&robj->list, &rdev->gem.objects);
  152. }
  153. return 0;
  154. }
  155. int radeon_object_kmap(struct radeon_object *robj, void **ptr)
  156. {
  157. int r;
  158. spin_lock(&robj->tobj.lock);
  159. if (robj->kptr) {
  160. if (ptr) {
  161. *ptr = robj->kptr;
  162. }
  163. spin_unlock(&robj->tobj.lock);
  164. return 0;
  165. }
  166. spin_unlock(&robj->tobj.lock);
  167. r = ttm_bo_kmap(&robj->tobj, 0, robj->tobj.num_pages, &robj->kmap);
  168. if (r) {
  169. return r;
  170. }
  171. spin_lock(&robj->tobj.lock);
  172. robj->kptr = ttm_kmap_obj_virtual(&robj->kmap, &robj->is_iomem);
  173. spin_unlock(&robj->tobj.lock);
  174. if (ptr) {
  175. *ptr = robj->kptr;
  176. }
  177. return 0;
  178. }
  179. void radeon_object_kunmap(struct radeon_object *robj)
  180. {
  181. spin_lock(&robj->tobj.lock);
  182. if (robj->kptr == NULL) {
  183. spin_unlock(&robj->tobj.lock);
  184. return;
  185. }
  186. robj->kptr = NULL;
  187. spin_unlock(&robj->tobj.lock);
  188. ttm_bo_kunmap(&robj->kmap);
  189. }
  190. void radeon_object_unref(struct radeon_object **robj)
  191. {
  192. struct ttm_buffer_object *tobj;
  193. if ((*robj) == NULL) {
  194. return;
  195. }
  196. tobj = &((*robj)->tobj);
  197. ttm_bo_unref(&tobj);
  198. if (tobj == NULL) {
  199. *robj = NULL;
  200. }
  201. }
  202. int radeon_object_mmap(struct radeon_object *robj, uint64_t *offset)
  203. {
  204. *offset = robj->tobj.addr_space_offset;
  205. return 0;
  206. }
  207. int radeon_object_pin(struct radeon_object *robj, uint32_t domain,
  208. uint64_t *gpu_addr)
  209. {
  210. uint32_t flags;
  211. uint32_t tmp;
  212. int r;
  213. flags = radeon_object_flags_from_domain(domain);
  214. spin_lock(&robj->tobj.lock);
  215. if (robj->pin_count) {
  216. robj->pin_count++;
  217. if (gpu_addr != NULL) {
  218. *gpu_addr = robj->gpu_addr;
  219. }
  220. spin_unlock(&robj->tobj.lock);
  221. return 0;
  222. }
  223. spin_unlock(&robj->tobj.lock);
  224. r = radeon_object_reserve(robj, false);
  225. if (unlikely(r != 0)) {
  226. DRM_ERROR("radeon: failed to reserve object for pinning it.\n");
  227. return r;
  228. }
  229. tmp = robj->tobj.mem.placement;
  230. ttm_flag_masked(&tmp, flags, TTM_PL_MASK_MEM);
  231. robj->tobj.proposed_placement = tmp | TTM_PL_FLAG_NO_EVICT | TTM_PL_MASK_CACHING;
  232. r = ttm_buffer_object_validate(&robj->tobj,
  233. robj->tobj.proposed_placement,
  234. false, false);
  235. radeon_object_gpu_addr(robj);
  236. if (gpu_addr != NULL) {
  237. *gpu_addr = robj->gpu_addr;
  238. }
  239. robj->pin_count = 1;
  240. if (unlikely(r != 0)) {
  241. DRM_ERROR("radeon: failed to pin object.\n");
  242. }
  243. radeon_object_unreserve(robj);
  244. return r;
  245. }
  246. void radeon_object_unpin(struct radeon_object *robj)
  247. {
  248. uint32_t flags;
  249. int r;
  250. spin_lock(&robj->tobj.lock);
  251. if (!robj->pin_count) {
  252. spin_unlock(&robj->tobj.lock);
  253. printk(KERN_WARNING "Unpin not necessary for %p !\n", robj);
  254. return;
  255. }
  256. robj->pin_count--;
  257. if (robj->pin_count) {
  258. spin_unlock(&robj->tobj.lock);
  259. return;
  260. }
  261. spin_unlock(&robj->tobj.lock);
  262. r = radeon_object_reserve(robj, false);
  263. if (unlikely(r != 0)) {
  264. DRM_ERROR("radeon: failed to reserve object for unpinning it.\n");
  265. return;
  266. }
  267. flags = robj->tobj.mem.placement;
  268. robj->tobj.proposed_placement = flags & ~TTM_PL_FLAG_NO_EVICT;
  269. r = ttm_buffer_object_validate(&robj->tobj,
  270. robj->tobj.proposed_placement,
  271. false, false);
  272. if (unlikely(r != 0)) {
  273. DRM_ERROR("radeon: failed to unpin buffer.\n");
  274. }
  275. radeon_object_unreserve(robj);
  276. }
  277. int radeon_object_wait(struct radeon_object *robj)
  278. {
  279. int r = 0;
  280. /* FIXME: should use block reservation instead */
  281. r = radeon_object_reserve(robj, true);
  282. if (unlikely(r != 0)) {
  283. DRM_ERROR("radeon: failed to reserve object for waiting.\n");
  284. return r;
  285. }
  286. spin_lock(&robj->tobj.lock);
  287. if (robj->tobj.sync_obj) {
  288. r = ttm_bo_wait(&robj->tobj, true, false, false);
  289. }
  290. spin_unlock(&robj->tobj.lock);
  291. radeon_object_unreserve(robj);
  292. return r;
  293. }
  294. int radeon_object_evict_vram(struct radeon_device *rdev)
  295. {
  296. if (rdev->flags & RADEON_IS_IGP) {
  297. /* Useless to evict on IGP chips */
  298. return 0;
  299. }
  300. return ttm_bo_evict_mm(&rdev->mman.bdev, TTM_PL_VRAM);
  301. }
  302. void radeon_object_force_delete(struct radeon_device *rdev)
  303. {
  304. struct radeon_object *robj, *n;
  305. struct drm_gem_object *gobj;
  306. if (list_empty(&rdev->gem.objects)) {
  307. return;
  308. }
  309. DRM_ERROR("Userspace still has active objects !\n");
  310. list_for_each_entry_safe(robj, n, &rdev->gem.objects, list) {
  311. mutex_lock(&rdev->ddev->struct_mutex);
  312. gobj = robj->gobj;
  313. DRM_ERROR("Force free for (%p,%p,%lu,%lu)\n",
  314. gobj, robj, (unsigned long)gobj->size,
  315. *((unsigned long *)&gobj->refcount));
  316. list_del_init(&robj->list);
  317. radeon_object_unref(&robj);
  318. gobj->driver_private = NULL;
  319. drm_gem_object_unreference(gobj);
  320. mutex_unlock(&rdev->ddev->struct_mutex);
  321. }
  322. }
  323. int radeon_object_init(struct radeon_device *rdev)
  324. {
  325. return radeon_ttm_init(rdev);
  326. }
  327. void radeon_object_fini(struct radeon_device *rdev)
  328. {
  329. radeon_ttm_fini(rdev);
  330. }
  331. void radeon_object_list_add_object(struct radeon_object_list *lobj,
  332. struct list_head *head)
  333. {
  334. if (lobj->wdomain) {
  335. list_add(&lobj->list, head);
  336. } else {
  337. list_add_tail(&lobj->list, head);
  338. }
  339. }
  340. int radeon_object_list_reserve(struct list_head *head)
  341. {
  342. struct radeon_object_list *lobj;
  343. struct list_head *i;
  344. int r;
  345. list_for_each(i, head) {
  346. lobj = list_entry(i, struct radeon_object_list, list);
  347. if (!lobj->robj->pin_count) {
  348. r = radeon_object_reserve(lobj->robj, true);
  349. if (unlikely(r != 0)) {
  350. DRM_ERROR("radeon: failed to reserve object.\n");
  351. return r;
  352. }
  353. } else {
  354. }
  355. }
  356. return 0;
  357. }
  358. void radeon_object_list_unreserve(struct list_head *head)
  359. {
  360. struct radeon_object_list *lobj;
  361. struct list_head *i;
  362. list_for_each(i, head) {
  363. lobj = list_entry(i, struct radeon_object_list, list);
  364. if (!lobj->robj->pin_count) {
  365. radeon_object_unreserve(lobj->robj);
  366. } else {
  367. }
  368. }
  369. }
  370. int radeon_object_list_validate(struct list_head *head, void *fence)
  371. {
  372. struct radeon_object_list *lobj;
  373. struct radeon_object *robj;
  374. struct radeon_fence *old_fence = NULL;
  375. struct list_head *i;
  376. uint32_t flags;
  377. int r;
  378. r = radeon_object_list_reserve(head);
  379. if (unlikely(r != 0)) {
  380. radeon_object_list_unreserve(head);
  381. return r;
  382. }
  383. list_for_each(i, head) {
  384. lobj = list_entry(i, struct radeon_object_list, list);
  385. robj = lobj->robj;
  386. if (lobj->wdomain) {
  387. flags = radeon_object_flags_from_domain(lobj->wdomain);
  388. flags |= TTM_PL_FLAG_TT;
  389. } else {
  390. flags = radeon_object_flags_from_domain(lobj->rdomain);
  391. flags |= TTM_PL_FLAG_TT;
  392. flags |= TTM_PL_FLAG_VRAM;
  393. }
  394. if (!robj->pin_count) {
  395. robj->tobj.proposed_placement = flags | TTM_PL_MASK_CACHING;
  396. r = ttm_buffer_object_validate(&robj->tobj,
  397. robj->tobj.proposed_placement,
  398. true, false);
  399. if (unlikely(r)) {
  400. DRM_ERROR("radeon: failed to validate.\n");
  401. return r;
  402. }
  403. radeon_object_gpu_addr(robj);
  404. }
  405. lobj->gpu_offset = robj->gpu_addr;
  406. lobj->tiling_flags = robj->tiling_flags;
  407. if (fence) {
  408. old_fence = (struct radeon_fence *)robj->tobj.sync_obj;
  409. robj->tobj.sync_obj = radeon_fence_ref(fence);
  410. robj->tobj.sync_obj_arg = NULL;
  411. }
  412. if (old_fence) {
  413. radeon_fence_unref(&old_fence);
  414. }
  415. }
  416. return 0;
  417. }
  418. void radeon_object_list_unvalidate(struct list_head *head)
  419. {
  420. struct radeon_object_list *lobj;
  421. struct radeon_fence *old_fence = NULL;
  422. struct list_head *i;
  423. list_for_each(i, head) {
  424. lobj = list_entry(i, struct radeon_object_list, list);
  425. old_fence = (struct radeon_fence *)lobj->robj->tobj.sync_obj;
  426. lobj->robj->tobj.sync_obj = NULL;
  427. if (old_fence) {
  428. radeon_fence_unref(&old_fence);
  429. }
  430. }
  431. radeon_object_list_unreserve(head);
  432. }
  433. void radeon_object_list_clean(struct list_head *head)
  434. {
  435. radeon_object_list_unreserve(head);
  436. }
  437. int radeon_object_fbdev_mmap(struct radeon_object *robj,
  438. struct vm_area_struct *vma)
  439. {
  440. return ttm_fbdev_mmap(vma, &robj->tobj);
  441. }
  442. unsigned long radeon_object_size(struct radeon_object *robj)
  443. {
  444. return robj->tobj.num_pages << PAGE_SHIFT;
  445. }
  446. int radeon_object_get_surface_reg(struct radeon_object *robj)
  447. {
  448. struct radeon_device *rdev = robj->rdev;
  449. struct radeon_surface_reg *reg;
  450. struct radeon_object *old_object;
  451. int steal;
  452. int i;
  453. if (!robj->tiling_flags)
  454. return 0;
  455. if (robj->surface_reg >= 0) {
  456. reg = &rdev->surface_regs[robj->surface_reg];
  457. i = robj->surface_reg;
  458. goto out;
  459. }
  460. steal = -1;
  461. for (i = 0; i < RADEON_GEM_MAX_SURFACES; i++) {
  462. reg = &rdev->surface_regs[i];
  463. if (!reg->robj)
  464. break;
  465. old_object = reg->robj;
  466. if (old_object->pin_count == 0)
  467. steal = i;
  468. }
  469. /* if we are all out */
  470. if (i == RADEON_GEM_MAX_SURFACES) {
  471. if (steal == -1)
  472. return -ENOMEM;
  473. /* find someone with a surface reg and nuke their BO */
  474. reg = &rdev->surface_regs[steal];
  475. old_object = reg->robj;
  476. /* blow away the mapping */
  477. DRM_DEBUG("stealing surface reg %d from %p\n", steal, old_object);
  478. ttm_bo_unmap_virtual(&old_object->tobj);
  479. old_object->surface_reg = -1;
  480. i = steal;
  481. }
  482. robj->surface_reg = i;
  483. reg->robj = robj;
  484. out:
  485. radeon_set_surface_reg(rdev, i, robj->tiling_flags, robj->pitch,
  486. robj->tobj.mem.mm_node->start << PAGE_SHIFT,
  487. robj->tobj.num_pages << PAGE_SHIFT);
  488. return 0;
  489. }
  490. void radeon_object_clear_surface_reg(struct radeon_object *robj)
  491. {
  492. struct radeon_device *rdev = robj->rdev;
  493. struct radeon_surface_reg *reg;
  494. if (robj->surface_reg == -1)
  495. return;
  496. reg = &rdev->surface_regs[robj->surface_reg];
  497. radeon_clear_surface_reg(rdev, robj->surface_reg);
  498. reg->robj = NULL;
  499. robj->surface_reg = -1;
  500. }
  501. void radeon_object_set_tiling_flags(struct radeon_object *robj,
  502. uint32_t tiling_flags, uint32_t pitch)
  503. {
  504. robj->tiling_flags = tiling_flags;
  505. robj->pitch = pitch;
  506. }
  507. void radeon_object_get_tiling_flags(struct radeon_object *robj,
  508. uint32_t *tiling_flags,
  509. uint32_t *pitch)
  510. {
  511. if (tiling_flags)
  512. *tiling_flags = robj->tiling_flags;
  513. if (pitch)
  514. *pitch = robj->pitch;
  515. }
  516. int radeon_object_check_tiling(struct radeon_object *robj, bool has_moved,
  517. bool force_drop)
  518. {
  519. if (!(robj->tiling_flags & RADEON_TILING_SURFACE))
  520. return 0;
  521. if (force_drop) {
  522. radeon_object_clear_surface_reg(robj);
  523. return 0;
  524. }
  525. if (robj->tobj.mem.mem_type != TTM_PL_VRAM) {
  526. if (!has_moved)
  527. return 0;
  528. if (robj->surface_reg >= 0)
  529. radeon_object_clear_surface_reg(robj);
  530. return 0;
  531. }
  532. if ((robj->surface_reg >= 0) && !has_moved)
  533. return 0;
  534. return radeon_object_get_surface_reg(robj);
  535. }
  536. void radeon_bo_move_notify(struct ttm_buffer_object *bo,
  537. struct ttm_mem_reg *mem)
  538. {
  539. struct radeon_object *robj = container_of(bo, struct radeon_object, tobj);
  540. radeon_object_check_tiling(robj, 0, 1);
  541. }
  542. void radeon_bo_fault_reserve_notify(struct ttm_buffer_object *bo)
  543. {
  544. struct radeon_object *robj = container_of(bo, struct radeon_object, tobj);
  545. radeon_object_check_tiling(robj, 0, 0);
  546. }