amdgpu_object.c 25 KB

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