amdgpu_object.c 25 KB

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