amdgpu_object.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  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. int amdgpu_ttm_init(struct amdgpu_device *adev);
  40. void amdgpu_ttm_fini(struct amdgpu_device *adev);
  41. static u64 amdgpu_get_vis_part_size(struct amdgpu_device *adev,
  42. struct ttm_mem_reg *mem)
  43. {
  44. if (mem->start << PAGE_SHIFT >= adev->mc.visible_vram_size)
  45. return 0;
  46. return ((mem->start << PAGE_SHIFT) + mem->size) >
  47. adev->mc.visible_vram_size ?
  48. adev->mc.visible_vram_size - (mem->start << PAGE_SHIFT) :
  49. mem->size;
  50. }
  51. static void amdgpu_update_memory_usage(struct amdgpu_device *adev,
  52. struct ttm_mem_reg *old_mem,
  53. struct ttm_mem_reg *new_mem)
  54. {
  55. u64 vis_size;
  56. if (!adev)
  57. return;
  58. if (new_mem) {
  59. switch (new_mem->mem_type) {
  60. case TTM_PL_TT:
  61. atomic64_add(new_mem->size, &adev->gtt_usage);
  62. break;
  63. case TTM_PL_VRAM:
  64. atomic64_add(new_mem->size, &adev->vram_usage);
  65. vis_size = amdgpu_get_vis_part_size(adev, new_mem);
  66. atomic64_add(vis_size, &adev->vram_vis_usage);
  67. break;
  68. }
  69. }
  70. if (old_mem) {
  71. switch (old_mem->mem_type) {
  72. case TTM_PL_TT:
  73. atomic64_sub(old_mem->size, &adev->gtt_usage);
  74. break;
  75. case TTM_PL_VRAM:
  76. atomic64_sub(old_mem->size, &adev->vram_usage);
  77. vis_size = amdgpu_get_vis_part_size(adev, old_mem);
  78. atomic64_sub(vis_size, &adev->vram_vis_usage);
  79. break;
  80. }
  81. }
  82. }
  83. static void amdgpu_ttm_bo_destroy(struct ttm_buffer_object *tbo)
  84. {
  85. struct amdgpu_bo *bo;
  86. bo = container_of(tbo, struct amdgpu_bo, tbo);
  87. amdgpu_update_memory_usage(bo->adev, &bo->tbo.mem, NULL);
  88. drm_gem_object_release(&bo->gem_base);
  89. amdgpu_bo_unref(&bo->parent);
  90. if (!list_empty(&bo->shadow_list)) {
  91. mutex_lock(&bo->adev->shadow_list_lock);
  92. list_del_init(&bo->shadow_list);
  93. mutex_unlock(&bo->adev->shadow_list_lock);
  94. }
  95. kfree(bo->metadata);
  96. kfree(bo);
  97. }
  98. bool amdgpu_ttm_bo_is_amdgpu_bo(struct ttm_buffer_object *bo)
  99. {
  100. if (bo->destroy == &amdgpu_ttm_bo_destroy)
  101. return true;
  102. return false;
  103. }
  104. static void amdgpu_ttm_placement_init(struct amdgpu_device *adev,
  105. struct ttm_placement *placement,
  106. struct ttm_place *places,
  107. u32 domain, u64 flags)
  108. {
  109. u32 c = 0, i;
  110. if (domain & AMDGPU_GEM_DOMAIN_VRAM) {
  111. unsigned visible_pfn = adev->mc.visible_vram_size >> PAGE_SHIFT;
  112. if (flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS &&
  113. adev->mc.visible_vram_size < adev->mc.real_vram_size) {
  114. places[c].fpfn = visible_pfn;
  115. if (flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED)
  116. places[c].lpfn = visible_pfn;
  117. else
  118. places[c].lpfn = 0;
  119. places[c].flags = TTM_PL_FLAG_WC |
  120. TTM_PL_FLAG_UNCACHED | TTM_PL_FLAG_VRAM |
  121. TTM_PL_FLAG_TOPDOWN;
  122. c++;
  123. }
  124. places[c].fpfn = 0;
  125. places[c].lpfn = 0;
  126. places[c].flags = TTM_PL_FLAG_WC | TTM_PL_FLAG_UNCACHED |
  127. TTM_PL_FLAG_VRAM;
  128. if (flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED)
  129. places[c].lpfn = visible_pfn;
  130. else
  131. places[c].flags |= TTM_PL_FLAG_TOPDOWN;
  132. c++;
  133. }
  134. if (domain & AMDGPU_GEM_DOMAIN_GTT) {
  135. places[c].fpfn = 0;
  136. places[c].lpfn = 0;
  137. places[c].flags = TTM_PL_FLAG_TT;
  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_CPU) {
  146. places[c].fpfn = 0;
  147. places[c].lpfn = 0;
  148. places[c].flags = TTM_PL_FLAG_SYSTEM;
  149. if (flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)
  150. places[c].flags |= TTM_PL_FLAG_WC |
  151. TTM_PL_FLAG_UNCACHED;
  152. else
  153. places[c].flags |= TTM_PL_FLAG_CACHED;
  154. c++;
  155. }
  156. if (domain & AMDGPU_GEM_DOMAIN_GDS) {
  157. places[c].fpfn = 0;
  158. places[c].lpfn = 0;
  159. places[c].flags = TTM_PL_FLAG_UNCACHED | AMDGPU_PL_FLAG_GDS;
  160. c++;
  161. }
  162. if (domain & AMDGPU_GEM_DOMAIN_GWS) {
  163. places[c].fpfn = 0;
  164. places[c].lpfn = 0;
  165. places[c].flags = TTM_PL_FLAG_UNCACHED | AMDGPU_PL_FLAG_GWS;
  166. c++;
  167. }
  168. if (domain & AMDGPU_GEM_DOMAIN_OA) {
  169. places[c].fpfn = 0;
  170. places[c].lpfn = 0;
  171. places[c].flags = TTM_PL_FLAG_UNCACHED | AMDGPU_PL_FLAG_OA;
  172. c++;
  173. }
  174. if (!c) {
  175. places[c].fpfn = 0;
  176. places[c].lpfn = 0;
  177. places[c].flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM;
  178. c++;
  179. }
  180. placement->num_placement = c;
  181. placement->placement = places;
  182. placement->num_busy_placement = c;
  183. placement->busy_placement = places;
  184. }
  185. void amdgpu_ttm_placement_from_domain(struct amdgpu_bo *rbo, u32 domain)
  186. {
  187. amdgpu_ttm_placement_init(rbo->adev, &rbo->placement,
  188. rbo->placements, domain, rbo->flags);
  189. }
  190. static void amdgpu_fill_placement_to_bo(struct amdgpu_bo *bo,
  191. struct ttm_placement *placement)
  192. {
  193. BUG_ON(placement->num_placement > (AMDGPU_GEM_DOMAIN_MAX + 1));
  194. memcpy(bo->placements, placement->placement,
  195. placement->num_placement * sizeof(struct ttm_place));
  196. bo->placement.num_placement = placement->num_placement;
  197. bo->placement.num_busy_placement = placement->num_busy_placement;
  198. bo->placement.placement = bo->placements;
  199. bo->placement.busy_placement = bo->placements;
  200. }
  201. /**
  202. * amdgpu_bo_create_kernel - create BO for kernel use
  203. *
  204. * @adev: amdgpu device object
  205. * @size: size for the new BO
  206. * @align: alignment for the new BO
  207. * @domain: where to place it
  208. * @bo_ptr: resulting BO
  209. * @gpu_addr: GPU addr of the pinned BO
  210. * @cpu_addr: optional CPU address mapping
  211. *
  212. * Allocates and pins a BO for kernel internal use.
  213. *
  214. * Returns 0 on success, negative error code otherwise.
  215. */
  216. int amdgpu_bo_create_kernel(struct amdgpu_device *adev,
  217. unsigned long size, int align,
  218. u32 domain, struct amdgpu_bo **bo_ptr,
  219. u64 *gpu_addr, void **cpu_addr)
  220. {
  221. int r;
  222. r = amdgpu_bo_create(adev, size, align, true, domain,
  223. AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED,
  224. NULL, NULL, bo_ptr);
  225. if (r) {
  226. dev_err(adev->dev, "(%d) failed to allocate kernel bo\n", r);
  227. return r;
  228. }
  229. r = amdgpu_bo_reserve(*bo_ptr, false);
  230. if (r) {
  231. dev_err(adev->dev, "(%d) failed to reserve kernel bo\n", r);
  232. goto error_free;
  233. }
  234. r = amdgpu_bo_pin(*bo_ptr, domain, gpu_addr);
  235. if (r) {
  236. dev_err(adev->dev, "(%d) kernel bo pin failed\n", r);
  237. goto error_unreserve;
  238. }
  239. if (cpu_addr) {
  240. r = amdgpu_bo_kmap(*bo_ptr, cpu_addr);
  241. if (r) {
  242. dev_err(adev->dev, "(%d) kernel bo map failed\n", r);
  243. goto error_unreserve;
  244. }
  245. }
  246. amdgpu_bo_unreserve(*bo_ptr);
  247. return 0;
  248. error_unreserve:
  249. amdgpu_bo_unreserve(*bo_ptr);
  250. error_free:
  251. amdgpu_bo_unref(bo_ptr);
  252. return r;
  253. }
  254. int amdgpu_bo_create_restricted(struct amdgpu_device *adev,
  255. unsigned long size, int byte_align,
  256. bool kernel, u32 domain, u64 flags,
  257. struct sg_table *sg,
  258. struct ttm_placement *placement,
  259. struct reservation_object *resv,
  260. struct amdgpu_bo **bo_ptr)
  261. {
  262. struct amdgpu_bo *bo;
  263. enum ttm_bo_type type;
  264. unsigned long page_align;
  265. size_t acc_size;
  266. int r;
  267. page_align = roundup(byte_align, PAGE_SIZE) >> PAGE_SHIFT;
  268. size = ALIGN(size, PAGE_SIZE);
  269. if (kernel) {
  270. type = ttm_bo_type_kernel;
  271. } else if (sg) {
  272. type = ttm_bo_type_sg;
  273. } else {
  274. type = ttm_bo_type_device;
  275. }
  276. *bo_ptr = NULL;
  277. acc_size = ttm_bo_dma_acc_size(&adev->mman.bdev, size,
  278. sizeof(struct amdgpu_bo));
  279. bo = kzalloc(sizeof(struct amdgpu_bo), GFP_KERNEL);
  280. if (bo == NULL)
  281. return -ENOMEM;
  282. r = drm_gem_object_init(adev->ddev, &bo->gem_base, size);
  283. if (unlikely(r)) {
  284. kfree(bo);
  285. return r;
  286. }
  287. bo->adev = adev;
  288. INIT_LIST_HEAD(&bo->list);
  289. INIT_LIST_HEAD(&bo->shadow_list);
  290. INIT_LIST_HEAD(&bo->va);
  291. bo->prefered_domains = domain & (AMDGPU_GEM_DOMAIN_VRAM |
  292. AMDGPU_GEM_DOMAIN_GTT |
  293. AMDGPU_GEM_DOMAIN_CPU |
  294. AMDGPU_GEM_DOMAIN_GDS |
  295. AMDGPU_GEM_DOMAIN_GWS |
  296. AMDGPU_GEM_DOMAIN_OA);
  297. bo->allowed_domains = bo->prefered_domains;
  298. if (!kernel && bo->allowed_domains == AMDGPU_GEM_DOMAIN_VRAM)
  299. bo->allowed_domains |= AMDGPU_GEM_DOMAIN_GTT;
  300. bo->flags = flags;
  301. /* For architectures that don't support WC memory,
  302. * mask out the WC flag from the BO
  303. */
  304. if (!drm_arch_can_wc_memory())
  305. bo->flags &= ~AMDGPU_GEM_CREATE_CPU_GTT_USWC;
  306. amdgpu_fill_placement_to_bo(bo, placement);
  307. /* Kernel allocation are uninterruptible */
  308. r = ttm_bo_init(&adev->mman.bdev, &bo->tbo, size, type,
  309. &bo->placement, page_align, !kernel, NULL,
  310. acc_size, sg, resv, &amdgpu_ttm_bo_destroy);
  311. if (unlikely(r != 0)) {
  312. return r;
  313. }
  314. if (flags & AMDGPU_GEM_CREATE_VRAM_CLEARED &&
  315. bo->tbo.mem.placement & TTM_PL_FLAG_VRAM) {
  316. struct fence *fence;
  317. if (adev->mman.buffer_funcs_ring == NULL ||
  318. !adev->mman.buffer_funcs_ring->ready) {
  319. r = -EBUSY;
  320. goto fail_free;
  321. }
  322. r = amdgpu_bo_reserve(bo, false);
  323. if (unlikely(r != 0))
  324. goto fail_free;
  325. amdgpu_ttm_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_VRAM);
  326. r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
  327. if (unlikely(r != 0))
  328. goto fail_unreserve;
  329. amdgpu_fill_buffer(bo, 0, bo->tbo.resv, &fence);
  330. amdgpu_bo_fence(bo, fence, false);
  331. amdgpu_bo_unreserve(bo);
  332. fence_put(bo->tbo.moving);
  333. bo->tbo.moving = fence_get(fence);
  334. fence_put(fence);
  335. }
  336. *bo_ptr = bo;
  337. trace_amdgpu_bo_create(bo);
  338. return 0;
  339. fail_unreserve:
  340. amdgpu_bo_unreserve(bo);
  341. fail_free:
  342. amdgpu_bo_unref(&bo);
  343. return r;
  344. }
  345. static int amdgpu_bo_create_shadow(struct amdgpu_device *adev,
  346. unsigned long size, int byte_align,
  347. struct amdgpu_bo *bo)
  348. {
  349. struct ttm_placement placement = {0};
  350. struct ttm_place placements[AMDGPU_GEM_DOMAIN_MAX + 1];
  351. int r;
  352. if (bo->shadow)
  353. return 0;
  354. bo->flags |= AMDGPU_GEM_CREATE_SHADOW;
  355. memset(&placements, 0,
  356. (AMDGPU_GEM_DOMAIN_MAX + 1) * sizeof(struct ttm_place));
  357. amdgpu_ttm_placement_init(adev, &placement,
  358. placements, AMDGPU_GEM_DOMAIN_GTT,
  359. AMDGPU_GEM_CREATE_CPU_GTT_USWC);
  360. r = amdgpu_bo_create_restricted(adev, size, byte_align, true,
  361. AMDGPU_GEM_DOMAIN_GTT,
  362. AMDGPU_GEM_CREATE_CPU_GTT_USWC,
  363. NULL, &placement,
  364. bo->tbo.resv,
  365. &bo->shadow);
  366. if (!r) {
  367. bo->shadow->parent = amdgpu_bo_ref(bo);
  368. mutex_lock(&adev->shadow_list_lock);
  369. list_add_tail(&bo->shadow_list, &adev->shadow_list);
  370. mutex_unlock(&adev->shadow_list_lock);
  371. }
  372. return r;
  373. }
  374. int amdgpu_bo_create(struct amdgpu_device *adev,
  375. unsigned long size, int byte_align,
  376. bool kernel, u32 domain, u64 flags,
  377. struct sg_table *sg,
  378. struct reservation_object *resv,
  379. struct amdgpu_bo **bo_ptr)
  380. {
  381. struct ttm_placement placement = {0};
  382. struct ttm_place placements[AMDGPU_GEM_DOMAIN_MAX + 1];
  383. int r;
  384. memset(&placements, 0,
  385. (AMDGPU_GEM_DOMAIN_MAX + 1) * sizeof(struct ttm_place));
  386. amdgpu_ttm_placement_init(adev, &placement,
  387. placements, domain, flags);
  388. r = amdgpu_bo_create_restricted(adev, size, byte_align, kernel,
  389. domain, flags, sg, &placement,
  390. resv, bo_ptr);
  391. if (r)
  392. return r;
  393. if (amdgpu_need_backup(adev) && (flags & AMDGPU_GEM_CREATE_SHADOW)) {
  394. r = amdgpu_bo_create_shadow(adev, size, byte_align, (*bo_ptr));
  395. if (r)
  396. amdgpu_bo_unref(bo_ptr);
  397. }
  398. return r;
  399. }
  400. int amdgpu_bo_backup_to_shadow(struct amdgpu_device *adev,
  401. struct amdgpu_ring *ring,
  402. struct amdgpu_bo *bo,
  403. struct reservation_object *resv,
  404. struct fence **fence,
  405. bool direct)
  406. {
  407. struct amdgpu_bo *shadow = bo->shadow;
  408. uint64_t bo_addr, shadow_addr;
  409. int r;
  410. if (!shadow)
  411. return -EINVAL;
  412. bo_addr = amdgpu_bo_gpu_offset(bo);
  413. shadow_addr = amdgpu_bo_gpu_offset(bo->shadow);
  414. r = reservation_object_reserve_shared(bo->tbo.resv);
  415. if (r)
  416. goto err;
  417. r = amdgpu_copy_buffer(ring, bo_addr, shadow_addr,
  418. amdgpu_bo_size(bo), resv, fence,
  419. direct);
  420. if (!r)
  421. amdgpu_bo_fence(bo, *fence, true);
  422. err:
  423. return r;
  424. }
  425. int amdgpu_bo_restore_from_shadow(struct amdgpu_device *adev,
  426. struct amdgpu_ring *ring,
  427. struct amdgpu_bo *bo,
  428. struct reservation_object *resv,
  429. struct fence **fence,
  430. bool direct)
  431. {
  432. struct amdgpu_bo *shadow = bo->shadow;
  433. uint64_t bo_addr, shadow_addr;
  434. int r;
  435. if (!shadow)
  436. return -EINVAL;
  437. bo_addr = amdgpu_bo_gpu_offset(bo);
  438. shadow_addr = amdgpu_bo_gpu_offset(bo->shadow);
  439. r = reservation_object_reserve_shared(bo->tbo.resv);
  440. if (r)
  441. goto err;
  442. r = amdgpu_copy_buffer(ring, shadow_addr, bo_addr,
  443. amdgpu_bo_size(bo), resv, fence,
  444. direct);
  445. if (!r)
  446. amdgpu_bo_fence(bo, *fence, true);
  447. err:
  448. return r;
  449. }
  450. int amdgpu_bo_kmap(struct amdgpu_bo *bo, void **ptr)
  451. {
  452. bool is_iomem;
  453. long r;
  454. if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)
  455. return -EPERM;
  456. if (bo->kptr) {
  457. if (ptr) {
  458. *ptr = bo->kptr;
  459. }
  460. return 0;
  461. }
  462. r = reservation_object_wait_timeout_rcu(bo->tbo.resv, false, false,
  463. MAX_SCHEDULE_TIMEOUT);
  464. if (r < 0)
  465. return r;
  466. r = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages, &bo->kmap);
  467. if (r)
  468. return r;
  469. bo->kptr = ttm_kmap_obj_virtual(&bo->kmap, &is_iomem);
  470. if (ptr)
  471. *ptr = bo->kptr;
  472. return 0;
  473. }
  474. void amdgpu_bo_kunmap(struct amdgpu_bo *bo)
  475. {
  476. if (bo->kptr == NULL)
  477. return;
  478. bo->kptr = NULL;
  479. ttm_bo_kunmap(&bo->kmap);
  480. }
  481. struct amdgpu_bo *amdgpu_bo_ref(struct amdgpu_bo *bo)
  482. {
  483. if (bo == NULL)
  484. return NULL;
  485. ttm_bo_reference(&bo->tbo);
  486. return bo;
  487. }
  488. void amdgpu_bo_unref(struct amdgpu_bo **bo)
  489. {
  490. struct ttm_buffer_object *tbo;
  491. if ((*bo) == NULL)
  492. return;
  493. tbo = &((*bo)->tbo);
  494. ttm_bo_unref(&tbo);
  495. if (tbo == NULL)
  496. *bo = NULL;
  497. }
  498. int amdgpu_bo_pin_restricted(struct amdgpu_bo *bo, u32 domain,
  499. u64 min_offset, u64 max_offset,
  500. u64 *gpu_addr)
  501. {
  502. int r, i;
  503. unsigned fpfn, lpfn;
  504. if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm))
  505. return -EPERM;
  506. if (WARN_ON_ONCE(min_offset > max_offset))
  507. return -EINVAL;
  508. if (bo->pin_count) {
  509. uint32_t mem_type = bo->tbo.mem.mem_type;
  510. if (domain != amdgpu_mem_type_to_domain(mem_type))
  511. return -EINVAL;
  512. bo->pin_count++;
  513. if (gpu_addr)
  514. *gpu_addr = amdgpu_bo_gpu_offset(bo);
  515. if (max_offset != 0) {
  516. u64 domain_start = bo->tbo.bdev->man[mem_type].gpu_offset;
  517. WARN_ON_ONCE(max_offset <
  518. (amdgpu_bo_gpu_offset(bo) - domain_start));
  519. }
  520. return 0;
  521. }
  522. amdgpu_ttm_placement_from_domain(bo, domain);
  523. for (i = 0; i < bo->placement.num_placement; i++) {
  524. /* force to pin into visible video ram */
  525. if ((bo->placements[i].flags & TTM_PL_FLAG_VRAM) &&
  526. !(bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS) &&
  527. (!max_offset || max_offset >
  528. bo->adev->mc.visible_vram_size)) {
  529. if (WARN_ON_ONCE(min_offset >
  530. bo->adev->mc.visible_vram_size))
  531. return -EINVAL;
  532. fpfn = min_offset >> PAGE_SHIFT;
  533. lpfn = bo->adev->mc.visible_vram_size >> PAGE_SHIFT;
  534. } else {
  535. fpfn = min_offset >> PAGE_SHIFT;
  536. lpfn = max_offset >> PAGE_SHIFT;
  537. }
  538. if (fpfn > bo->placements[i].fpfn)
  539. bo->placements[i].fpfn = fpfn;
  540. if (!bo->placements[i].lpfn ||
  541. (lpfn && lpfn < bo->placements[i].lpfn))
  542. bo->placements[i].lpfn = lpfn;
  543. bo->placements[i].flags |= TTM_PL_FLAG_NO_EVICT;
  544. }
  545. r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
  546. if (unlikely(r)) {
  547. dev_err(bo->adev->dev, "%p pin failed\n", bo);
  548. goto error;
  549. }
  550. bo->pin_count = 1;
  551. if (gpu_addr != NULL)
  552. *gpu_addr = amdgpu_bo_gpu_offset(bo);
  553. if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
  554. bo->adev->vram_pin_size += amdgpu_bo_size(bo);
  555. if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)
  556. bo->adev->invisible_pin_size += amdgpu_bo_size(bo);
  557. } else if (domain == AMDGPU_GEM_DOMAIN_GTT) {
  558. bo->adev->gart_pin_size += amdgpu_bo_size(bo);
  559. }
  560. error:
  561. return r;
  562. }
  563. int amdgpu_bo_pin(struct amdgpu_bo *bo, u32 domain, u64 *gpu_addr)
  564. {
  565. return amdgpu_bo_pin_restricted(bo, domain, 0, 0, gpu_addr);
  566. }
  567. int amdgpu_bo_unpin(struct amdgpu_bo *bo)
  568. {
  569. int r, i;
  570. if (!bo->pin_count) {
  571. dev_warn(bo->adev->dev, "%p unpin not necessary\n", bo);
  572. return 0;
  573. }
  574. bo->pin_count--;
  575. if (bo->pin_count)
  576. return 0;
  577. for (i = 0; i < bo->placement.num_placement; i++) {
  578. bo->placements[i].lpfn = 0;
  579. bo->placements[i].flags &= ~TTM_PL_FLAG_NO_EVICT;
  580. }
  581. r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
  582. if (unlikely(r)) {
  583. dev_err(bo->adev->dev, "%p validate failed for unpin\n", bo);
  584. goto error;
  585. }
  586. if (bo->tbo.mem.mem_type == TTM_PL_VRAM) {
  587. bo->adev->vram_pin_size -= amdgpu_bo_size(bo);
  588. if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)
  589. bo->adev->invisible_pin_size -= amdgpu_bo_size(bo);
  590. } else {
  591. bo->adev->gart_pin_size -= amdgpu_bo_size(bo);
  592. }
  593. error:
  594. return r;
  595. }
  596. int amdgpu_bo_evict_vram(struct amdgpu_device *adev)
  597. {
  598. /* late 2.6.33 fix IGP hibernate - we need pm ops to do this correct */
  599. if (0 && (adev->flags & AMD_IS_APU)) {
  600. /* Useless to evict on IGP chips */
  601. return 0;
  602. }
  603. return ttm_bo_evict_mm(&adev->mman.bdev, TTM_PL_VRAM);
  604. }
  605. static const char *amdgpu_vram_names[] = {
  606. "UNKNOWN",
  607. "GDDR1",
  608. "DDR2",
  609. "GDDR3",
  610. "GDDR4",
  611. "GDDR5",
  612. "HBM",
  613. "DDR3"
  614. };
  615. int amdgpu_bo_init(struct amdgpu_device *adev)
  616. {
  617. /* Add an MTRR for the VRAM */
  618. adev->mc.vram_mtrr = arch_phys_wc_add(adev->mc.aper_base,
  619. adev->mc.aper_size);
  620. DRM_INFO("Detected VRAM RAM=%lluM, BAR=%lluM\n",
  621. adev->mc.mc_vram_size >> 20,
  622. (unsigned long long)adev->mc.aper_size >> 20);
  623. DRM_INFO("RAM width %dbits %s\n",
  624. adev->mc.vram_width, amdgpu_vram_names[adev->mc.vram_type]);
  625. return amdgpu_ttm_init(adev);
  626. }
  627. void amdgpu_bo_fini(struct amdgpu_device *adev)
  628. {
  629. amdgpu_ttm_fini(adev);
  630. arch_phys_wc_del(adev->mc.vram_mtrr);
  631. }
  632. int amdgpu_bo_fbdev_mmap(struct amdgpu_bo *bo,
  633. struct vm_area_struct *vma)
  634. {
  635. return ttm_fbdev_mmap(vma, &bo->tbo);
  636. }
  637. int amdgpu_bo_set_tiling_flags(struct amdgpu_bo *bo, u64 tiling_flags)
  638. {
  639. if (AMDGPU_TILING_GET(tiling_flags, TILE_SPLIT) > 6)
  640. return -EINVAL;
  641. bo->tiling_flags = tiling_flags;
  642. return 0;
  643. }
  644. void amdgpu_bo_get_tiling_flags(struct amdgpu_bo *bo, u64 *tiling_flags)
  645. {
  646. lockdep_assert_held(&bo->tbo.resv->lock.base);
  647. if (tiling_flags)
  648. *tiling_flags = bo->tiling_flags;
  649. }
  650. int amdgpu_bo_set_metadata (struct amdgpu_bo *bo, void *metadata,
  651. uint32_t metadata_size, uint64_t flags)
  652. {
  653. void *buffer;
  654. if (!metadata_size) {
  655. if (bo->metadata_size) {
  656. kfree(bo->metadata);
  657. bo->metadata = NULL;
  658. bo->metadata_size = 0;
  659. }
  660. return 0;
  661. }
  662. if (metadata == NULL)
  663. return -EINVAL;
  664. buffer = kmemdup(metadata, metadata_size, GFP_KERNEL);
  665. if (buffer == NULL)
  666. return -ENOMEM;
  667. kfree(bo->metadata);
  668. bo->metadata_flags = flags;
  669. bo->metadata = buffer;
  670. bo->metadata_size = metadata_size;
  671. return 0;
  672. }
  673. int amdgpu_bo_get_metadata(struct amdgpu_bo *bo, void *buffer,
  674. size_t buffer_size, uint32_t *metadata_size,
  675. uint64_t *flags)
  676. {
  677. if (!buffer && !metadata_size)
  678. return -EINVAL;
  679. if (buffer) {
  680. if (buffer_size < bo->metadata_size)
  681. return -EINVAL;
  682. if (bo->metadata_size)
  683. memcpy(buffer, bo->metadata, bo->metadata_size);
  684. }
  685. if (metadata_size)
  686. *metadata_size = bo->metadata_size;
  687. if (flags)
  688. *flags = bo->metadata_flags;
  689. return 0;
  690. }
  691. void amdgpu_bo_move_notify(struct ttm_buffer_object *bo,
  692. struct ttm_mem_reg *new_mem)
  693. {
  694. struct amdgpu_bo *rbo;
  695. struct ttm_mem_reg *old_mem = &bo->mem;
  696. if (!amdgpu_ttm_bo_is_amdgpu_bo(bo))
  697. return;
  698. rbo = container_of(bo, struct amdgpu_bo, tbo);
  699. amdgpu_vm_bo_invalidate(rbo->adev, rbo);
  700. /* update statistics */
  701. if (!new_mem)
  702. return;
  703. /* move_notify is called before move happens */
  704. amdgpu_update_memory_usage(rbo->adev, &bo->mem, new_mem);
  705. trace_amdgpu_ttm_bo_move(rbo, new_mem->mem_type, old_mem->mem_type);
  706. }
  707. int amdgpu_bo_fault_reserve_notify(struct ttm_buffer_object *bo)
  708. {
  709. struct amdgpu_device *adev;
  710. struct amdgpu_bo *abo;
  711. unsigned long offset, size, lpfn;
  712. int i, r;
  713. if (!amdgpu_ttm_bo_is_amdgpu_bo(bo))
  714. return 0;
  715. abo = container_of(bo, struct amdgpu_bo, tbo);
  716. adev = abo->adev;
  717. if (bo->mem.mem_type != TTM_PL_VRAM)
  718. return 0;
  719. size = bo->mem.num_pages << PAGE_SHIFT;
  720. offset = bo->mem.start << PAGE_SHIFT;
  721. if ((offset + size) <= adev->mc.visible_vram_size)
  722. return 0;
  723. /* Can't move a pinned BO to visible VRAM */
  724. if (abo->pin_count > 0)
  725. return -EINVAL;
  726. /* hurrah the memory is not visible ! */
  727. amdgpu_ttm_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_VRAM);
  728. lpfn = adev->mc.visible_vram_size >> PAGE_SHIFT;
  729. for (i = 0; i < abo->placement.num_placement; i++) {
  730. /* Force into visible VRAM */
  731. if ((abo->placements[i].flags & TTM_PL_FLAG_VRAM) &&
  732. (!abo->placements[i].lpfn ||
  733. abo->placements[i].lpfn > lpfn))
  734. abo->placements[i].lpfn = lpfn;
  735. }
  736. r = ttm_bo_validate(bo, &abo->placement, false, false);
  737. if (unlikely(r == -ENOMEM)) {
  738. amdgpu_ttm_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_GTT);
  739. return ttm_bo_validate(bo, &abo->placement, false, false);
  740. } else if (unlikely(r != 0)) {
  741. return r;
  742. }
  743. offset = bo->mem.start << PAGE_SHIFT;
  744. /* this should never happen */
  745. if ((offset + size) > adev->mc.visible_vram_size)
  746. return -EINVAL;
  747. return 0;
  748. }
  749. /**
  750. * amdgpu_bo_fence - add fence to buffer object
  751. *
  752. * @bo: buffer object in question
  753. * @fence: fence to add
  754. * @shared: true if fence should be added shared
  755. *
  756. */
  757. void amdgpu_bo_fence(struct amdgpu_bo *bo, struct fence *fence,
  758. bool shared)
  759. {
  760. struct reservation_object *resv = bo->tbo.resv;
  761. if (shared)
  762. reservation_object_add_shared_fence(resv, fence);
  763. else
  764. reservation_object_add_excl_fence(resv, fence);
  765. }
  766. /**
  767. * amdgpu_bo_gpu_offset - return GPU offset of bo
  768. * @bo: amdgpu object for which we query the offset
  769. *
  770. * Returns current GPU offset of the object.
  771. *
  772. * Note: object should either be pinned or reserved when calling this
  773. * function, it might be useful to add check for this for debugging.
  774. */
  775. u64 amdgpu_bo_gpu_offset(struct amdgpu_bo *bo)
  776. {
  777. WARN_ON_ONCE(bo->tbo.mem.mem_type == TTM_PL_SYSTEM);
  778. WARN_ON_ONCE(!ww_mutex_is_locked(&bo->tbo.resv->lock) &&
  779. !bo->pin_count);
  780. return bo->tbo.offset;
  781. }