amdgpu_object.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  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/amdgpu_drm.h>
  36. #include <drm/drm_cache.h>
  37. #include "amdgpu.h"
  38. #include "amdgpu_trace.h"
  39. static u64 amdgpu_get_vis_part_size(struct amdgpu_device *adev,
  40. struct ttm_mem_reg *mem)
  41. {
  42. if (mem->start << PAGE_SHIFT >= adev->mc.visible_vram_size)
  43. return 0;
  44. return ((mem->start << PAGE_SHIFT) + mem->size) >
  45. adev->mc.visible_vram_size ?
  46. adev->mc.visible_vram_size - (mem->start << PAGE_SHIFT) :
  47. mem->size;
  48. }
  49. static void amdgpu_update_memory_usage(struct amdgpu_device *adev,
  50. struct ttm_mem_reg *old_mem,
  51. struct ttm_mem_reg *new_mem)
  52. {
  53. u64 vis_size;
  54. if (!adev)
  55. return;
  56. if (new_mem) {
  57. switch (new_mem->mem_type) {
  58. case TTM_PL_TT:
  59. atomic64_add(new_mem->size, &adev->gtt_usage);
  60. break;
  61. case TTM_PL_VRAM:
  62. atomic64_add(new_mem->size, &adev->vram_usage);
  63. vis_size = amdgpu_get_vis_part_size(adev, new_mem);
  64. atomic64_add(vis_size, &adev->vram_vis_usage);
  65. break;
  66. }
  67. }
  68. if (old_mem) {
  69. switch (old_mem->mem_type) {
  70. case TTM_PL_TT:
  71. atomic64_sub(old_mem->size, &adev->gtt_usage);
  72. break;
  73. case TTM_PL_VRAM:
  74. atomic64_sub(old_mem->size, &adev->vram_usage);
  75. vis_size = amdgpu_get_vis_part_size(adev, old_mem);
  76. atomic64_sub(vis_size, &adev->vram_vis_usage);
  77. break;
  78. }
  79. }
  80. }
  81. static void amdgpu_ttm_bo_destroy(struct ttm_buffer_object *tbo)
  82. {
  83. struct amdgpu_device *adev = amdgpu_ttm_adev(tbo->bdev);
  84. struct amdgpu_bo *bo;
  85. bo = container_of(tbo, struct amdgpu_bo, tbo);
  86. amdgpu_update_memory_usage(adev, &bo->tbo.mem, NULL);
  87. drm_gem_object_release(&bo->gem_base);
  88. amdgpu_bo_unref(&bo->parent);
  89. if (!list_empty(&bo->shadow_list)) {
  90. mutex_lock(&adev->shadow_list_lock);
  91. list_del_init(&bo->shadow_list);
  92. mutex_unlock(&adev->shadow_list_lock);
  93. }
  94. kfree(bo->metadata);
  95. kfree(bo);
  96. }
  97. bool amdgpu_ttm_bo_is_amdgpu_bo(struct ttm_buffer_object *bo)
  98. {
  99. if (bo->destroy == &amdgpu_ttm_bo_destroy)
  100. return true;
  101. return false;
  102. }
  103. static void amdgpu_ttm_placement_init(struct amdgpu_device *adev,
  104. struct ttm_placement *placement,
  105. struct ttm_place *places,
  106. u32 domain, u64 flags)
  107. {
  108. u32 c = 0;
  109. if (domain & AMDGPU_GEM_DOMAIN_VRAM) {
  110. unsigned visible_pfn = adev->mc.visible_vram_size >> PAGE_SHIFT;
  111. unsigned lpfn = 0;
  112. /* This forces a reallocation if the flag wasn't set before */
  113. if (flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS)
  114. lpfn = adev->mc.real_vram_size >> PAGE_SHIFT;
  115. places[c].fpfn = 0;
  116. places[c].lpfn = lpfn;
  117. places[c].flags = TTM_PL_FLAG_WC | TTM_PL_FLAG_UNCACHED |
  118. TTM_PL_FLAG_VRAM;
  119. if (flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED)
  120. places[c].lpfn = visible_pfn;
  121. else
  122. places[c].flags |= TTM_PL_FLAG_TOPDOWN;
  123. c++;
  124. }
  125. if (domain & AMDGPU_GEM_DOMAIN_GTT) {
  126. places[c].fpfn = 0;
  127. places[c].lpfn = 0;
  128. places[c].flags = TTM_PL_FLAG_TT;
  129. if (flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)
  130. places[c].flags |= TTM_PL_FLAG_WC |
  131. TTM_PL_FLAG_UNCACHED;
  132. else
  133. places[c].flags |= TTM_PL_FLAG_CACHED;
  134. c++;
  135. }
  136. if (domain & AMDGPU_GEM_DOMAIN_CPU) {
  137. places[c].fpfn = 0;
  138. places[c].lpfn = 0;
  139. places[c].flags = TTM_PL_FLAG_SYSTEM;
  140. if (flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)
  141. places[c].flags |= TTM_PL_FLAG_WC |
  142. TTM_PL_FLAG_UNCACHED;
  143. else
  144. places[c].flags |= TTM_PL_FLAG_CACHED;
  145. c++;
  146. }
  147. if (domain & AMDGPU_GEM_DOMAIN_GDS) {
  148. places[c].fpfn = 0;
  149. places[c].lpfn = 0;
  150. places[c].flags = TTM_PL_FLAG_UNCACHED | AMDGPU_PL_FLAG_GDS;
  151. c++;
  152. }
  153. if (domain & AMDGPU_GEM_DOMAIN_GWS) {
  154. places[c].fpfn = 0;
  155. places[c].lpfn = 0;
  156. places[c].flags = TTM_PL_FLAG_UNCACHED | AMDGPU_PL_FLAG_GWS;
  157. c++;
  158. }
  159. if (domain & AMDGPU_GEM_DOMAIN_OA) {
  160. places[c].fpfn = 0;
  161. places[c].lpfn = 0;
  162. places[c].flags = TTM_PL_FLAG_UNCACHED | AMDGPU_PL_FLAG_OA;
  163. c++;
  164. }
  165. if (!c) {
  166. places[c].fpfn = 0;
  167. places[c].lpfn = 0;
  168. places[c].flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM;
  169. c++;
  170. }
  171. placement->num_placement = c;
  172. placement->placement = places;
  173. placement->num_busy_placement = c;
  174. placement->busy_placement = places;
  175. }
  176. void amdgpu_ttm_placement_from_domain(struct amdgpu_bo *abo, u32 domain)
  177. {
  178. struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
  179. amdgpu_ttm_placement_init(adev, &abo->placement, abo->placements,
  180. domain, abo->flags);
  181. }
  182. static void amdgpu_fill_placement_to_bo(struct amdgpu_bo *bo,
  183. struct ttm_placement *placement)
  184. {
  185. BUG_ON(placement->num_placement > (AMDGPU_GEM_DOMAIN_MAX + 1));
  186. memcpy(bo->placements, placement->placement,
  187. placement->num_placement * sizeof(struct ttm_place));
  188. bo->placement.num_placement = placement->num_placement;
  189. bo->placement.num_busy_placement = placement->num_busy_placement;
  190. bo->placement.placement = bo->placements;
  191. bo->placement.busy_placement = bo->placements;
  192. }
  193. /**
  194. * amdgpu_bo_create_kernel - create BO for kernel use
  195. *
  196. * @adev: amdgpu device object
  197. * @size: size for the new BO
  198. * @align: alignment for the new BO
  199. * @domain: where to place it
  200. * @bo_ptr: resulting BO
  201. * @gpu_addr: GPU addr of the pinned BO
  202. * @cpu_addr: optional CPU address mapping
  203. *
  204. * Allocates and pins a BO for kernel internal use.
  205. *
  206. * Returns 0 on success, negative error code otherwise.
  207. */
  208. int amdgpu_bo_create_kernel(struct amdgpu_device *adev,
  209. unsigned long size, int align,
  210. u32 domain, struct amdgpu_bo **bo_ptr,
  211. u64 *gpu_addr, void **cpu_addr)
  212. {
  213. int r;
  214. r = amdgpu_bo_create(adev, size, align, true, domain,
  215. AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
  216. AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS,
  217. NULL, NULL, bo_ptr);
  218. if (r) {
  219. dev_err(adev->dev, "(%d) failed to allocate kernel bo\n", r);
  220. return r;
  221. }
  222. r = amdgpu_bo_reserve(*bo_ptr, false);
  223. if (r) {
  224. dev_err(adev->dev, "(%d) failed to reserve kernel bo\n", r);
  225. goto error_free;
  226. }
  227. r = amdgpu_bo_pin(*bo_ptr, domain, gpu_addr);
  228. if (r) {
  229. dev_err(adev->dev, "(%d) kernel bo pin failed\n", r);
  230. goto error_unreserve;
  231. }
  232. if (cpu_addr) {
  233. r = amdgpu_bo_kmap(*bo_ptr, cpu_addr);
  234. if (r) {
  235. dev_err(adev->dev, "(%d) kernel bo map failed\n", r);
  236. goto error_unreserve;
  237. }
  238. }
  239. amdgpu_bo_unreserve(*bo_ptr);
  240. return 0;
  241. error_unreserve:
  242. amdgpu_bo_unreserve(*bo_ptr);
  243. error_free:
  244. amdgpu_bo_unref(bo_ptr);
  245. return r;
  246. }
  247. /**
  248. * amdgpu_bo_free_kernel - free BO for kernel use
  249. *
  250. * @bo: amdgpu BO to free
  251. *
  252. * unmaps and unpin a BO for kernel internal use.
  253. */
  254. void amdgpu_bo_free_kernel(struct amdgpu_bo **bo, u64 *gpu_addr,
  255. void **cpu_addr)
  256. {
  257. if (*bo == NULL)
  258. return;
  259. if (likely(amdgpu_bo_reserve(*bo, false) == 0)) {
  260. if (cpu_addr)
  261. amdgpu_bo_kunmap(*bo);
  262. amdgpu_bo_unpin(*bo);
  263. amdgpu_bo_unreserve(*bo);
  264. }
  265. amdgpu_bo_unref(bo);
  266. if (gpu_addr)
  267. *gpu_addr = 0;
  268. if (cpu_addr)
  269. *cpu_addr = NULL;
  270. }
  271. int amdgpu_bo_create_restricted(struct amdgpu_device *adev,
  272. unsigned long size, int byte_align,
  273. bool kernel, u32 domain, u64 flags,
  274. struct sg_table *sg,
  275. struct ttm_placement *placement,
  276. struct reservation_object *resv,
  277. struct amdgpu_bo **bo_ptr)
  278. {
  279. struct amdgpu_bo *bo;
  280. enum ttm_bo_type type;
  281. unsigned long page_align;
  282. size_t acc_size;
  283. int r;
  284. page_align = roundup(byte_align, PAGE_SIZE) >> PAGE_SHIFT;
  285. size = ALIGN(size, PAGE_SIZE);
  286. if (kernel) {
  287. type = ttm_bo_type_kernel;
  288. } else if (sg) {
  289. type = ttm_bo_type_sg;
  290. } else {
  291. type = ttm_bo_type_device;
  292. }
  293. *bo_ptr = NULL;
  294. acc_size = ttm_bo_dma_acc_size(&adev->mman.bdev, size,
  295. sizeof(struct amdgpu_bo));
  296. bo = kzalloc(sizeof(struct amdgpu_bo), GFP_KERNEL);
  297. if (bo == NULL)
  298. return -ENOMEM;
  299. r = drm_gem_object_init(adev->ddev, &bo->gem_base, size);
  300. if (unlikely(r)) {
  301. kfree(bo);
  302. return r;
  303. }
  304. INIT_LIST_HEAD(&bo->shadow_list);
  305. INIT_LIST_HEAD(&bo->va);
  306. bo->prefered_domains = domain & (AMDGPU_GEM_DOMAIN_VRAM |
  307. AMDGPU_GEM_DOMAIN_GTT |
  308. AMDGPU_GEM_DOMAIN_CPU |
  309. AMDGPU_GEM_DOMAIN_GDS |
  310. AMDGPU_GEM_DOMAIN_GWS |
  311. AMDGPU_GEM_DOMAIN_OA);
  312. bo->allowed_domains = bo->prefered_domains;
  313. if (!kernel && bo->allowed_domains == AMDGPU_GEM_DOMAIN_VRAM)
  314. bo->allowed_domains |= AMDGPU_GEM_DOMAIN_GTT;
  315. bo->flags = flags;
  316. #ifdef CONFIG_X86_32
  317. /* XXX: Write-combined CPU mappings of GTT seem broken on 32-bit
  318. * See https://bugs.freedesktop.org/show_bug.cgi?id=84627
  319. */
  320. bo->flags &= ~AMDGPU_GEM_CREATE_CPU_GTT_USWC;
  321. #elif defined(CONFIG_X86) && !defined(CONFIG_X86_PAT)
  322. /* Don't try to enable write-combining when it can't work, or things
  323. * may be slow
  324. * See https://bugs.freedesktop.org/show_bug.cgi?id=88758
  325. */
  326. #warning Please enable CONFIG_MTRR and CONFIG_X86_PAT for better performance \
  327. thanks to write-combining
  328. if (bo->flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)
  329. DRM_INFO_ONCE("Please enable CONFIG_MTRR and CONFIG_X86_PAT for "
  330. "better performance thanks to write-combining\n");
  331. bo->flags &= ~AMDGPU_GEM_CREATE_CPU_GTT_USWC;
  332. #else
  333. /* For architectures that don't support WC memory,
  334. * mask out the WC flag from the BO
  335. */
  336. if (!drm_arch_can_wc_memory())
  337. bo->flags &= ~AMDGPU_GEM_CREATE_CPU_GTT_USWC;
  338. #endif
  339. amdgpu_fill_placement_to_bo(bo, placement);
  340. /* Kernel allocation are uninterruptible */
  341. if (!resv) {
  342. bool locked;
  343. reservation_object_init(&bo->tbo.ttm_resv);
  344. locked = ww_mutex_trylock(&bo->tbo.ttm_resv.lock);
  345. WARN_ON(!locked);
  346. }
  347. r = ttm_bo_init(&adev->mman.bdev, &bo->tbo, size, type,
  348. &bo->placement, page_align, !kernel, NULL,
  349. acc_size, sg, resv ? resv : &bo->tbo.ttm_resv,
  350. &amdgpu_ttm_bo_destroy);
  351. if (unlikely(r != 0))
  352. return r;
  353. bo->tbo.priority = ilog2(bo->tbo.num_pages);
  354. if (kernel)
  355. bo->tbo.priority *= 2;
  356. bo->tbo.priority = min(bo->tbo.priority, (unsigned)(TTM_MAX_BO_PRIORITY - 1));
  357. if (flags & AMDGPU_GEM_CREATE_VRAM_CLEARED &&
  358. bo->tbo.mem.placement & TTM_PL_FLAG_VRAM) {
  359. struct dma_fence *fence;
  360. r = amdgpu_fill_buffer(bo, 0, bo->tbo.resv, &fence);
  361. if (unlikely(r))
  362. goto fail_unreserve;
  363. amdgpu_bo_fence(bo, fence, false);
  364. dma_fence_put(bo->tbo.moving);
  365. bo->tbo.moving = dma_fence_get(fence);
  366. dma_fence_put(fence);
  367. }
  368. if (!resv)
  369. ww_mutex_unlock(&bo->tbo.resv->lock);
  370. *bo_ptr = bo;
  371. trace_amdgpu_bo_create(bo);
  372. return 0;
  373. fail_unreserve:
  374. if (!resv)
  375. ww_mutex_unlock(&bo->tbo.resv->lock);
  376. amdgpu_bo_unref(&bo);
  377. return r;
  378. }
  379. static int amdgpu_bo_create_shadow(struct amdgpu_device *adev,
  380. unsigned long size, int byte_align,
  381. struct amdgpu_bo *bo)
  382. {
  383. struct ttm_placement placement = {0};
  384. struct ttm_place placements[AMDGPU_GEM_DOMAIN_MAX + 1];
  385. int r;
  386. if (bo->shadow)
  387. return 0;
  388. bo->flags |= AMDGPU_GEM_CREATE_SHADOW;
  389. memset(&placements, 0,
  390. (AMDGPU_GEM_DOMAIN_MAX + 1) * sizeof(struct ttm_place));
  391. amdgpu_ttm_placement_init(adev, &placement,
  392. placements, AMDGPU_GEM_DOMAIN_GTT,
  393. AMDGPU_GEM_CREATE_CPU_GTT_USWC);
  394. r = amdgpu_bo_create_restricted(adev, size, byte_align, true,
  395. AMDGPU_GEM_DOMAIN_GTT,
  396. AMDGPU_GEM_CREATE_CPU_GTT_USWC,
  397. NULL, &placement,
  398. bo->tbo.resv,
  399. &bo->shadow);
  400. if (!r) {
  401. bo->shadow->parent = amdgpu_bo_ref(bo);
  402. mutex_lock(&adev->shadow_list_lock);
  403. list_add_tail(&bo->shadow_list, &adev->shadow_list);
  404. mutex_unlock(&adev->shadow_list_lock);
  405. }
  406. return r;
  407. }
  408. int amdgpu_bo_create(struct amdgpu_device *adev,
  409. unsigned long size, int byte_align,
  410. bool kernel, u32 domain, u64 flags,
  411. struct sg_table *sg,
  412. struct reservation_object *resv,
  413. struct amdgpu_bo **bo_ptr)
  414. {
  415. struct ttm_placement placement = {0};
  416. struct ttm_place placements[AMDGPU_GEM_DOMAIN_MAX + 1];
  417. int r;
  418. memset(&placements, 0,
  419. (AMDGPU_GEM_DOMAIN_MAX + 1) * sizeof(struct ttm_place));
  420. amdgpu_ttm_placement_init(adev, &placement,
  421. placements, domain, flags);
  422. r = amdgpu_bo_create_restricted(adev, size, byte_align, kernel,
  423. domain, flags, sg, &placement,
  424. resv, bo_ptr);
  425. if (r)
  426. return r;
  427. if (amdgpu_need_backup(adev) && (flags & AMDGPU_GEM_CREATE_SHADOW)) {
  428. if (!resv) {
  429. r = ww_mutex_lock(&(*bo_ptr)->tbo.resv->lock, NULL);
  430. WARN_ON(r != 0);
  431. }
  432. r = amdgpu_bo_create_shadow(adev, size, byte_align, (*bo_ptr));
  433. if (!resv)
  434. ww_mutex_unlock(&(*bo_ptr)->tbo.resv->lock);
  435. if (r)
  436. amdgpu_bo_unref(bo_ptr);
  437. }
  438. return r;
  439. }
  440. int amdgpu_bo_backup_to_shadow(struct amdgpu_device *adev,
  441. struct amdgpu_ring *ring,
  442. struct amdgpu_bo *bo,
  443. struct reservation_object *resv,
  444. struct dma_fence **fence,
  445. bool direct)
  446. {
  447. struct amdgpu_bo *shadow = bo->shadow;
  448. uint64_t bo_addr, shadow_addr;
  449. int r;
  450. if (!shadow)
  451. return -EINVAL;
  452. bo_addr = amdgpu_bo_gpu_offset(bo);
  453. shadow_addr = amdgpu_bo_gpu_offset(bo->shadow);
  454. r = reservation_object_reserve_shared(bo->tbo.resv);
  455. if (r)
  456. goto err;
  457. r = amdgpu_copy_buffer(ring, bo_addr, shadow_addr,
  458. amdgpu_bo_size(bo), resv, fence,
  459. direct);
  460. if (!r)
  461. amdgpu_bo_fence(bo, *fence, true);
  462. err:
  463. return r;
  464. }
  465. int amdgpu_bo_restore_from_shadow(struct amdgpu_device *adev,
  466. struct amdgpu_ring *ring,
  467. struct amdgpu_bo *bo,
  468. struct reservation_object *resv,
  469. struct dma_fence **fence,
  470. bool direct)
  471. {
  472. struct amdgpu_bo *shadow = bo->shadow;
  473. uint64_t bo_addr, shadow_addr;
  474. int r;
  475. if (!shadow)
  476. return -EINVAL;
  477. bo_addr = amdgpu_bo_gpu_offset(bo);
  478. shadow_addr = amdgpu_bo_gpu_offset(bo->shadow);
  479. r = reservation_object_reserve_shared(bo->tbo.resv);
  480. if (r)
  481. goto err;
  482. r = amdgpu_copy_buffer(ring, shadow_addr, bo_addr,
  483. amdgpu_bo_size(bo), resv, fence,
  484. direct);
  485. if (!r)
  486. amdgpu_bo_fence(bo, *fence, true);
  487. err:
  488. return r;
  489. }
  490. int amdgpu_bo_kmap(struct amdgpu_bo *bo, void **ptr)
  491. {
  492. bool is_iomem;
  493. long r;
  494. if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)
  495. return -EPERM;
  496. if (bo->kptr) {
  497. if (ptr) {
  498. *ptr = bo->kptr;
  499. }
  500. return 0;
  501. }
  502. r = reservation_object_wait_timeout_rcu(bo->tbo.resv, false, false,
  503. MAX_SCHEDULE_TIMEOUT);
  504. if (r < 0)
  505. return r;
  506. r = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages, &bo->kmap);
  507. if (r)
  508. return r;
  509. bo->kptr = ttm_kmap_obj_virtual(&bo->kmap, &is_iomem);
  510. if (ptr)
  511. *ptr = bo->kptr;
  512. return 0;
  513. }
  514. void amdgpu_bo_kunmap(struct amdgpu_bo *bo)
  515. {
  516. if (bo->kptr == NULL)
  517. return;
  518. bo->kptr = NULL;
  519. ttm_bo_kunmap(&bo->kmap);
  520. }
  521. struct amdgpu_bo *amdgpu_bo_ref(struct amdgpu_bo *bo)
  522. {
  523. if (bo == NULL)
  524. return NULL;
  525. ttm_bo_reference(&bo->tbo);
  526. return bo;
  527. }
  528. void amdgpu_bo_unref(struct amdgpu_bo **bo)
  529. {
  530. struct ttm_buffer_object *tbo;
  531. if ((*bo) == NULL)
  532. return;
  533. tbo = &((*bo)->tbo);
  534. ttm_bo_unref(&tbo);
  535. if (tbo == NULL)
  536. *bo = NULL;
  537. }
  538. int amdgpu_bo_pin_restricted(struct amdgpu_bo *bo, u32 domain,
  539. u64 min_offset, u64 max_offset,
  540. u64 *gpu_addr)
  541. {
  542. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
  543. int r, i;
  544. unsigned fpfn, lpfn;
  545. if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm))
  546. return -EPERM;
  547. if (WARN_ON_ONCE(min_offset > max_offset))
  548. return -EINVAL;
  549. if (bo->pin_count) {
  550. uint32_t mem_type = bo->tbo.mem.mem_type;
  551. if (domain != amdgpu_mem_type_to_domain(mem_type))
  552. return -EINVAL;
  553. bo->pin_count++;
  554. if (gpu_addr)
  555. *gpu_addr = amdgpu_bo_gpu_offset(bo);
  556. if (max_offset != 0) {
  557. u64 domain_start = bo->tbo.bdev->man[mem_type].gpu_offset;
  558. WARN_ON_ONCE(max_offset <
  559. (amdgpu_bo_gpu_offset(bo) - domain_start));
  560. }
  561. return 0;
  562. }
  563. bo->flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
  564. amdgpu_ttm_placement_from_domain(bo, domain);
  565. for (i = 0; i < bo->placement.num_placement; i++) {
  566. /* force to pin into visible video ram */
  567. if ((bo->placements[i].flags & TTM_PL_FLAG_VRAM) &&
  568. !(bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS) &&
  569. (!max_offset || max_offset >
  570. adev->mc.visible_vram_size)) {
  571. if (WARN_ON_ONCE(min_offset >
  572. adev->mc.visible_vram_size))
  573. return -EINVAL;
  574. fpfn = min_offset >> PAGE_SHIFT;
  575. lpfn = adev->mc.visible_vram_size >> PAGE_SHIFT;
  576. } else {
  577. fpfn = min_offset >> PAGE_SHIFT;
  578. lpfn = max_offset >> PAGE_SHIFT;
  579. }
  580. if (fpfn > bo->placements[i].fpfn)
  581. bo->placements[i].fpfn = fpfn;
  582. if (!bo->placements[i].lpfn ||
  583. (lpfn && lpfn < bo->placements[i].lpfn))
  584. bo->placements[i].lpfn = lpfn;
  585. bo->placements[i].flags |= TTM_PL_FLAG_NO_EVICT;
  586. }
  587. r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
  588. if (unlikely(r)) {
  589. dev_err(adev->dev, "%p pin failed\n", bo);
  590. goto error;
  591. }
  592. r = amdgpu_ttm_bind(&bo->tbo, &bo->tbo.mem);
  593. if (unlikely(r)) {
  594. dev_err(adev->dev, "%p bind failed\n", bo);
  595. goto error;
  596. }
  597. bo->pin_count = 1;
  598. if (gpu_addr != NULL)
  599. *gpu_addr = amdgpu_bo_gpu_offset(bo);
  600. if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
  601. adev->vram_pin_size += amdgpu_bo_size(bo);
  602. if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)
  603. adev->invisible_pin_size += amdgpu_bo_size(bo);
  604. } else if (domain == AMDGPU_GEM_DOMAIN_GTT) {
  605. adev->gart_pin_size += amdgpu_bo_size(bo);
  606. }
  607. error:
  608. return r;
  609. }
  610. int amdgpu_bo_pin(struct amdgpu_bo *bo, u32 domain, u64 *gpu_addr)
  611. {
  612. return amdgpu_bo_pin_restricted(bo, domain, 0, 0, gpu_addr);
  613. }
  614. int amdgpu_bo_unpin(struct amdgpu_bo *bo)
  615. {
  616. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
  617. int r, i;
  618. if (!bo->pin_count) {
  619. dev_warn(adev->dev, "%p unpin not necessary\n", bo);
  620. return 0;
  621. }
  622. bo->pin_count--;
  623. if (bo->pin_count)
  624. return 0;
  625. for (i = 0; i < bo->placement.num_placement; i++) {
  626. bo->placements[i].lpfn = 0;
  627. bo->placements[i].flags &= ~TTM_PL_FLAG_NO_EVICT;
  628. }
  629. r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
  630. if (unlikely(r)) {
  631. dev_err(adev->dev, "%p validate failed for unpin\n", bo);
  632. goto error;
  633. }
  634. if (bo->tbo.mem.mem_type == TTM_PL_VRAM) {
  635. adev->vram_pin_size -= amdgpu_bo_size(bo);
  636. if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)
  637. adev->invisible_pin_size -= amdgpu_bo_size(bo);
  638. } else if (bo->tbo.mem.mem_type == TTM_PL_TT) {
  639. adev->gart_pin_size -= amdgpu_bo_size(bo);
  640. }
  641. error:
  642. return r;
  643. }
  644. int amdgpu_bo_evict_vram(struct amdgpu_device *adev)
  645. {
  646. /* late 2.6.33 fix IGP hibernate - we need pm ops to do this correct */
  647. if (0 && (adev->flags & AMD_IS_APU)) {
  648. /* Useless to evict on IGP chips */
  649. return 0;
  650. }
  651. return ttm_bo_evict_mm(&adev->mman.bdev, TTM_PL_VRAM);
  652. }
  653. static const char *amdgpu_vram_names[] = {
  654. "UNKNOWN",
  655. "GDDR1",
  656. "DDR2",
  657. "GDDR3",
  658. "GDDR4",
  659. "GDDR5",
  660. "HBM",
  661. "DDR3"
  662. };
  663. int amdgpu_bo_init(struct amdgpu_device *adev)
  664. {
  665. /* reserve PAT memory space to WC for VRAM */
  666. arch_io_reserve_memtype_wc(adev->mc.aper_base,
  667. adev->mc.aper_size);
  668. /* Add an MTRR for the VRAM */
  669. adev->mc.vram_mtrr = arch_phys_wc_add(adev->mc.aper_base,
  670. adev->mc.aper_size);
  671. DRM_INFO("Detected VRAM RAM=%lluM, BAR=%lluM\n",
  672. adev->mc.mc_vram_size >> 20,
  673. (unsigned long long)adev->mc.aper_size >> 20);
  674. DRM_INFO("RAM width %dbits %s\n",
  675. adev->mc.vram_width, amdgpu_vram_names[adev->mc.vram_type]);
  676. return amdgpu_ttm_init(adev);
  677. }
  678. void amdgpu_bo_fini(struct amdgpu_device *adev)
  679. {
  680. amdgpu_ttm_fini(adev);
  681. arch_phys_wc_del(adev->mc.vram_mtrr);
  682. arch_io_free_memtype_wc(adev->mc.aper_base, adev->mc.aper_size);
  683. }
  684. int amdgpu_bo_fbdev_mmap(struct amdgpu_bo *bo,
  685. struct vm_area_struct *vma)
  686. {
  687. return ttm_fbdev_mmap(vma, &bo->tbo);
  688. }
  689. int amdgpu_bo_set_tiling_flags(struct amdgpu_bo *bo, u64 tiling_flags)
  690. {
  691. if (AMDGPU_TILING_GET(tiling_flags, TILE_SPLIT) > 6)
  692. return -EINVAL;
  693. bo->tiling_flags = tiling_flags;
  694. return 0;
  695. }
  696. void amdgpu_bo_get_tiling_flags(struct amdgpu_bo *bo, u64 *tiling_flags)
  697. {
  698. lockdep_assert_held(&bo->tbo.resv->lock.base);
  699. if (tiling_flags)
  700. *tiling_flags = bo->tiling_flags;
  701. }
  702. int amdgpu_bo_set_metadata (struct amdgpu_bo *bo, void *metadata,
  703. uint32_t metadata_size, uint64_t flags)
  704. {
  705. void *buffer;
  706. if (!metadata_size) {
  707. if (bo->metadata_size) {
  708. kfree(bo->metadata);
  709. bo->metadata = NULL;
  710. bo->metadata_size = 0;
  711. }
  712. return 0;
  713. }
  714. if (metadata == NULL)
  715. return -EINVAL;
  716. buffer = kmemdup(metadata, metadata_size, GFP_KERNEL);
  717. if (buffer == NULL)
  718. return -ENOMEM;
  719. kfree(bo->metadata);
  720. bo->metadata_flags = flags;
  721. bo->metadata = buffer;
  722. bo->metadata_size = metadata_size;
  723. return 0;
  724. }
  725. int amdgpu_bo_get_metadata(struct amdgpu_bo *bo, void *buffer,
  726. size_t buffer_size, uint32_t *metadata_size,
  727. uint64_t *flags)
  728. {
  729. if (!buffer && !metadata_size)
  730. return -EINVAL;
  731. if (buffer) {
  732. if (buffer_size < bo->metadata_size)
  733. return -EINVAL;
  734. if (bo->metadata_size)
  735. memcpy(buffer, bo->metadata, bo->metadata_size);
  736. }
  737. if (metadata_size)
  738. *metadata_size = bo->metadata_size;
  739. if (flags)
  740. *flags = bo->metadata_flags;
  741. return 0;
  742. }
  743. void amdgpu_bo_move_notify(struct ttm_buffer_object *bo,
  744. bool evict,
  745. struct ttm_mem_reg *new_mem)
  746. {
  747. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
  748. struct amdgpu_bo *abo;
  749. struct ttm_mem_reg *old_mem = &bo->mem;
  750. if (!amdgpu_ttm_bo_is_amdgpu_bo(bo))
  751. return;
  752. abo = container_of(bo, struct amdgpu_bo, tbo);
  753. amdgpu_vm_bo_invalidate(adev, abo);
  754. /* remember the eviction */
  755. if (evict)
  756. atomic64_inc(&adev->num_evictions);
  757. /* update statistics */
  758. if (!new_mem)
  759. return;
  760. /* move_notify is called before move happens */
  761. amdgpu_update_memory_usage(adev, &bo->mem, new_mem);
  762. trace_amdgpu_ttm_bo_move(abo, new_mem->mem_type, old_mem->mem_type);
  763. }
  764. int amdgpu_bo_fault_reserve_notify(struct ttm_buffer_object *bo)
  765. {
  766. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
  767. struct amdgpu_bo *abo;
  768. unsigned long offset, size, lpfn;
  769. int i, r;
  770. if (!amdgpu_ttm_bo_is_amdgpu_bo(bo))
  771. return 0;
  772. abo = container_of(bo, struct amdgpu_bo, tbo);
  773. if (bo->mem.mem_type != TTM_PL_VRAM)
  774. return 0;
  775. size = bo->mem.num_pages << PAGE_SHIFT;
  776. offset = bo->mem.start << PAGE_SHIFT;
  777. /* TODO: figure out how to map scattered VRAM to the CPU */
  778. if ((offset + size) <= adev->mc.visible_vram_size &&
  779. (abo->flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS))
  780. return 0;
  781. /* Can't move a pinned BO to visible VRAM */
  782. if (abo->pin_count > 0)
  783. return -EINVAL;
  784. /* hurrah the memory is not visible ! */
  785. abo->flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
  786. amdgpu_ttm_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_VRAM);
  787. lpfn = adev->mc.visible_vram_size >> PAGE_SHIFT;
  788. for (i = 0; i < abo->placement.num_placement; i++) {
  789. /* Force into visible VRAM */
  790. if ((abo->placements[i].flags & TTM_PL_FLAG_VRAM) &&
  791. (!abo->placements[i].lpfn ||
  792. abo->placements[i].lpfn > lpfn))
  793. abo->placements[i].lpfn = lpfn;
  794. }
  795. r = ttm_bo_validate(bo, &abo->placement, false, false);
  796. if (unlikely(r == -ENOMEM)) {
  797. amdgpu_ttm_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_GTT);
  798. return ttm_bo_validate(bo, &abo->placement, false, false);
  799. } else if (unlikely(r != 0)) {
  800. return r;
  801. }
  802. offset = bo->mem.start << PAGE_SHIFT;
  803. /* this should never happen */
  804. if ((offset + size) > adev->mc.visible_vram_size)
  805. return -EINVAL;
  806. return 0;
  807. }
  808. /**
  809. * amdgpu_bo_fence - add fence to buffer object
  810. *
  811. * @bo: buffer object in question
  812. * @fence: fence to add
  813. * @shared: true if fence should be added shared
  814. *
  815. */
  816. void amdgpu_bo_fence(struct amdgpu_bo *bo, struct dma_fence *fence,
  817. bool shared)
  818. {
  819. struct reservation_object *resv = bo->tbo.resv;
  820. if (shared)
  821. reservation_object_add_shared_fence(resv, fence);
  822. else
  823. reservation_object_add_excl_fence(resv, fence);
  824. }
  825. /**
  826. * amdgpu_bo_gpu_offset - return GPU offset of bo
  827. * @bo: amdgpu object for which we query the offset
  828. *
  829. * Returns current GPU offset of the object.
  830. *
  831. * Note: object should either be pinned or reserved when calling this
  832. * function, it might be useful to add check for this for debugging.
  833. */
  834. u64 amdgpu_bo_gpu_offset(struct amdgpu_bo *bo)
  835. {
  836. WARN_ON_ONCE(bo->tbo.mem.mem_type == TTM_PL_SYSTEM);
  837. WARN_ON_ONCE(bo->tbo.mem.mem_type == TTM_PL_TT &&
  838. !amdgpu_ttm_is_bound(bo->tbo.ttm));
  839. WARN_ON_ONCE(!ww_mutex_is_locked(&bo->tbo.resv->lock) &&
  840. !bo->pin_count);
  841. WARN_ON_ONCE(bo->tbo.mem.start == AMDGPU_BO_INVALID_OFFSET);
  842. WARN_ON_ONCE(bo->tbo.mem.mem_type == TTM_PL_VRAM &&
  843. !(bo->flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS));
  844. return bo->tbo.offset;
  845. }