amdgpu_object.c 24 KB

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