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