radeon_object.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  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. WARN_ON_ONCE(max_offset != 0);
  212. return 0;
  213. }
  214. radeon_ttm_placement_from_domain(bo, domain);
  215. if (domain == RADEON_GEM_DOMAIN_VRAM) {
  216. /* force to pin into visible video ram */
  217. bo->placement.lpfn = bo->rdev->mc.visible_vram_size >> PAGE_SHIFT;
  218. }
  219. if (max_offset) {
  220. u64 lpfn = max_offset >> PAGE_SHIFT;
  221. if (!bo->placement.lpfn)
  222. bo->placement.lpfn = bo->rdev->mc.gtt_size >> PAGE_SHIFT;
  223. if (lpfn < bo->placement.lpfn)
  224. bo->placement.lpfn = lpfn;
  225. }
  226. for (i = 0; i < bo->placement.num_placement; i++)
  227. bo->placements[i] |= TTM_PL_FLAG_NO_EVICT;
  228. r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false, false);
  229. if (likely(r == 0)) {
  230. bo->pin_count = 1;
  231. if (gpu_addr != NULL)
  232. *gpu_addr = radeon_bo_gpu_offset(bo);
  233. }
  234. if (unlikely(r != 0))
  235. dev_err(bo->rdev->dev, "%p pin failed\n", bo);
  236. return r;
  237. }
  238. int radeon_bo_pin(struct radeon_bo *bo, u32 domain, u64 *gpu_addr)
  239. {
  240. return radeon_bo_pin_restricted(bo, domain, 0, gpu_addr);
  241. }
  242. int radeon_bo_unpin(struct radeon_bo *bo)
  243. {
  244. int r, i;
  245. if (!bo->pin_count) {
  246. dev_warn(bo->rdev->dev, "%p unpin not necessary\n", bo);
  247. return 0;
  248. }
  249. bo->pin_count--;
  250. if (bo->pin_count)
  251. return 0;
  252. for (i = 0; i < bo->placement.num_placement; i++)
  253. bo->placements[i] &= ~TTM_PL_FLAG_NO_EVICT;
  254. r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false, false);
  255. if (unlikely(r != 0))
  256. dev_err(bo->rdev->dev, "%p validate failed for unpin\n", bo);
  257. return r;
  258. }
  259. int radeon_bo_evict_vram(struct radeon_device *rdev)
  260. {
  261. /* late 2.6.33 fix IGP hibernate - we need pm ops to do this correct */
  262. if (0 && (rdev->flags & RADEON_IS_IGP)) {
  263. if (rdev->mc.igp_sideport_enabled == false)
  264. /* Useless to evict on IGP chips */
  265. return 0;
  266. }
  267. return ttm_bo_evict_mm(&rdev->mman.bdev, TTM_PL_VRAM);
  268. }
  269. void radeon_bo_force_delete(struct radeon_device *rdev)
  270. {
  271. struct radeon_bo *bo, *n;
  272. if (list_empty(&rdev->gem.objects)) {
  273. return;
  274. }
  275. dev_err(rdev->dev, "Userspace still has active objects !\n");
  276. list_for_each_entry_safe(bo, n, &rdev->gem.objects, list) {
  277. mutex_lock(&rdev->ddev->struct_mutex);
  278. dev_err(rdev->dev, "%p %p %lu %lu force free\n",
  279. &bo->gem_base, bo, (unsigned long)bo->gem_base.size,
  280. *((unsigned long *)&bo->gem_base.refcount));
  281. mutex_lock(&bo->rdev->gem.mutex);
  282. list_del_init(&bo->list);
  283. mutex_unlock(&bo->rdev->gem.mutex);
  284. /* this should unref the ttm bo */
  285. drm_gem_object_unreference(&bo->gem_base);
  286. mutex_unlock(&rdev->ddev->struct_mutex);
  287. }
  288. }
  289. int radeon_bo_init(struct radeon_device *rdev)
  290. {
  291. /* Add an MTRR for the VRAM */
  292. rdev->mc.vram_mtrr = mtrr_add(rdev->mc.aper_base, rdev->mc.aper_size,
  293. MTRR_TYPE_WRCOMB, 1);
  294. DRM_INFO("Detected VRAM RAM=%lluM, BAR=%lluM\n",
  295. rdev->mc.mc_vram_size >> 20,
  296. (unsigned long long)rdev->mc.aper_size >> 20);
  297. DRM_INFO("RAM width %dbits %cDR\n",
  298. rdev->mc.vram_width, rdev->mc.vram_is_ddr ? 'D' : 'S');
  299. return radeon_ttm_init(rdev);
  300. }
  301. void radeon_bo_fini(struct radeon_device *rdev)
  302. {
  303. radeon_ttm_fini(rdev);
  304. }
  305. void radeon_bo_list_add_object(struct radeon_bo_list *lobj,
  306. struct list_head *head)
  307. {
  308. if (lobj->wdomain) {
  309. list_add(&lobj->tv.head, head);
  310. } else {
  311. list_add_tail(&lobj->tv.head, head);
  312. }
  313. }
  314. int radeon_bo_list_validate(struct list_head *head)
  315. {
  316. struct radeon_bo_list *lobj;
  317. struct radeon_bo *bo;
  318. u32 domain;
  319. int r;
  320. r = ttm_eu_reserve_buffers(head);
  321. if (unlikely(r != 0)) {
  322. return r;
  323. }
  324. list_for_each_entry(lobj, head, tv.head) {
  325. bo = lobj->bo;
  326. if (!bo->pin_count) {
  327. domain = lobj->wdomain ? lobj->wdomain : lobj->rdomain;
  328. retry:
  329. radeon_ttm_placement_from_domain(bo, domain);
  330. r = ttm_bo_validate(&bo->tbo, &bo->placement,
  331. true, false, false);
  332. if (unlikely(r)) {
  333. if (r != -ERESTARTSYS && domain == RADEON_GEM_DOMAIN_VRAM) {
  334. domain |= RADEON_GEM_DOMAIN_GTT;
  335. goto retry;
  336. }
  337. return r;
  338. }
  339. }
  340. lobj->gpu_offset = radeon_bo_gpu_offset(bo);
  341. lobj->tiling_flags = bo->tiling_flags;
  342. }
  343. return 0;
  344. }
  345. int radeon_bo_fbdev_mmap(struct radeon_bo *bo,
  346. struct vm_area_struct *vma)
  347. {
  348. return ttm_fbdev_mmap(vma, &bo->tbo);
  349. }
  350. int radeon_bo_get_surface_reg(struct radeon_bo *bo)
  351. {
  352. struct radeon_device *rdev = bo->rdev;
  353. struct radeon_surface_reg *reg;
  354. struct radeon_bo *old_object;
  355. int steal;
  356. int i;
  357. BUG_ON(!atomic_read(&bo->tbo.reserved));
  358. if (!bo->tiling_flags)
  359. return 0;
  360. if (bo->surface_reg >= 0) {
  361. reg = &rdev->surface_regs[bo->surface_reg];
  362. i = bo->surface_reg;
  363. goto out;
  364. }
  365. steal = -1;
  366. for (i = 0; i < RADEON_GEM_MAX_SURFACES; i++) {
  367. reg = &rdev->surface_regs[i];
  368. if (!reg->bo)
  369. break;
  370. old_object = reg->bo;
  371. if (old_object->pin_count == 0)
  372. steal = i;
  373. }
  374. /* if we are all out */
  375. if (i == RADEON_GEM_MAX_SURFACES) {
  376. if (steal == -1)
  377. return -ENOMEM;
  378. /* find someone with a surface reg and nuke their BO */
  379. reg = &rdev->surface_regs[steal];
  380. old_object = reg->bo;
  381. /* blow away the mapping */
  382. DRM_DEBUG("stealing surface reg %d from %p\n", steal, old_object);
  383. ttm_bo_unmap_virtual(&old_object->tbo);
  384. old_object->surface_reg = -1;
  385. i = steal;
  386. }
  387. bo->surface_reg = i;
  388. reg->bo = bo;
  389. out:
  390. radeon_set_surface_reg(rdev, i, bo->tiling_flags, bo->pitch,
  391. bo->tbo.mem.start << PAGE_SHIFT,
  392. bo->tbo.num_pages << PAGE_SHIFT);
  393. return 0;
  394. }
  395. static void radeon_bo_clear_surface_reg(struct radeon_bo *bo)
  396. {
  397. struct radeon_device *rdev = bo->rdev;
  398. struct radeon_surface_reg *reg;
  399. if (bo->surface_reg == -1)
  400. return;
  401. reg = &rdev->surface_regs[bo->surface_reg];
  402. radeon_clear_surface_reg(rdev, bo->surface_reg);
  403. reg->bo = NULL;
  404. bo->surface_reg = -1;
  405. }
  406. int radeon_bo_set_tiling_flags(struct radeon_bo *bo,
  407. uint32_t tiling_flags, uint32_t pitch)
  408. {
  409. struct radeon_device *rdev = bo->rdev;
  410. int r;
  411. if (rdev->family >= CHIP_CEDAR) {
  412. unsigned bankw, bankh, mtaspect, tilesplit, stilesplit;
  413. bankw = (tiling_flags >> RADEON_TILING_EG_BANKW_SHIFT) & RADEON_TILING_EG_BANKW_MASK;
  414. bankh = (tiling_flags >> RADEON_TILING_EG_BANKH_SHIFT) & RADEON_TILING_EG_BANKH_MASK;
  415. mtaspect = (tiling_flags >> RADEON_TILING_EG_MACRO_TILE_ASPECT_SHIFT) & RADEON_TILING_EG_MACRO_TILE_ASPECT_MASK;
  416. tilesplit = (tiling_flags >> RADEON_TILING_EG_TILE_SPLIT_SHIFT) & RADEON_TILING_EG_TILE_SPLIT_MASK;
  417. stilesplit = (tiling_flags >> RADEON_TILING_EG_STENCIL_TILE_SPLIT_SHIFT) & RADEON_TILING_EG_STENCIL_TILE_SPLIT_MASK;
  418. switch (bankw) {
  419. case 0:
  420. case 1:
  421. case 2:
  422. case 4:
  423. case 8:
  424. break;
  425. default:
  426. return -EINVAL;
  427. }
  428. switch (bankh) {
  429. case 0:
  430. case 1:
  431. case 2:
  432. case 4:
  433. case 8:
  434. break;
  435. default:
  436. return -EINVAL;
  437. }
  438. switch (mtaspect) {
  439. case 0:
  440. case 1:
  441. case 2:
  442. case 4:
  443. case 8:
  444. break;
  445. default:
  446. return -EINVAL;
  447. }
  448. if (tilesplit > 6) {
  449. return -EINVAL;
  450. }
  451. if (stilesplit > 6) {
  452. return -EINVAL;
  453. }
  454. }
  455. r = radeon_bo_reserve(bo, false);
  456. if (unlikely(r != 0))
  457. return r;
  458. bo->tiling_flags = tiling_flags;
  459. bo->pitch = pitch;
  460. radeon_bo_unreserve(bo);
  461. return 0;
  462. }
  463. void radeon_bo_get_tiling_flags(struct radeon_bo *bo,
  464. uint32_t *tiling_flags,
  465. uint32_t *pitch)
  466. {
  467. BUG_ON(!atomic_read(&bo->tbo.reserved));
  468. if (tiling_flags)
  469. *tiling_flags = bo->tiling_flags;
  470. if (pitch)
  471. *pitch = bo->pitch;
  472. }
  473. int radeon_bo_check_tiling(struct radeon_bo *bo, bool has_moved,
  474. bool force_drop)
  475. {
  476. BUG_ON(!atomic_read(&bo->tbo.reserved));
  477. if (!(bo->tiling_flags & RADEON_TILING_SURFACE))
  478. return 0;
  479. if (force_drop) {
  480. radeon_bo_clear_surface_reg(bo);
  481. return 0;
  482. }
  483. if (bo->tbo.mem.mem_type != TTM_PL_VRAM) {
  484. if (!has_moved)
  485. return 0;
  486. if (bo->surface_reg >= 0)
  487. radeon_bo_clear_surface_reg(bo);
  488. return 0;
  489. }
  490. if ((bo->surface_reg >= 0) && !has_moved)
  491. return 0;
  492. return radeon_bo_get_surface_reg(bo);
  493. }
  494. void radeon_bo_move_notify(struct ttm_buffer_object *bo,
  495. struct ttm_mem_reg *mem)
  496. {
  497. struct radeon_bo *rbo;
  498. if (!radeon_ttm_bo_is_radeon_bo(bo))
  499. return;
  500. rbo = container_of(bo, struct radeon_bo, tbo);
  501. radeon_bo_check_tiling(rbo, 0, 1);
  502. radeon_vm_bo_invalidate(rbo->rdev, rbo);
  503. }
  504. int radeon_bo_fault_reserve_notify(struct ttm_buffer_object *bo)
  505. {
  506. struct radeon_device *rdev;
  507. struct radeon_bo *rbo;
  508. unsigned long offset, size;
  509. int r;
  510. if (!radeon_ttm_bo_is_radeon_bo(bo))
  511. return 0;
  512. rbo = container_of(bo, struct radeon_bo, tbo);
  513. radeon_bo_check_tiling(rbo, 0, 0);
  514. rdev = rbo->rdev;
  515. if (bo->mem.mem_type == TTM_PL_VRAM) {
  516. size = bo->mem.num_pages << PAGE_SHIFT;
  517. offset = bo->mem.start << PAGE_SHIFT;
  518. if ((offset + size) > rdev->mc.visible_vram_size) {
  519. /* hurrah the memory is not visible ! */
  520. radeon_ttm_placement_from_domain(rbo, RADEON_GEM_DOMAIN_VRAM);
  521. rbo->placement.lpfn = rdev->mc.visible_vram_size >> PAGE_SHIFT;
  522. r = ttm_bo_validate(bo, &rbo->placement, false, true, false);
  523. if (unlikely(r != 0))
  524. return r;
  525. offset = bo->mem.start << PAGE_SHIFT;
  526. /* this should not happen */
  527. if ((offset + size) > rdev->mc.visible_vram_size)
  528. return -EINVAL;
  529. }
  530. }
  531. return 0;
  532. }
  533. int radeon_bo_wait(struct radeon_bo *bo, u32 *mem_type, bool no_wait)
  534. {
  535. int r;
  536. r = ttm_bo_reserve(&bo->tbo, true, no_wait, false, 0);
  537. if (unlikely(r != 0))
  538. return r;
  539. spin_lock(&bo->tbo.bdev->fence_lock);
  540. if (mem_type)
  541. *mem_type = bo->tbo.mem.mem_type;
  542. if (bo->tbo.sync_obj)
  543. r = ttm_bo_wait(&bo->tbo, true, true, no_wait);
  544. spin_unlock(&bo->tbo.bdev->fence_lock);
  545. ttm_bo_unreserve(&bo->tbo);
  546. return r;
  547. }
  548. /**
  549. * radeon_bo_reserve - reserve bo
  550. * @bo: bo structure
  551. * @no_wait: don't sleep while trying to reserve (return -EBUSY)
  552. *
  553. * Returns:
  554. * -EBUSY: buffer is busy and @no_wait is true
  555. * -ERESTARTSYS: A wait for the buffer to become unreserved was interrupted by
  556. * a signal. Release all buffer reservations and return to user-space.
  557. */
  558. int radeon_bo_reserve(struct radeon_bo *bo, bool no_wait)
  559. {
  560. int r;
  561. r = ttm_bo_reserve(&bo->tbo, true, no_wait, false, 0);
  562. if (unlikely(r != 0)) {
  563. if (r != -ERESTARTSYS)
  564. dev_err(bo->rdev->dev, "%p reserve failed\n", bo);
  565. return r;
  566. }
  567. return 0;
  568. }
  569. /* object have to be reserved */
  570. struct radeon_bo_va *radeon_bo_va(struct radeon_bo *rbo, struct radeon_vm *vm)
  571. {
  572. struct radeon_bo_va *bo_va;
  573. list_for_each_entry(bo_va, &rbo->va, bo_list) {
  574. if (bo_va->vm == vm) {
  575. return bo_va;
  576. }
  577. }
  578. return NULL;
  579. }