radeon_object.c 23 KB

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