radeon_object.c 16 KB

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