amdgpu_object.c 25 KB

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