radeon_object.c 21 KB

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