radeon_object.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  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/io.h>
  33. #include <linux/list.h>
  34. #include <linux/slab.h>
  35. #include <drm/drm_cache.h>
  36. #include <drm/drm_prime.h>
  37. #include <drm/radeon_drm.h>
  38. #include "radeon.h"
  39. #include "radeon_trace.h"
  40. #include "radeon_ttm.h"
  41. static void radeon_bo_clear_surface_reg(struct radeon_bo *bo);
  42. /*
  43. * To exclude mutual BO access we rely on bo_reserve exclusion, as all
  44. * function are calling it.
  45. */
  46. static void radeon_ttm_bo_destroy(struct ttm_buffer_object *tbo)
  47. {
  48. struct radeon_bo *bo;
  49. bo = container_of(tbo, struct radeon_bo, tbo);
  50. mutex_lock(&bo->rdev->gem.mutex);
  51. list_del_init(&bo->list);
  52. mutex_unlock(&bo->rdev->gem.mutex);
  53. radeon_bo_clear_surface_reg(bo);
  54. WARN_ON_ONCE(!list_empty(&bo->va));
  55. if (bo->tbo.base.import_attach)
  56. drm_prime_gem_destroy(&bo->tbo.base, bo->tbo.sg);
  57. drm_gem_object_release(&bo->tbo.base);
  58. kfree(bo);
  59. }
  60. bool radeon_ttm_bo_is_radeon_bo(struct ttm_buffer_object *bo)
  61. {
  62. if (bo->destroy == &radeon_ttm_bo_destroy)
  63. return true;
  64. return false;
  65. }
  66. void radeon_ttm_placement_from_domain(struct radeon_bo *rbo, u32 domain)
  67. {
  68. u32 c = 0, i;
  69. rbo->placement.placement = rbo->placements;
  70. if (domain & RADEON_GEM_DOMAIN_VRAM) {
  71. /* Try placing BOs which don't need CPU access outside of the
  72. * CPU accessible part of VRAM
  73. */
  74. if ((rbo->flags & RADEON_GEM_NO_CPU_ACCESS) &&
  75. rbo->rdev->mc.visible_vram_size < rbo->rdev->mc.real_vram_size) {
  76. rbo->placements[c].fpfn =
  77. rbo->rdev->mc.visible_vram_size >> PAGE_SHIFT;
  78. rbo->placements[c].mem_type = TTM_PL_VRAM;
  79. rbo->placements[c++].flags = 0;
  80. }
  81. rbo->placements[c].fpfn = 0;
  82. rbo->placements[c].mem_type = TTM_PL_VRAM;
  83. rbo->placements[c++].flags = 0;
  84. }
  85. if (domain & RADEON_GEM_DOMAIN_GTT) {
  86. rbo->placements[c].fpfn = 0;
  87. rbo->placements[c].mem_type = TTM_PL_TT;
  88. rbo->placements[c++].flags = 0;
  89. }
  90. if (domain & RADEON_GEM_DOMAIN_CPU) {
  91. rbo->placements[c].fpfn = 0;
  92. rbo->placements[c].mem_type = TTM_PL_SYSTEM;
  93. rbo->placements[c++].flags = 0;
  94. }
  95. if (!c) {
  96. rbo->placements[c].fpfn = 0;
  97. rbo->placements[c].mem_type = TTM_PL_SYSTEM;
  98. rbo->placements[c++].flags = 0;
  99. }
  100. rbo->placement.num_placement = c;
  101. for (i = 0; i < c; ++i) {
  102. if ((rbo->flags & RADEON_GEM_CPU_ACCESS) &&
  103. (rbo->placements[i].mem_type == TTM_PL_VRAM) &&
  104. !rbo->placements[i].fpfn)
  105. rbo->placements[i].lpfn =
  106. rbo->rdev->mc.visible_vram_size >> PAGE_SHIFT;
  107. else
  108. rbo->placements[i].lpfn = 0;
  109. }
  110. }
  111. int radeon_bo_create(struct radeon_device *rdev,
  112. unsigned long size, int byte_align, bool kernel,
  113. u32 domain, u32 flags, struct sg_table *sg,
  114. struct dma_resv *resv,
  115. struct radeon_bo **bo_ptr)
  116. {
  117. struct radeon_bo *bo;
  118. enum ttm_bo_type type;
  119. unsigned long page_align = roundup(byte_align, PAGE_SIZE) >> PAGE_SHIFT;
  120. int r;
  121. size = ALIGN(size, PAGE_SIZE);
  122. if (kernel) {
  123. type = ttm_bo_type_kernel;
  124. } else if (sg) {
  125. type = ttm_bo_type_sg;
  126. } else {
  127. type = ttm_bo_type_device;
  128. }
  129. *bo_ptr = NULL;
  130. bo = kzalloc(sizeof(struct radeon_bo), GFP_KERNEL);
  131. if (bo == NULL)
  132. return -ENOMEM;
  133. drm_gem_private_object_init(rdev_to_drm(rdev), &bo->tbo.base, size);
  134. bo->tbo.base.funcs = &radeon_gem_object_funcs;
  135. bo->rdev = rdev;
  136. bo->surface_reg = -1;
  137. INIT_LIST_HEAD(&bo->list);
  138. INIT_LIST_HEAD(&bo->va);
  139. bo->initial_domain = domain & (RADEON_GEM_DOMAIN_VRAM |
  140. RADEON_GEM_DOMAIN_GTT |
  141. RADEON_GEM_DOMAIN_CPU);
  142. bo->flags = flags;
  143. /* PCI GART is always snooped */
  144. if (!(rdev->flags & RADEON_IS_PCIE))
  145. bo->flags &= ~(RADEON_GEM_GTT_WC | RADEON_GEM_GTT_UC);
  146. /* Write-combined CPU mappings of GTT cause GPU hangs with RV6xx
  147. * See https://bugs.freedesktop.org/show_bug.cgi?id=91268
  148. */
  149. if (rdev->family >= CHIP_RV610 && rdev->family <= CHIP_RV635)
  150. bo->flags &= ~(RADEON_GEM_GTT_WC | RADEON_GEM_GTT_UC);
  151. #ifdef CONFIG_X86_32
  152. /* XXX: Write-combined CPU mappings of GTT seem broken on 32-bit
  153. * See https://bugs.freedesktop.org/show_bug.cgi?id=84627
  154. */
  155. bo->flags &= ~(RADEON_GEM_GTT_WC | RADEON_GEM_GTT_UC);
  156. #elif defined(CONFIG_X86) && !defined(CONFIG_X86_PAT)
  157. /* Don't try to enable write-combining when it can't work, or things
  158. * may be slow
  159. * See https://bugs.freedesktop.org/show_bug.cgi?id=88758
  160. */
  161. #ifndef CONFIG_COMPILE_TEST
  162. #warning Please enable CONFIG_MTRR and CONFIG_X86_PAT for better performance \
  163. thanks to write-combining
  164. #endif
  165. if (bo->flags & RADEON_GEM_GTT_WC)
  166. DRM_INFO_ONCE("Please enable CONFIG_MTRR and CONFIG_X86_PAT for "
  167. "better performance thanks to write-combining\n");
  168. bo->flags &= ~(RADEON_GEM_GTT_WC | RADEON_GEM_GTT_UC);
  169. #else
  170. /* For architectures that don't support WC memory,
  171. * mask out the WC flag from the BO
  172. */
  173. if (!drm_arch_can_wc_memory())
  174. bo->flags &= ~RADEON_GEM_GTT_WC;
  175. #endif
  176. radeon_ttm_placement_from_domain(bo, domain);
  177. /* Kernel allocation are uninterruptible */
  178. down_read(&rdev->pm.mclk_lock);
  179. r = ttm_bo_init_validate(&rdev->mman.bdev, &bo->tbo, type,
  180. &bo->placement, page_align, !kernel, sg, resv,
  181. &radeon_ttm_bo_destroy);
  182. up_read(&rdev->pm.mclk_lock);
  183. if (unlikely(r != 0)) {
  184. return r;
  185. }
  186. *bo_ptr = bo;
  187. trace_radeon_bo_create(bo);
  188. return 0;
  189. }
  190. int radeon_bo_kmap(struct radeon_bo *bo, void **ptr)
  191. {
  192. bool is_iomem;
  193. long r;
  194. r = dma_resv_wait_timeout(bo->tbo.base.resv, DMA_RESV_USAGE_KERNEL,
  195. false, MAX_SCHEDULE_TIMEOUT);
  196. if (r < 0)
  197. return r;
  198. if (bo->kptr) {
  199. if (ptr) {
  200. *ptr = bo->kptr;
  201. }
  202. return 0;
  203. }
  204. r = ttm_bo_kmap(&bo->tbo, 0, PFN_UP(bo->tbo.base.size), &bo->kmap);
  205. if (r) {
  206. return r;
  207. }
  208. bo->kptr = ttm_kmap_obj_virtual(&bo->kmap, &is_iomem);
  209. if (ptr) {
  210. *ptr = bo->kptr;
  211. }
  212. radeon_bo_check_tiling(bo, 0, 0);
  213. return 0;
  214. }
  215. void radeon_bo_kunmap(struct radeon_bo *bo)
  216. {
  217. if (bo->kptr == NULL)
  218. return;
  219. bo->kptr = NULL;
  220. radeon_bo_check_tiling(bo, 0, 0);
  221. ttm_bo_kunmap(&bo->kmap);
  222. }
  223. struct radeon_bo *radeon_bo_ref(struct radeon_bo *bo)
  224. {
  225. if (bo == NULL)
  226. return NULL;
  227. drm_gem_object_get(&bo->tbo.base);
  228. return bo;
  229. }
  230. void radeon_bo_unref(struct radeon_bo **bo)
  231. {
  232. if ((*bo) == NULL)
  233. return;
  234. drm_gem_object_put(&(*bo)->tbo.base);
  235. *bo = NULL;
  236. }
  237. int radeon_bo_pin_restricted(struct radeon_bo *bo, u32 domain, u64 max_offset,
  238. u64 *gpu_addr)
  239. {
  240. struct ttm_operation_ctx ctx = { false, false };
  241. int r, i;
  242. if (radeon_ttm_tt_has_userptr(bo->rdev, bo->tbo.ttm))
  243. return -EPERM;
  244. if (bo->tbo.pin_count) {
  245. ttm_bo_pin(&bo->tbo);
  246. if (gpu_addr)
  247. *gpu_addr = radeon_bo_gpu_offset(bo);
  248. if (max_offset != 0) {
  249. u64 domain_start;
  250. if (domain == RADEON_GEM_DOMAIN_VRAM)
  251. domain_start = bo->rdev->mc.vram_start;
  252. else
  253. domain_start = bo->rdev->mc.gtt_start;
  254. WARN_ON_ONCE(max_offset <
  255. (radeon_bo_gpu_offset(bo) - domain_start));
  256. }
  257. return 0;
  258. }
  259. if (bo->prime_shared_count && domain == RADEON_GEM_DOMAIN_VRAM) {
  260. /* A BO shared as a dma-buf cannot be sensibly migrated to VRAM */
  261. return -EINVAL;
  262. }
  263. radeon_ttm_placement_from_domain(bo, domain);
  264. for (i = 0; i < bo->placement.num_placement; i++) {
  265. /* force to pin into visible video ram */
  266. if ((bo->placements[i].mem_type == TTM_PL_VRAM) &&
  267. !(bo->flags & RADEON_GEM_NO_CPU_ACCESS) &&
  268. (!max_offset || max_offset > bo->rdev->mc.visible_vram_size))
  269. bo->placements[i].lpfn =
  270. bo->rdev->mc.visible_vram_size >> PAGE_SHIFT;
  271. else
  272. bo->placements[i].lpfn = max_offset >> PAGE_SHIFT;
  273. }
  274. r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
  275. if (likely(r == 0)) {
  276. ttm_bo_pin(&bo->tbo);
  277. if (gpu_addr != NULL)
  278. *gpu_addr = radeon_bo_gpu_offset(bo);
  279. if (domain == RADEON_GEM_DOMAIN_VRAM)
  280. bo->rdev->vram_pin_size += radeon_bo_size(bo);
  281. else
  282. bo->rdev->gart_pin_size += radeon_bo_size(bo);
  283. } else {
  284. dev_err(bo->rdev->dev, "%p pin failed\n", bo);
  285. }
  286. return r;
  287. }
  288. int radeon_bo_pin(struct radeon_bo *bo, u32 domain, u64 *gpu_addr)
  289. {
  290. return radeon_bo_pin_restricted(bo, domain, 0, gpu_addr);
  291. }
  292. void radeon_bo_unpin(struct radeon_bo *bo)
  293. {
  294. ttm_bo_unpin(&bo->tbo);
  295. if (!bo->tbo.pin_count) {
  296. if (bo->tbo.resource->mem_type == TTM_PL_VRAM)
  297. bo->rdev->vram_pin_size -= radeon_bo_size(bo);
  298. else
  299. bo->rdev->gart_pin_size -= radeon_bo_size(bo);
  300. }
  301. }
  302. int radeon_bo_evict_vram(struct radeon_device *rdev)
  303. {
  304. struct ttm_device *bdev = &rdev->mman.bdev;
  305. struct ttm_resource_manager *man;
  306. /* late 2.6.33 fix IGP hibernate - we need pm ops to do this correct */
  307. #ifndef CONFIG_HIBERNATION
  308. if (rdev->flags & RADEON_IS_IGP) {
  309. if (rdev->mc.igp_sideport_enabled == false)
  310. /* Useless to evict on IGP chips */
  311. return 0;
  312. }
  313. #endif
  314. man = ttm_manager_type(bdev, TTM_PL_VRAM);
  315. if (!man)
  316. return 0;
  317. return ttm_resource_manager_evict_all(bdev, man);
  318. }
  319. void radeon_bo_force_delete(struct radeon_device *rdev)
  320. {
  321. struct radeon_bo *bo, *n;
  322. if (list_empty(&rdev->gem.objects)) {
  323. return;
  324. }
  325. dev_err(rdev->dev, "Userspace still has active objects !\n");
  326. list_for_each_entry_safe(bo, n, &rdev->gem.objects, list) {
  327. dev_err(rdev->dev, "%p %p %lu %lu force free\n",
  328. &bo->tbo.base, bo, (unsigned long)bo->tbo.base.size,
  329. *((unsigned long *)&bo->tbo.base.refcount));
  330. mutex_lock(&bo->rdev->gem.mutex);
  331. list_del_init(&bo->list);
  332. mutex_unlock(&bo->rdev->gem.mutex);
  333. /* this should unref the ttm bo */
  334. drm_gem_object_put(&bo->tbo.base);
  335. }
  336. }
  337. int radeon_bo_init(struct radeon_device *rdev)
  338. {
  339. /* reserve PAT memory space to WC for VRAM */
  340. arch_io_reserve_memtype_wc(rdev->mc.aper_base,
  341. rdev->mc.aper_size);
  342. /* Add an MTRR for the VRAM */
  343. if (!rdev->fastfb_working) {
  344. rdev->mc.vram_mtrr = arch_phys_wc_add(rdev->mc.aper_base,
  345. rdev->mc.aper_size);
  346. }
  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_bo_fini(struct radeon_device *rdev)
  355. {
  356. radeon_ttm_fini(rdev);
  357. arch_phys_wc_del(rdev->mc.vram_mtrr);
  358. arch_io_free_memtype_wc(rdev->mc.aper_base, rdev->mc.aper_size);
  359. }
  360. /* Returns how many bytes TTM can move per IB.
  361. */
  362. static u64 radeon_bo_get_threshold_for_moves(struct radeon_device *rdev)
  363. {
  364. u64 real_vram_size = rdev->mc.real_vram_size;
  365. struct ttm_resource_manager *man =
  366. ttm_manager_type(&rdev->mman.bdev, TTM_PL_VRAM);
  367. u64 vram_usage = ttm_resource_manager_usage(man);
  368. /* This function is based on the current VRAM usage.
  369. *
  370. * - If all of VRAM is free, allow relocating the number of bytes that
  371. * is equal to 1/4 of the size of VRAM for this IB.
  372. * - If more than one half of VRAM is occupied, only allow relocating
  373. * 1 MB of data for this IB.
  374. *
  375. * - From 0 to one half of used VRAM, the threshold decreases
  376. * linearly.
  377. * __________________
  378. * 1/4 of -|\ |
  379. * VRAM | \ |
  380. * | \ |
  381. * | \ |
  382. * | \ |
  383. * | \ |
  384. * | \ |
  385. * | \________|1 MB
  386. * |----------------|
  387. * VRAM 0 % 100 %
  388. * used used
  389. *
  390. * Note: It's a threshold, not a limit. The threshold must be crossed
  391. * for buffer relocations to stop, so any buffer of an arbitrary size
  392. * can be moved as long as the threshold isn't crossed before
  393. * the relocation takes place. We don't want to disable buffer
  394. * relocations completely.
  395. *
  396. * The idea is that buffers should be placed in VRAM at creation time
  397. * and TTM should only do a minimum number of relocations during
  398. * command submission. In practice, you need to submit at least
  399. * a dozen IBs to move all buffers to VRAM if they are in GTT.
  400. *
  401. * Also, things can get pretty crazy under memory pressure and actual
  402. * VRAM usage can change a lot, so playing safe even at 50% does
  403. * consistently increase performance.
  404. */
  405. u64 half_vram = real_vram_size >> 1;
  406. u64 half_free_vram = vram_usage >= half_vram ? 0 : half_vram - vram_usage;
  407. u64 bytes_moved_threshold = half_free_vram >> 1;
  408. return max(bytes_moved_threshold, 1024*1024ull);
  409. }
  410. int radeon_bo_list_validate(struct radeon_device *rdev,
  411. struct drm_exec *exec,
  412. struct list_head *head, int ring)
  413. {
  414. struct ttm_operation_ctx ctx = { true, false };
  415. struct radeon_bo_list *lobj;
  416. u64 bytes_moved = 0, initial_bytes_moved;
  417. u64 bytes_moved_threshold = radeon_bo_get_threshold_for_moves(rdev);
  418. int r;
  419. drm_exec_until_all_locked(exec) {
  420. list_for_each_entry(lobj, head, list) {
  421. r = drm_exec_prepare_obj(exec, &lobj->robj->tbo.base,
  422. 1);
  423. drm_exec_retry_on_contention(exec);
  424. if (unlikely(r && r != -EALREADY))
  425. return r;
  426. }
  427. }
  428. list_for_each_entry(lobj, head, list) {
  429. struct radeon_bo *bo = lobj->robj;
  430. if (!bo->tbo.pin_count) {
  431. u32 domain = lobj->preferred_domains;
  432. u32 allowed = lobj->allowed_domains;
  433. u32 current_domain =
  434. radeon_mem_type_to_domain(bo->tbo.resource->mem_type);
  435. /* Check if this buffer will be moved and don't move it
  436. * if we have moved too many buffers for this IB already.
  437. *
  438. * Note that this allows moving at least one buffer of
  439. * any size, because it doesn't take the current "bo"
  440. * into account. We don't want to disallow buffer moves
  441. * completely.
  442. */
  443. if ((allowed & current_domain) != 0 &&
  444. (domain & current_domain) == 0 && /* will be moved */
  445. bytes_moved > bytes_moved_threshold) {
  446. /* don't move it */
  447. domain = current_domain;
  448. }
  449. retry:
  450. radeon_ttm_placement_from_domain(bo, domain);
  451. if (ring == R600_RING_TYPE_UVD_INDEX)
  452. radeon_uvd_force_into_uvd_segment(bo, allowed);
  453. initial_bytes_moved = atomic64_read(&rdev->num_bytes_moved);
  454. r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
  455. bytes_moved += atomic64_read(&rdev->num_bytes_moved) -
  456. initial_bytes_moved;
  457. if (unlikely(r)) {
  458. if (r != -ERESTARTSYS &&
  459. domain != lobj->allowed_domains) {
  460. domain = lobj->allowed_domains;
  461. goto retry;
  462. }
  463. return r;
  464. }
  465. }
  466. lobj->gpu_offset = radeon_bo_gpu_offset(bo);
  467. lobj->tiling_flags = bo->tiling_flags;
  468. }
  469. return 0;
  470. }
  471. int radeon_bo_get_surface_reg(struct radeon_bo *bo)
  472. {
  473. struct radeon_device *rdev = bo->rdev;
  474. struct radeon_surface_reg *reg;
  475. struct radeon_bo *old_object;
  476. int steal;
  477. int i;
  478. dma_resv_assert_held(bo->tbo.base.resv);
  479. if (!bo->tiling_flags)
  480. return 0;
  481. if (bo->surface_reg >= 0) {
  482. i = bo->surface_reg;
  483. goto out;
  484. }
  485. steal = -1;
  486. for (i = 0; i < RADEON_GEM_MAX_SURFACES; i++) {
  487. reg = &rdev->surface_regs[i];
  488. if (!reg->bo)
  489. break;
  490. old_object = reg->bo;
  491. if (old_object->tbo.pin_count == 0)
  492. steal = i;
  493. }
  494. /* if we are all out */
  495. if (i == RADEON_GEM_MAX_SURFACES) {
  496. if (steal == -1)
  497. return -ENOMEM;
  498. /* find someone with a surface reg and nuke their BO */
  499. reg = &rdev->surface_regs[steal];
  500. old_object = reg->bo;
  501. /* blow away the mapping */
  502. DRM_DEBUG("stealing surface reg %d from %p\n", steal, old_object);
  503. ttm_bo_unmap_virtual(&old_object->tbo);
  504. old_object->surface_reg = -1;
  505. i = steal;
  506. }
  507. bo->surface_reg = i;
  508. reg->bo = bo;
  509. out:
  510. radeon_set_surface_reg(rdev, i, bo->tiling_flags, bo->pitch,
  511. bo->tbo.resource->start << PAGE_SHIFT,
  512. bo->tbo.base.size);
  513. return 0;
  514. }
  515. static void radeon_bo_clear_surface_reg(struct radeon_bo *bo)
  516. {
  517. struct radeon_device *rdev = bo->rdev;
  518. struct radeon_surface_reg *reg;
  519. if (bo->surface_reg == -1)
  520. return;
  521. reg = &rdev->surface_regs[bo->surface_reg];
  522. radeon_clear_surface_reg(rdev, bo->surface_reg);
  523. reg->bo = NULL;
  524. bo->surface_reg = -1;
  525. }
  526. int radeon_bo_set_tiling_flags(struct radeon_bo *bo,
  527. uint32_t tiling_flags, uint32_t pitch)
  528. {
  529. struct radeon_device *rdev = bo->rdev;
  530. int r;
  531. if (rdev->family >= CHIP_CEDAR) {
  532. unsigned bankw, bankh, mtaspect, tilesplit, stilesplit;
  533. bankw = (tiling_flags >> RADEON_TILING_EG_BANKW_SHIFT) & RADEON_TILING_EG_BANKW_MASK;
  534. bankh = (tiling_flags >> RADEON_TILING_EG_BANKH_SHIFT) & RADEON_TILING_EG_BANKH_MASK;
  535. mtaspect = (tiling_flags >> RADEON_TILING_EG_MACRO_TILE_ASPECT_SHIFT) & RADEON_TILING_EG_MACRO_TILE_ASPECT_MASK;
  536. tilesplit = (tiling_flags >> RADEON_TILING_EG_TILE_SPLIT_SHIFT) & RADEON_TILING_EG_TILE_SPLIT_MASK;
  537. stilesplit = (tiling_flags >> RADEON_TILING_EG_STENCIL_TILE_SPLIT_SHIFT) & RADEON_TILING_EG_STENCIL_TILE_SPLIT_MASK;
  538. switch (bankw) {
  539. case 0:
  540. case 1:
  541. case 2:
  542. case 4:
  543. case 8:
  544. break;
  545. default:
  546. return -EINVAL;
  547. }
  548. switch (bankh) {
  549. case 0:
  550. case 1:
  551. case 2:
  552. case 4:
  553. case 8:
  554. break;
  555. default:
  556. return -EINVAL;
  557. }
  558. switch (mtaspect) {
  559. case 0:
  560. case 1:
  561. case 2:
  562. case 4:
  563. case 8:
  564. break;
  565. default:
  566. return -EINVAL;
  567. }
  568. if (tilesplit > 6) {
  569. return -EINVAL;
  570. }
  571. if (stilesplit > 6) {
  572. return -EINVAL;
  573. }
  574. }
  575. r = radeon_bo_reserve(bo, false);
  576. if (unlikely(r != 0))
  577. return r;
  578. bo->tiling_flags = tiling_flags;
  579. bo->pitch = pitch;
  580. radeon_bo_unreserve(bo);
  581. return 0;
  582. }
  583. void radeon_bo_get_tiling_flags(struct radeon_bo *bo,
  584. uint32_t *tiling_flags,
  585. uint32_t *pitch)
  586. {
  587. dma_resv_assert_held(bo->tbo.base.resv);
  588. if (tiling_flags)
  589. *tiling_flags = bo->tiling_flags;
  590. if (pitch)
  591. *pitch = bo->pitch;
  592. }
  593. int radeon_bo_check_tiling(struct radeon_bo *bo, bool has_moved,
  594. bool force_drop)
  595. {
  596. if (!force_drop)
  597. dma_resv_assert_held(bo->tbo.base.resv);
  598. if (!(bo->tiling_flags & RADEON_TILING_SURFACE))
  599. return 0;
  600. if (force_drop) {
  601. radeon_bo_clear_surface_reg(bo);
  602. return 0;
  603. }
  604. if (bo->tbo.resource->mem_type != TTM_PL_VRAM) {
  605. if (!has_moved)
  606. return 0;
  607. if (bo->surface_reg >= 0)
  608. radeon_bo_clear_surface_reg(bo);
  609. return 0;
  610. }
  611. if ((bo->surface_reg >= 0) && !has_moved)
  612. return 0;
  613. return radeon_bo_get_surface_reg(bo);
  614. }
  615. void radeon_bo_move_notify(struct ttm_buffer_object *bo)
  616. {
  617. struct radeon_bo *rbo;
  618. if (!radeon_ttm_bo_is_radeon_bo(bo))
  619. return;
  620. rbo = container_of(bo, struct radeon_bo, tbo);
  621. radeon_bo_check_tiling(rbo, 0, 1);
  622. radeon_vm_bo_invalidate(rbo->rdev, rbo);
  623. }
  624. vm_fault_t radeon_bo_fault_reserve_notify(struct ttm_buffer_object *bo)
  625. {
  626. struct ttm_operation_ctx ctx = { false, false };
  627. struct radeon_device *rdev;
  628. struct radeon_bo *rbo;
  629. unsigned long offset, size, lpfn;
  630. int i, r;
  631. if (!radeon_ttm_bo_is_radeon_bo(bo))
  632. return 0;
  633. rbo = container_of(bo, struct radeon_bo, tbo);
  634. radeon_bo_check_tiling(rbo, 0, 0);
  635. rdev = rbo->rdev;
  636. if (bo->resource->mem_type != TTM_PL_VRAM)
  637. return 0;
  638. size = bo->resource->size;
  639. offset = bo->resource->start << PAGE_SHIFT;
  640. if ((offset + size) <= rdev->mc.visible_vram_size)
  641. return 0;
  642. /* Can't move a pinned BO to visible VRAM */
  643. if (rbo->tbo.pin_count > 0)
  644. return VM_FAULT_SIGBUS;
  645. /* hurrah the memory is not visible ! */
  646. radeon_ttm_placement_from_domain(rbo, RADEON_GEM_DOMAIN_VRAM);
  647. lpfn = rdev->mc.visible_vram_size >> PAGE_SHIFT;
  648. for (i = 0; i < rbo->placement.num_placement; i++) {
  649. /* Force into visible VRAM */
  650. if ((rbo->placements[i].mem_type == TTM_PL_VRAM) &&
  651. (!rbo->placements[i].lpfn || rbo->placements[i].lpfn > lpfn))
  652. rbo->placements[i].lpfn = lpfn;
  653. }
  654. r = ttm_bo_validate(bo, &rbo->placement, &ctx);
  655. if (unlikely(r == -ENOMEM)) {
  656. radeon_ttm_placement_from_domain(rbo, RADEON_GEM_DOMAIN_GTT);
  657. r = ttm_bo_validate(bo, &rbo->placement, &ctx);
  658. } else if (likely(!r)) {
  659. offset = bo->resource->start << PAGE_SHIFT;
  660. /* this should never happen */
  661. if ((offset + size) > rdev->mc.visible_vram_size)
  662. return VM_FAULT_SIGBUS;
  663. }
  664. if (unlikely(r == -EBUSY || r == -ERESTARTSYS))
  665. return VM_FAULT_NOPAGE;
  666. else if (unlikely(r))
  667. return VM_FAULT_SIGBUS;
  668. ttm_bo_move_to_lru_tail_unlocked(bo);
  669. return 0;
  670. }
  671. /**
  672. * radeon_bo_fence - add fence to buffer object
  673. *
  674. * @bo: buffer object in question
  675. * @fence: fence to add
  676. * @shared: true if fence should be added shared
  677. *
  678. */
  679. void radeon_bo_fence(struct radeon_bo *bo, struct radeon_fence *fence,
  680. bool shared)
  681. {
  682. struct dma_resv *resv = bo->tbo.base.resv;
  683. int r;
  684. r = dma_resv_reserve_fences(resv, 1);
  685. if (r) {
  686. /* As last resort on OOM we block for the fence */
  687. dma_fence_wait(&fence->base, false);
  688. return;
  689. }
  690. dma_resv_add_fence(resv, &fence->base, shared ?
  691. DMA_RESV_USAGE_READ : DMA_RESV_USAGE_WRITE);
  692. }