radeon_object.c 21 KB

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