amdgpu_object.c 25 KB

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