radeon_object.c 22 KB

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