amdgpu_object.c 26 KB

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