radeon_object.c 16 KB

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