radeon_object.c 23 KB

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