radeon_object.c 22 KB

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