radeon_object.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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 | TTM_PL_FLAG_WC | TTM_PL_FLAG_UNCACHED;
  98. }
  99. if (domain & RADEON_GEM_DOMAIN_GTT) {
  100. flags |= TTM_PL_FLAG_TT | TTM_PL_MASK_CACHING;
  101. }
  102. if (domain & RADEON_GEM_DOMAIN_CPU) {
  103. flags |= TTM_PL_FLAG_SYSTEM | TTM_PL_MASK_CACHING;
  104. }
  105. if (!flags) {
  106. flags |= TTM_PL_FLAG_SYSTEM | TTM_PL_MASK_CACHING;
  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. radeon_object_check_tiling(robj, 0, 0);
  178. return 0;
  179. }
  180. void radeon_object_kunmap(struct radeon_object *robj)
  181. {
  182. spin_lock(&robj->tobj.lock);
  183. if (robj->kptr == NULL) {
  184. spin_unlock(&robj->tobj.lock);
  185. return;
  186. }
  187. robj->kptr = NULL;
  188. spin_unlock(&robj->tobj.lock);
  189. radeon_object_check_tiling(robj, 0, 0);
  190. ttm_bo_kunmap(&robj->kmap);
  191. }
  192. void radeon_object_unref(struct radeon_object **robj)
  193. {
  194. struct ttm_buffer_object *tobj;
  195. if ((*robj) == NULL) {
  196. return;
  197. }
  198. tobj = &((*robj)->tobj);
  199. ttm_bo_unref(&tobj);
  200. if (tobj == NULL) {
  201. *robj = NULL;
  202. }
  203. }
  204. int radeon_object_mmap(struct radeon_object *robj, uint64_t *offset)
  205. {
  206. *offset = robj->tobj.addr_space_offset;
  207. return 0;
  208. }
  209. int radeon_object_pin(struct radeon_object *robj, uint32_t domain,
  210. uint64_t *gpu_addr)
  211. {
  212. uint32_t flags;
  213. uint32_t tmp;
  214. int r;
  215. flags = radeon_object_flags_from_domain(domain);
  216. spin_lock(&robj->tobj.lock);
  217. if (robj->pin_count) {
  218. robj->pin_count++;
  219. if (gpu_addr != NULL) {
  220. *gpu_addr = robj->gpu_addr;
  221. }
  222. spin_unlock(&robj->tobj.lock);
  223. return 0;
  224. }
  225. spin_unlock(&robj->tobj.lock);
  226. r = radeon_object_reserve(robj, false);
  227. if (unlikely(r != 0)) {
  228. DRM_ERROR("radeon: failed to reserve object for pinning it.\n");
  229. return r;
  230. }
  231. tmp = robj->tobj.mem.placement;
  232. ttm_flag_masked(&tmp, flags, TTM_PL_MASK_MEM);
  233. robj->tobj.proposed_placement = tmp | TTM_PL_FLAG_NO_EVICT | TTM_PL_MASK_CACHING;
  234. r = ttm_buffer_object_validate(&robj->tobj,
  235. robj->tobj.proposed_placement,
  236. false, false);
  237. radeon_object_gpu_addr(robj);
  238. if (gpu_addr != NULL) {
  239. *gpu_addr = robj->gpu_addr;
  240. }
  241. robj->pin_count = 1;
  242. if (unlikely(r != 0)) {
  243. DRM_ERROR("radeon: failed to pin object.\n");
  244. }
  245. radeon_object_unreserve(robj);
  246. return r;
  247. }
  248. void radeon_object_unpin(struct radeon_object *robj)
  249. {
  250. uint32_t flags;
  251. int r;
  252. spin_lock(&robj->tobj.lock);
  253. if (!robj->pin_count) {
  254. spin_unlock(&robj->tobj.lock);
  255. printk(KERN_WARNING "Unpin not necessary for %p !\n", robj);
  256. return;
  257. }
  258. robj->pin_count--;
  259. if (robj->pin_count) {
  260. spin_unlock(&robj->tobj.lock);
  261. return;
  262. }
  263. spin_unlock(&robj->tobj.lock);
  264. r = radeon_object_reserve(robj, false);
  265. if (unlikely(r != 0)) {
  266. DRM_ERROR("radeon: failed to reserve object for unpinning it.\n");
  267. return;
  268. }
  269. flags = robj->tobj.mem.placement;
  270. robj->tobj.proposed_placement = flags & ~TTM_PL_FLAG_NO_EVICT;
  271. r = ttm_buffer_object_validate(&robj->tobj,
  272. robj->tobj.proposed_placement,
  273. false, false);
  274. if (unlikely(r != 0)) {
  275. DRM_ERROR("radeon: failed to unpin buffer.\n");
  276. }
  277. radeon_object_unreserve(robj);
  278. }
  279. int radeon_object_wait(struct radeon_object *robj)
  280. {
  281. int r = 0;
  282. /* FIXME: should use block reservation instead */
  283. r = radeon_object_reserve(robj, true);
  284. if (unlikely(r != 0)) {
  285. DRM_ERROR("radeon: failed to reserve object for waiting.\n");
  286. return r;
  287. }
  288. spin_lock(&robj->tobj.lock);
  289. if (robj->tobj.sync_obj) {
  290. r = ttm_bo_wait(&robj->tobj, true, true, false);
  291. }
  292. spin_unlock(&robj->tobj.lock);
  293. radeon_object_unreserve(robj);
  294. return r;
  295. }
  296. int radeon_object_busy_domain(struct radeon_object *robj, uint32_t *cur_placement)
  297. {
  298. int r = 0;
  299. r = radeon_object_reserve(robj, true);
  300. if (unlikely(r != 0)) {
  301. DRM_ERROR("radeon: failed to reserve object for waiting.\n");
  302. return r;
  303. }
  304. spin_lock(&robj->tobj.lock);
  305. *cur_placement = robj->tobj.mem.mem_type;
  306. if (robj->tobj.sync_obj) {
  307. r = ttm_bo_wait(&robj->tobj, true, true, true);
  308. }
  309. spin_unlock(&robj->tobj.lock);
  310. radeon_object_unreserve(robj);
  311. return r;
  312. }
  313. int radeon_object_evict_vram(struct radeon_device *rdev)
  314. {
  315. if (rdev->flags & RADEON_IS_IGP) {
  316. /* Useless to evict on IGP chips */
  317. return 0;
  318. }
  319. return ttm_bo_evict_mm(&rdev->mman.bdev, TTM_PL_VRAM);
  320. }
  321. void radeon_object_force_delete(struct radeon_device *rdev)
  322. {
  323. struct radeon_object *robj, *n;
  324. struct drm_gem_object *gobj;
  325. if (list_empty(&rdev->gem.objects)) {
  326. return;
  327. }
  328. DRM_ERROR("Userspace still has active objects !\n");
  329. list_for_each_entry_safe(robj, n, &rdev->gem.objects, list) {
  330. mutex_lock(&rdev->ddev->struct_mutex);
  331. gobj = robj->gobj;
  332. DRM_ERROR("Force free for (%p,%p,%lu,%lu)\n",
  333. gobj, robj, (unsigned long)gobj->size,
  334. *((unsigned long *)&gobj->refcount));
  335. list_del_init(&robj->list);
  336. radeon_object_unref(&robj);
  337. gobj->driver_private = NULL;
  338. drm_gem_object_unreference(gobj);
  339. mutex_unlock(&rdev->ddev->struct_mutex);
  340. }
  341. }
  342. int radeon_object_init(struct radeon_device *rdev)
  343. {
  344. /* Add an MTRR for the VRAM */
  345. rdev->mc.vram_mtrr = mtrr_add(rdev->mc.aper_base, rdev->mc.aper_size,
  346. MTRR_TYPE_WRCOMB, 1);
  347. DRM_INFO("Detected VRAM RAM=%lluM, BAR=%lluM\n",
  348. rdev->mc.mc_vram_size >> 20,
  349. (unsigned long long)rdev->mc.aper_size >> 20);
  350. DRM_INFO("RAM width %dbits %cDR\n",
  351. rdev->mc.vram_width, rdev->mc.vram_is_ddr ? 'D' : 'S');
  352. return radeon_ttm_init(rdev);
  353. }
  354. void radeon_object_fini(struct radeon_device *rdev)
  355. {
  356. radeon_ttm_fini(rdev);
  357. }
  358. void radeon_object_list_add_object(struct radeon_object_list *lobj,
  359. struct list_head *head)
  360. {
  361. if (lobj->wdomain) {
  362. list_add(&lobj->list, head);
  363. } else {
  364. list_add_tail(&lobj->list, head);
  365. }
  366. }
  367. int radeon_object_list_reserve(struct list_head *head)
  368. {
  369. struct radeon_object_list *lobj;
  370. int r;
  371. list_for_each_entry(lobj, head, list){
  372. if (!lobj->robj->pin_count) {
  373. r = radeon_object_reserve(lobj->robj, true);
  374. if (unlikely(r != 0)) {
  375. DRM_ERROR("radeon: failed to reserve object.\n");
  376. return r;
  377. }
  378. } else {
  379. }
  380. }
  381. return 0;
  382. }
  383. void radeon_object_list_unreserve(struct list_head *head)
  384. {
  385. struct radeon_object_list *lobj;
  386. list_for_each_entry(lobj, head, list) {
  387. if (!lobj->robj->pin_count) {
  388. radeon_object_unreserve(lobj->robj);
  389. }
  390. }
  391. }
  392. int radeon_object_list_validate(struct list_head *head, void *fence)
  393. {
  394. struct radeon_object_list *lobj;
  395. struct radeon_object *robj;
  396. struct radeon_fence *old_fence = NULL;
  397. int r;
  398. r = radeon_object_list_reserve(head);
  399. if (unlikely(r != 0)) {
  400. radeon_object_list_unreserve(head);
  401. return r;
  402. }
  403. list_for_each_entry(lobj, head, list) {
  404. robj = lobj->robj;
  405. if (!robj->pin_count) {
  406. if (lobj->wdomain) {
  407. robj->tobj.proposed_placement =
  408. radeon_object_flags_from_domain(lobj->wdomain);
  409. } else {
  410. robj->tobj.proposed_placement =
  411. radeon_object_flags_from_domain(lobj->rdomain);
  412. }
  413. r = ttm_buffer_object_validate(&robj->tobj,
  414. robj->tobj.proposed_placement,
  415. true, false);
  416. if (unlikely(r)) {
  417. DRM_ERROR("radeon: failed to validate.\n");
  418. return r;
  419. }
  420. radeon_object_gpu_addr(robj);
  421. }
  422. lobj->gpu_offset = robj->gpu_addr;
  423. lobj->tiling_flags = robj->tiling_flags;
  424. if (fence) {
  425. old_fence = (struct radeon_fence *)robj->tobj.sync_obj;
  426. robj->tobj.sync_obj = radeon_fence_ref(fence);
  427. robj->tobj.sync_obj_arg = NULL;
  428. }
  429. if (old_fence) {
  430. radeon_fence_unref(&old_fence);
  431. }
  432. }
  433. return 0;
  434. }
  435. void radeon_object_list_unvalidate(struct list_head *head)
  436. {
  437. struct radeon_object_list *lobj;
  438. struct radeon_fence *old_fence = NULL;
  439. list_for_each_entry(lobj, head, list) {
  440. old_fence = (struct radeon_fence *)lobj->robj->tobj.sync_obj;
  441. lobj->robj->tobj.sync_obj = NULL;
  442. if (old_fence) {
  443. radeon_fence_unref(&old_fence);
  444. }
  445. }
  446. radeon_object_list_unreserve(head);
  447. }
  448. void radeon_object_list_clean(struct list_head *head)
  449. {
  450. radeon_object_list_unreserve(head);
  451. }
  452. int radeon_object_fbdev_mmap(struct radeon_object *robj,
  453. struct vm_area_struct *vma)
  454. {
  455. return ttm_fbdev_mmap(vma, &robj->tobj);
  456. }
  457. unsigned long radeon_object_size(struct radeon_object *robj)
  458. {
  459. return robj->tobj.num_pages << PAGE_SHIFT;
  460. }
  461. int radeon_object_get_surface_reg(struct radeon_object *robj)
  462. {
  463. struct radeon_device *rdev = robj->rdev;
  464. struct radeon_surface_reg *reg;
  465. struct radeon_object *old_object;
  466. int steal;
  467. int i;
  468. if (!robj->tiling_flags)
  469. return 0;
  470. if (robj->surface_reg >= 0) {
  471. reg = &rdev->surface_regs[robj->surface_reg];
  472. i = robj->surface_reg;
  473. goto out;
  474. }
  475. steal = -1;
  476. for (i = 0; i < RADEON_GEM_MAX_SURFACES; i++) {
  477. reg = &rdev->surface_regs[i];
  478. if (!reg->robj)
  479. break;
  480. old_object = reg->robj;
  481. if (old_object->pin_count == 0)
  482. steal = i;
  483. }
  484. /* if we are all out */
  485. if (i == RADEON_GEM_MAX_SURFACES) {
  486. if (steal == -1)
  487. return -ENOMEM;
  488. /* find someone with a surface reg and nuke their BO */
  489. reg = &rdev->surface_regs[steal];
  490. old_object = reg->robj;
  491. /* blow away the mapping */
  492. DRM_DEBUG("stealing surface reg %d from %p\n", steal, old_object);
  493. ttm_bo_unmap_virtual(&old_object->tobj);
  494. old_object->surface_reg = -1;
  495. i = steal;
  496. }
  497. robj->surface_reg = i;
  498. reg->robj = robj;
  499. out:
  500. radeon_set_surface_reg(rdev, i, robj->tiling_flags, robj->pitch,
  501. robj->tobj.mem.mm_node->start << PAGE_SHIFT,
  502. robj->tobj.num_pages << PAGE_SHIFT);
  503. return 0;
  504. }
  505. void radeon_object_clear_surface_reg(struct radeon_object *robj)
  506. {
  507. struct radeon_device *rdev = robj->rdev;
  508. struct radeon_surface_reg *reg;
  509. if (robj->surface_reg == -1)
  510. return;
  511. reg = &rdev->surface_regs[robj->surface_reg];
  512. radeon_clear_surface_reg(rdev, robj->surface_reg);
  513. reg->robj = NULL;
  514. robj->surface_reg = -1;
  515. }
  516. void radeon_object_set_tiling_flags(struct radeon_object *robj,
  517. uint32_t tiling_flags, uint32_t pitch)
  518. {
  519. robj->tiling_flags = tiling_flags;
  520. robj->pitch = pitch;
  521. }
  522. void radeon_object_get_tiling_flags(struct radeon_object *robj,
  523. uint32_t *tiling_flags,
  524. uint32_t *pitch)
  525. {
  526. if (tiling_flags)
  527. *tiling_flags = robj->tiling_flags;
  528. if (pitch)
  529. *pitch = robj->pitch;
  530. }
  531. int radeon_object_check_tiling(struct radeon_object *robj, bool has_moved,
  532. bool force_drop)
  533. {
  534. if (!(robj->tiling_flags & RADEON_TILING_SURFACE))
  535. return 0;
  536. if (force_drop) {
  537. radeon_object_clear_surface_reg(robj);
  538. return 0;
  539. }
  540. if (robj->tobj.mem.mem_type != TTM_PL_VRAM) {
  541. if (!has_moved)
  542. return 0;
  543. if (robj->surface_reg >= 0)
  544. radeon_object_clear_surface_reg(robj);
  545. return 0;
  546. }
  547. if ((robj->surface_reg >= 0) && !has_moved)
  548. return 0;
  549. return radeon_object_get_surface_reg(robj);
  550. }
  551. void radeon_bo_move_notify(struct ttm_buffer_object *bo,
  552. struct ttm_mem_reg *mem)
  553. {
  554. struct radeon_object *robj = container_of(bo, struct radeon_object, tobj);
  555. radeon_object_check_tiling(robj, 0, 1);
  556. }
  557. void radeon_bo_fault_reserve_notify(struct ttm_buffer_object *bo)
  558. {
  559. struct radeon_object *robj = container_of(bo, struct radeon_object, tobj);
  560. radeon_object_check_tiling(robj, 0, 0);
  561. }