radeon_object.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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 <linux/slab.h>
  34. #include <drm/drmP.h>
  35. #include "radeon_drm.h"
  36. #include "radeon.h"
  37. #include "radeon_trace.h"
  38. int radeon_ttm_init(struct radeon_device *rdev);
  39. void radeon_ttm_fini(struct radeon_device *rdev);
  40. static void radeon_bo_clear_surface_reg(struct radeon_bo *bo);
  41. /*
  42. * To exclude mutual BO access we rely on bo_reserve exclusion, as all
  43. * function are calling it.
  44. */
  45. static void radeon_ttm_bo_destroy(struct ttm_buffer_object *tbo)
  46. {
  47. struct radeon_bo *bo;
  48. bo = container_of(tbo, struct radeon_bo, tbo);
  49. mutex_lock(&bo->rdev->gem.mutex);
  50. list_del_init(&bo->list);
  51. mutex_unlock(&bo->rdev->gem.mutex);
  52. radeon_bo_clear_surface_reg(bo);
  53. drm_gem_object_release(&bo->gem_base);
  54. kfree(bo);
  55. }
  56. bool radeon_ttm_bo_is_radeon_bo(struct ttm_buffer_object *bo)
  57. {
  58. if (bo->destroy == &radeon_ttm_bo_destroy)
  59. return true;
  60. return false;
  61. }
  62. void radeon_ttm_placement_from_domain(struct radeon_bo *rbo, u32 domain)
  63. {
  64. u32 c = 0;
  65. rbo->placement.fpfn = 0;
  66. rbo->placement.lpfn = 0;
  67. rbo->placement.placement = rbo->placements;
  68. rbo->placement.busy_placement = rbo->placements;
  69. if (domain & RADEON_GEM_DOMAIN_VRAM)
  70. rbo->placements[c++] = TTM_PL_FLAG_WC | TTM_PL_FLAG_UNCACHED |
  71. TTM_PL_FLAG_VRAM;
  72. if (domain & RADEON_GEM_DOMAIN_GTT)
  73. rbo->placements[c++] = TTM_PL_MASK_CACHING | TTM_PL_FLAG_TT;
  74. if (domain & RADEON_GEM_DOMAIN_CPU)
  75. rbo->placements[c++] = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM;
  76. if (!c)
  77. rbo->placements[c++] = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM;
  78. rbo->placement.num_placement = c;
  79. rbo->placement.num_busy_placement = c;
  80. }
  81. int radeon_bo_create(struct radeon_device *rdev,
  82. unsigned long size, int byte_align, bool kernel, u32 domain,
  83. struct radeon_bo **bo_ptr)
  84. {
  85. struct radeon_bo *bo;
  86. enum ttm_bo_type type;
  87. unsigned long page_align = roundup(byte_align, PAGE_SIZE) >> PAGE_SHIFT;
  88. unsigned long max_size = 0;
  89. size_t acc_size;
  90. int r;
  91. size = ALIGN(size, PAGE_SIZE);
  92. if (unlikely(rdev->mman.bdev.dev_mapping == NULL)) {
  93. rdev->mman.bdev.dev_mapping = rdev->ddev->dev_mapping;
  94. }
  95. if (kernel) {
  96. type = ttm_bo_type_kernel;
  97. } else {
  98. type = ttm_bo_type_device;
  99. }
  100. *bo_ptr = NULL;
  101. /* maximun bo size is the minimun btw visible vram and gtt size */
  102. max_size = min(rdev->mc.visible_vram_size, rdev->mc.gtt_size);
  103. if ((page_align << PAGE_SHIFT) >= max_size) {
  104. printk(KERN_WARNING "%s:%d alloc size %ldM bigger than %ldMb limit\n",
  105. __func__, __LINE__, page_align >> (20 - PAGE_SHIFT), max_size >> 20);
  106. return -ENOMEM;
  107. }
  108. acc_size = ttm_bo_dma_acc_size(&rdev->mman.bdev, size,
  109. sizeof(struct radeon_bo));
  110. retry:
  111. bo = kzalloc(sizeof(struct radeon_bo), GFP_KERNEL);
  112. if (bo == NULL)
  113. return -ENOMEM;
  114. r = drm_gem_object_init(rdev->ddev, &bo->gem_base, size);
  115. if (unlikely(r)) {
  116. kfree(bo);
  117. return r;
  118. }
  119. bo->rdev = rdev;
  120. bo->gem_base.driver_private = NULL;
  121. bo->surface_reg = -1;
  122. INIT_LIST_HEAD(&bo->list);
  123. radeon_ttm_placement_from_domain(bo, domain);
  124. /* Kernel allocation are uninterruptible */
  125. mutex_lock(&rdev->vram_mutex);
  126. r = ttm_bo_init(&rdev->mman.bdev, &bo->tbo, size, type,
  127. &bo->placement, page_align, 0, !kernel, NULL,
  128. acc_size, &radeon_ttm_bo_destroy);
  129. mutex_unlock(&rdev->vram_mutex);
  130. if (unlikely(r != 0)) {
  131. if (r != -ERESTARTSYS) {
  132. if (domain == RADEON_GEM_DOMAIN_VRAM) {
  133. domain |= RADEON_GEM_DOMAIN_GTT;
  134. goto retry;
  135. }
  136. dev_err(rdev->dev,
  137. "object_init failed for (%lu, 0x%08X)\n",
  138. size, domain);
  139. }
  140. return r;
  141. }
  142. *bo_ptr = bo;
  143. trace_radeon_bo_create(bo);
  144. return 0;
  145. }
  146. int radeon_bo_kmap(struct radeon_bo *bo, void **ptr)
  147. {
  148. bool is_iomem;
  149. int r;
  150. if (bo->kptr) {
  151. if (ptr) {
  152. *ptr = bo->kptr;
  153. }
  154. return 0;
  155. }
  156. r = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages, &bo->kmap);
  157. if (r) {
  158. return r;
  159. }
  160. bo->kptr = ttm_kmap_obj_virtual(&bo->kmap, &is_iomem);
  161. if (ptr) {
  162. *ptr = bo->kptr;
  163. }
  164. radeon_bo_check_tiling(bo, 0, 0);
  165. return 0;
  166. }
  167. void radeon_bo_kunmap(struct radeon_bo *bo)
  168. {
  169. if (bo->kptr == NULL)
  170. return;
  171. bo->kptr = NULL;
  172. radeon_bo_check_tiling(bo, 0, 0);
  173. ttm_bo_kunmap(&bo->kmap);
  174. }
  175. void radeon_bo_unref(struct radeon_bo **bo)
  176. {
  177. struct ttm_buffer_object *tbo;
  178. struct radeon_device *rdev;
  179. if ((*bo) == NULL)
  180. return;
  181. rdev = (*bo)->rdev;
  182. tbo = &((*bo)->tbo);
  183. mutex_lock(&rdev->vram_mutex);
  184. ttm_bo_unref(&tbo);
  185. mutex_unlock(&rdev->vram_mutex);
  186. if (tbo == NULL)
  187. *bo = NULL;
  188. }
  189. int radeon_bo_pin(struct radeon_bo *bo, u32 domain, u64 *gpu_addr)
  190. {
  191. int r, i;
  192. if (bo->pin_count) {
  193. bo->pin_count++;
  194. if (gpu_addr)
  195. *gpu_addr = radeon_bo_gpu_offset(bo);
  196. return 0;
  197. }
  198. radeon_ttm_placement_from_domain(bo, domain);
  199. if (domain == RADEON_GEM_DOMAIN_VRAM) {
  200. /* force to pin into visible video ram */
  201. bo->placement.lpfn = bo->rdev->mc.visible_vram_size >> PAGE_SHIFT;
  202. }
  203. for (i = 0; i < bo->placement.num_placement; i++)
  204. bo->placements[i] |= TTM_PL_FLAG_NO_EVICT;
  205. r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false, false);
  206. if (likely(r == 0)) {
  207. bo->pin_count = 1;
  208. if (gpu_addr != NULL)
  209. *gpu_addr = radeon_bo_gpu_offset(bo);
  210. }
  211. if (unlikely(r != 0))
  212. dev_err(bo->rdev->dev, "%p pin failed\n", bo);
  213. return r;
  214. }
  215. int radeon_bo_unpin(struct radeon_bo *bo)
  216. {
  217. int r, i;
  218. if (!bo->pin_count) {
  219. dev_warn(bo->rdev->dev, "%p unpin not necessary\n", bo);
  220. return 0;
  221. }
  222. bo->pin_count--;
  223. if (bo->pin_count)
  224. return 0;
  225. for (i = 0; i < bo->placement.num_placement; i++)
  226. bo->placements[i] &= ~TTM_PL_FLAG_NO_EVICT;
  227. r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false, false);
  228. if (unlikely(r != 0))
  229. dev_err(bo->rdev->dev, "%p validate failed for unpin\n", bo);
  230. return r;
  231. }
  232. int radeon_bo_evict_vram(struct radeon_device *rdev)
  233. {
  234. /* late 2.6.33 fix IGP hibernate - we need pm ops to do this correct */
  235. if (0 && (rdev->flags & RADEON_IS_IGP)) {
  236. if (rdev->mc.igp_sideport_enabled == false)
  237. /* Useless to evict on IGP chips */
  238. return 0;
  239. }
  240. return ttm_bo_evict_mm(&rdev->mman.bdev, TTM_PL_VRAM);
  241. }
  242. void radeon_bo_force_delete(struct radeon_device *rdev)
  243. {
  244. struct radeon_bo *bo, *n;
  245. if (list_empty(&rdev->gem.objects)) {
  246. return;
  247. }
  248. dev_err(rdev->dev, "Userspace still has active objects !\n");
  249. list_for_each_entry_safe(bo, n, &rdev->gem.objects, list) {
  250. mutex_lock(&rdev->ddev->struct_mutex);
  251. dev_err(rdev->dev, "%p %p %lu %lu force free\n",
  252. &bo->gem_base, bo, (unsigned long)bo->gem_base.size,
  253. *((unsigned long *)&bo->gem_base.refcount));
  254. mutex_lock(&bo->rdev->gem.mutex);
  255. list_del_init(&bo->list);
  256. mutex_unlock(&bo->rdev->gem.mutex);
  257. /* this should unref the ttm bo */
  258. drm_gem_object_unreference(&bo->gem_base);
  259. mutex_unlock(&rdev->ddev->struct_mutex);
  260. }
  261. }
  262. int radeon_bo_init(struct radeon_device *rdev)
  263. {
  264. /* Add an MTRR for the VRAM */
  265. rdev->mc.vram_mtrr = mtrr_add(rdev->mc.aper_base, rdev->mc.aper_size,
  266. MTRR_TYPE_WRCOMB, 1);
  267. DRM_INFO("Detected VRAM RAM=%lluM, BAR=%lluM\n",
  268. rdev->mc.mc_vram_size >> 20,
  269. (unsigned long long)rdev->mc.aper_size >> 20);
  270. DRM_INFO("RAM width %dbits %cDR\n",
  271. rdev->mc.vram_width, rdev->mc.vram_is_ddr ? 'D' : 'S');
  272. return radeon_ttm_init(rdev);
  273. }
  274. void radeon_bo_fini(struct radeon_device *rdev)
  275. {
  276. radeon_ttm_fini(rdev);
  277. }
  278. void radeon_bo_list_add_object(struct radeon_bo_list *lobj,
  279. struct list_head *head)
  280. {
  281. if (lobj->wdomain) {
  282. list_add(&lobj->tv.head, head);
  283. } else {
  284. list_add_tail(&lobj->tv.head, head);
  285. }
  286. }
  287. int radeon_bo_list_validate(struct list_head *head)
  288. {
  289. struct radeon_bo_list *lobj;
  290. struct radeon_bo *bo;
  291. u32 domain;
  292. int r;
  293. r = ttm_eu_reserve_buffers(head);
  294. if (unlikely(r != 0)) {
  295. return r;
  296. }
  297. list_for_each_entry(lobj, head, tv.head) {
  298. bo = lobj->bo;
  299. if (!bo->pin_count) {
  300. domain = lobj->wdomain ? lobj->wdomain : lobj->rdomain;
  301. retry:
  302. radeon_ttm_placement_from_domain(bo, domain);
  303. r = ttm_bo_validate(&bo->tbo, &bo->placement,
  304. true, false, false);
  305. if (unlikely(r)) {
  306. if (r != -ERESTARTSYS && domain == RADEON_GEM_DOMAIN_VRAM) {
  307. domain |= RADEON_GEM_DOMAIN_GTT;
  308. goto retry;
  309. }
  310. return r;
  311. }
  312. }
  313. lobj->gpu_offset = radeon_bo_gpu_offset(bo);
  314. lobj->tiling_flags = bo->tiling_flags;
  315. }
  316. return 0;
  317. }
  318. int radeon_bo_fbdev_mmap(struct radeon_bo *bo,
  319. struct vm_area_struct *vma)
  320. {
  321. return ttm_fbdev_mmap(vma, &bo->tbo);
  322. }
  323. int radeon_bo_get_surface_reg(struct radeon_bo *bo)
  324. {
  325. struct radeon_device *rdev = bo->rdev;
  326. struct radeon_surface_reg *reg;
  327. struct radeon_bo *old_object;
  328. int steal;
  329. int i;
  330. BUG_ON(!atomic_read(&bo->tbo.reserved));
  331. if (!bo->tiling_flags)
  332. return 0;
  333. if (bo->surface_reg >= 0) {
  334. reg = &rdev->surface_regs[bo->surface_reg];
  335. i = bo->surface_reg;
  336. goto out;
  337. }
  338. steal = -1;
  339. for (i = 0; i < RADEON_GEM_MAX_SURFACES; i++) {
  340. reg = &rdev->surface_regs[i];
  341. if (!reg->bo)
  342. break;
  343. old_object = reg->bo;
  344. if (old_object->pin_count == 0)
  345. steal = i;
  346. }
  347. /* if we are all out */
  348. if (i == RADEON_GEM_MAX_SURFACES) {
  349. if (steal == -1)
  350. return -ENOMEM;
  351. /* find someone with a surface reg and nuke their BO */
  352. reg = &rdev->surface_regs[steal];
  353. old_object = reg->bo;
  354. /* blow away the mapping */
  355. DRM_DEBUG("stealing surface reg %d from %p\n", steal, old_object);
  356. ttm_bo_unmap_virtual(&old_object->tbo);
  357. old_object->surface_reg = -1;
  358. i = steal;
  359. }
  360. bo->surface_reg = i;
  361. reg->bo = bo;
  362. out:
  363. radeon_set_surface_reg(rdev, i, bo->tiling_flags, bo->pitch,
  364. bo->tbo.mem.start << PAGE_SHIFT,
  365. bo->tbo.num_pages << PAGE_SHIFT);
  366. return 0;
  367. }
  368. static void radeon_bo_clear_surface_reg(struct radeon_bo *bo)
  369. {
  370. struct radeon_device *rdev = bo->rdev;
  371. struct radeon_surface_reg *reg;
  372. if (bo->surface_reg == -1)
  373. return;
  374. reg = &rdev->surface_regs[bo->surface_reg];
  375. radeon_clear_surface_reg(rdev, bo->surface_reg);
  376. reg->bo = NULL;
  377. bo->surface_reg = -1;
  378. }
  379. int radeon_bo_set_tiling_flags(struct radeon_bo *bo,
  380. uint32_t tiling_flags, uint32_t pitch)
  381. {
  382. int r;
  383. r = radeon_bo_reserve(bo, false);
  384. if (unlikely(r != 0))
  385. return r;
  386. bo->tiling_flags = tiling_flags;
  387. bo->pitch = pitch;
  388. radeon_bo_unreserve(bo);
  389. return 0;
  390. }
  391. void radeon_bo_get_tiling_flags(struct radeon_bo *bo,
  392. uint32_t *tiling_flags,
  393. uint32_t *pitch)
  394. {
  395. BUG_ON(!atomic_read(&bo->tbo.reserved));
  396. if (tiling_flags)
  397. *tiling_flags = bo->tiling_flags;
  398. if (pitch)
  399. *pitch = bo->pitch;
  400. }
  401. int radeon_bo_check_tiling(struct radeon_bo *bo, bool has_moved,
  402. bool force_drop)
  403. {
  404. BUG_ON(!atomic_read(&bo->tbo.reserved));
  405. if (!(bo->tiling_flags & RADEON_TILING_SURFACE))
  406. return 0;
  407. if (force_drop) {
  408. radeon_bo_clear_surface_reg(bo);
  409. return 0;
  410. }
  411. if (bo->tbo.mem.mem_type != TTM_PL_VRAM) {
  412. if (!has_moved)
  413. return 0;
  414. if (bo->surface_reg >= 0)
  415. radeon_bo_clear_surface_reg(bo);
  416. return 0;
  417. }
  418. if ((bo->surface_reg >= 0) && !has_moved)
  419. return 0;
  420. return radeon_bo_get_surface_reg(bo);
  421. }
  422. void radeon_bo_move_notify(struct ttm_buffer_object *bo,
  423. struct ttm_mem_reg *mem)
  424. {
  425. struct radeon_bo *rbo;
  426. if (!radeon_ttm_bo_is_radeon_bo(bo))
  427. return;
  428. rbo = container_of(bo, struct radeon_bo, tbo);
  429. radeon_bo_check_tiling(rbo, 0, 1);
  430. }
  431. int radeon_bo_fault_reserve_notify(struct ttm_buffer_object *bo)
  432. {
  433. struct radeon_device *rdev;
  434. struct radeon_bo *rbo;
  435. unsigned long offset, size;
  436. int r;
  437. if (!radeon_ttm_bo_is_radeon_bo(bo))
  438. return 0;
  439. rbo = container_of(bo, struct radeon_bo, tbo);
  440. radeon_bo_check_tiling(rbo, 0, 0);
  441. rdev = rbo->rdev;
  442. if (bo->mem.mem_type == TTM_PL_VRAM) {
  443. size = bo->mem.num_pages << PAGE_SHIFT;
  444. offset = bo->mem.start << PAGE_SHIFT;
  445. if ((offset + size) > rdev->mc.visible_vram_size) {
  446. /* hurrah the memory is not visible ! */
  447. radeon_ttm_placement_from_domain(rbo, RADEON_GEM_DOMAIN_VRAM);
  448. rbo->placement.lpfn = rdev->mc.visible_vram_size >> PAGE_SHIFT;
  449. r = ttm_bo_validate(bo, &rbo->placement, false, true, false);
  450. if (unlikely(r != 0))
  451. return r;
  452. offset = bo->mem.start << PAGE_SHIFT;
  453. /* this should not happen */
  454. if ((offset + size) > rdev->mc.visible_vram_size)
  455. return -EINVAL;
  456. }
  457. }
  458. return 0;
  459. }
  460. int radeon_bo_wait(struct radeon_bo *bo, u32 *mem_type, bool no_wait)
  461. {
  462. int r;
  463. r = ttm_bo_reserve(&bo->tbo, true, no_wait, false, 0);
  464. if (unlikely(r != 0))
  465. return r;
  466. spin_lock(&bo->tbo.bdev->fence_lock);
  467. if (mem_type)
  468. *mem_type = bo->tbo.mem.mem_type;
  469. if (bo->tbo.sync_obj)
  470. r = ttm_bo_wait(&bo->tbo, true, true, no_wait);
  471. spin_unlock(&bo->tbo.bdev->fence_lock);
  472. ttm_bo_unreserve(&bo->tbo);
  473. return r;
  474. }
  475. /**
  476. * radeon_bo_reserve - reserve bo
  477. * @bo: bo structure
  478. * @no_wait: don't sleep while trying to reserve (return -EBUSY)
  479. *
  480. * Returns:
  481. * -EBUSY: buffer is busy and @no_wait is true
  482. * -ERESTARTSYS: A wait for the buffer to become unreserved was interrupted by
  483. * a signal. Release all buffer reservations and return to user-space.
  484. */
  485. int radeon_bo_reserve(struct radeon_bo *bo, bool no_wait)
  486. {
  487. int r;
  488. r = ttm_bo_reserve(&bo->tbo, true, no_wait, false, 0);
  489. if (unlikely(r != 0)) {
  490. if (r != -ERESTARTSYS)
  491. dev_err(bo->rdev->dev, "%p reserve failed\n", bo);
  492. return r;
  493. }
  494. return 0;
  495. }