radeon_object.c 19 KB

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