amdgpu_object.c 22 KB

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