ttm_bo.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761
  1. /**************************************************************************
  2. *
  3. * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
  4. * All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sub license, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the
  15. * next paragraph) shall be included in all copies or substantial portions
  16. * of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  21. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  22. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  23. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  24. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. **************************************************************************/
  27. /*
  28. * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
  29. */
  30. #define pr_fmt(fmt) "[TTM] " fmt
  31. #include <drm/ttm/ttm_module.h>
  32. #include <drm/ttm/ttm_bo_driver.h>
  33. #include <drm/ttm/ttm_placement.h>
  34. #include <linux/jiffies.h>
  35. #include <linux/slab.h>
  36. #include <linux/sched.h>
  37. #include <linux/mm.h>
  38. #include <linux/file.h>
  39. #include <linux/module.h>
  40. #include <linux/atomic.h>
  41. #include <linux/reservation.h>
  42. #define TTM_ASSERT_LOCKED(param)
  43. #define TTM_DEBUG(fmt, arg...)
  44. #define TTM_BO_HASH_ORDER 13
  45. static int ttm_bo_swapout(struct ttm_mem_shrink *shrink);
  46. static void ttm_bo_global_kobj_release(struct kobject *kobj);
  47. static struct attribute ttm_bo_count = {
  48. .name = "bo_count",
  49. .mode = S_IRUGO
  50. };
  51. static inline int ttm_mem_type_from_place(const struct ttm_place *place,
  52. uint32_t *mem_type)
  53. {
  54. int pos;
  55. pos = ffs(place->flags & TTM_PL_MASK_MEM);
  56. if (unlikely(!pos))
  57. return -EINVAL;
  58. *mem_type = pos - 1;
  59. return 0;
  60. }
  61. static void ttm_mem_type_debug(struct ttm_bo_device *bdev, int mem_type)
  62. {
  63. struct ttm_mem_type_manager *man = &bdev->man[mem_type];
  64. pr_err(" has_type: %d\n", man->has_type);
  65. pr_err(" use_type: %d\n", man->use_type);
  66. pr_err(" flags: 0x%08X\n", man->flags);
  67. pr_err(" gpu_offset: 0x%08llX\n", man->gpu_offset);
  68. pr_err(" size: %llu\n", man->size);
  69. pr_err(" available_caching: 0x%08X\n", man->available_caching);
  70. pr_err(" default_caching: 0x%08X\n", man->default_caching);
  71. if (mem_type != TTM_PL_SYSTEM)
  72. (*man->func->debug)(man, TTM_PFX);
  73. }
  74. static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
  75. struct ttm_placement *placement)
  76. {
  77. int i, ret, mem_type;
  78. pr_err("No space for %p (%lu pages, %luK, %luM)\n",
  79. bo, bo->mem.num_pages, bo->mem.size >> 10,
  80. bo->mem.size >> 20);
  81. for (i = 0; i < placement->num_placement; i++) {
  82. ret = ttm_mem_type_from_place(&placement->placement[i],
  83. &mem_type);
  84. if (ret)
  85. return;
  86. pr_err(" placement[%d]=0x%08X (%d)\n",
  87. i, placement->placement[i].flags, mem_type);
  88. ttm_mem_type_debug(bo->bdev, mem_type);
  89. }
  90. }
  91. static ssize_t ttm_bo_global_show(struct kobject *kobj,
  92. struct attribute *attr,
  93. char *buffer)
  94. {
  95. struct ttm_bo_global *glob =
  96. container_of(kobj, struct ttm_bo_global, kobj);
  97. return snprintf(buffer, PAGE_SIZE, "%lu\n",
  98. (unsigned long) atomic_read(&glob->bo_count));
  99. }
  100. static struct attribute *ttm_bo_global_attrs[] = {
  101. &ttm_bo_count,
  102. NULL
  103. };
  104. static const struct sysfs_ops ttm_bo_global_ops = {
  105. .show = &ttm_bo_global_show
  106. };
  107. static struct kobj_type ttm_bo_glob_kobj_type = {
  108. .release = &ttm_bo_global_kobj_release,
  109. .sysfs_ops = &ttm_bo_global_ops,
  110. .default_attrs = ttm_bo_global_attrs
  111. };
  112. static inline uint32_t ttm_bo_type_flags(unsigned type)
  113. {
  114. return 1 << (type);
  115. }
  116. static void ttm_bo_release_list(struct kref *list_kref)
  117. {
  118. struct ttm_buffer_object *bo =
  119. container_of(list_kref, struct ttm_buffer_object, list_kref);
  120. struct ttm_bo_device *bdev = bo->bdev;
  121. size_t acc_size = bo->acc_size;
  122. BUG_ON(kref_read(&bo->list_kref));
  123. BUG_ON(kref_read(&bo->kref));
  124. BUG_ON(atomic_read(&bo->cpu_writers));
  125. BUG_ON(bo->mem.mm_node != NULL);
  126. BUG_ON(!list_empty(&bo->lru));
  127. BUG_ON(!list_empty(&bo->ddestroy));
  128. ttm_tt_destroy(bo->ttm);
  129. atomic_dec(&bo->glob->bo_count);
  130. dma_fence_put(bo->moving);
  131. if (bo->resv == &bo->ttm_resv)
  132. reservation_object_fini(&bo->ttm_resv);
  133. mutex_destroy(&bo->wu_mutex);
  134. if (bo->destroy)
  135. bo->destroy(bo);
  136. else {
  137. kfree(bo);
  138. }
  139. ttm_mem_global_free(bdev->glob->mem_glob, acc_size);
  140. }
  141. void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
  142. {
  143. struct ttm_bo_device *bdev = bo->bdev;
  144. struct ttm_mem_type_manager *man;
  145. lockdep_assert_held(&bo->resv->lock.base);
  146. if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
  147. BUG_ON(!list_empty(&bo->lru));
  148. man = &bdev->man[bo->mem.mem_type];
  149. list_add_tail(&bo->lru, &man->lru[bo->priority]);
  150. kref_get(&bo->list_kref);
  151. if (bo->ttm && !(bo->ttm->page_flags & TTM_PAGE_FLAG_SG)) {
  152. list_add_tail(&bo->swap,
  153. &bo->glob->swap_lru[bo->priority]);
  154. kref_get(&bo->list_kref);
  155. }
  156. }
  157. }
  158. EXPORT_SYMBOL(ttm_bo_add_to_lru);
  159. static void ttm_bo_ref_bug(struct kref *list_kref)
  160. {
  161. BUG();
  162. }
  163. void ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
  164. {
  165. if (!list_empty(&bo->swap)) {
  166. list_del_init(&bo->swap);
  167. kref_put(&bo->list_kref, ttm_bo_ref_bug);
  168. }
  169. if (!list_empty(&bo->lru)) {
  170. list_del_init(&bo->lru);
  171. kref_put(&bo->list_kref, ttm_bo_ref_bug);
  172. }
  173. /*
  174. * TODO: Add a driver hook to delete from
  175. * driver-specific LRU's here.
  176. */
  177. }
  178. void ttm_bo_del_sub_from_lru(struct ttm_buffer_object *bo)
  179. {
  180. spin_lock(&bo->glob->lru_lock);
  181. ttm_bo_del_from_lru(bo);
  182. spin_unlock(&bo->glob->lru_lock);
  183. }
  184. EXPORT_SYMBOL(ttm_bo_del_sub_from_lru);
  185. void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo)
  186. {
  187. lockdep_assert_held(&bo->resv->lock.base);
  188. ttm_bo_del_from_lru(bo);
  189. ttm_bo_add_to_lru(bo);
  190. }
  191. EXPORT_SYMBOL(ttm_bo_move_to_lru_tail);
  192. /*
  193. * Call bo->mutex locked.
  194. */
  195. static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
  196. {
  197. struct ttm_bo_device *bdev = bo->bdev;
  198. struct ttm_bo_global *glob = bo->glob;
  199. int ret = 0;
  200. uint32_t page_flags = 0;
  201. TTM_ASSERT_LOCKED(&bo->mutex);
  202. bo->ttm = NULL;
  203. if (bdev->need_dma32)
  204. page_flags |= TTM_PAGE_FLAG_DMA32;
  205. switch (bo->type) {
  206. case ttm_bo_type_device:
  207. if (zero_alloc)
  208. page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
  209. case ttm_bo_type_kernel:
  210. bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
  211. page_flags, glob->dummy_read_page);
  212. if (unlikely(bo->ttm == NULL))
  213. ret = -ENOMEM;
  214. break;
  215. case ttm_bo_type_sg:
  216. bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
  217. page_flags | TTM_PAGE_FLAG_SG,
  218. glob->dummy_read_page);
  219. if (unlikely(bo->ttm == NULL)) {
  220. ret = -ENOMEM;
  221. break;
  222. }
  223. bo->ttm->sg = bo->sg;
  224. break;
  225. default:
  226. pr_err("Illegal buffer object type\n");
  227. ret = -EINVAL;
  228. break;
  229. }
  230. return ret;
  231. }
  232. static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
  233. struct ttm_mem_reg *mem,
  234. bool evict, bool interruptible,
  235. bool no_wait_gpu)
  236. {
  237. struct ttm_bo_device *bdev = bo->bdev;
  238. bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
  239. bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
  240. struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
  241. struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
  242. int ret = 0;
  243. if (old_is_pci || new_is_pci ||
  244. ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) {
  245. ret = ttm_mem_io_lock(old_man, true);
  246. if (unlikely(ret != 0))
  247. goto out_err;
  248. ttm_bo_unmap_virtual_locked(bo);
  249. ttm_mem_io_unlock(old_man);
  250. }
  251. /*
  252. * Create and bind a ttm if required.
  253. */
  254. if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
  255. if (bo->ttm == NULL) {
  256. bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED);
  257. ret = ttm_bo_add_ttm(bo, zero);
  258. if (ret)
  259. goto out_err;
  260. }
  261. ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
  262. if (ret)
  263. goto out_err;
  264. if (mem->mem_type != TTM_PL_SYSTEM) {
  265. ret = ttm_tt_bind(bo->ttm, mem);
  266. if (ret)
  267. goto out_err;
  268. }
  269. if (bo->mem.mem_type == TTM_PL_SYSTEM) {
  270. if (bdev->driver->move_notify)
  271. bdev->driver->move_notify(bo, evict, mem);
  272. bo->mem = *mem;
  273. mem->mm_node = NULL;
  274. goto moved;
  275. }
  276. }
  277. if (bdev->driver->move_notify)
  278. bdev->driver->move_notify(bo, evict, mem);
  279. if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
  280. !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
  281. ret = ttm_bo_move_ttm(bo, interruptible, no_wait_gpu, mem);
  282. else if (bdev->driver->move)
  283. ret = bdev->driver->move(bo, evict, interruptible,
  284. no_wait_gpu, mem);
  285. else
  286. ret = ttm_bo_move_memcpy(bo, interruptible, no_wait_gpu, mem);
  287. if (ret) {
  288. if (bdev->driver->move_notify) {
  289. struct ttm_mem_reg tmp_mem = *mem;
  290. *mem = bo->mem;
  291. bo->mem = tmp_mem;
  292. bdev->driver->move_notify(bo, false, mem);
  293. bo->mem = *mem;
  294. *mem = tmp_mem;
  295. }
  296. goto out_err;
  297. }
  298. moved:
  299. if (bo->evicted) {
  300. if (bdev->driver->invalidate_caches) {
  301. ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
  302. if (ret)
  303. pr_err("Can not flush read caches\n");
  304. }
  305. bo->evicted = false;
  306. }
  307. if (bo->mem.mm_node) {
  308. bo->offset = (bo->mem.start << PAGE_SHIFT) +
  309. bdev->man[bo->mem.mem_type].gpu_offset;
  310. bo->cur_placement = bo->mem.placement;
  311. } else
  312. bo->offset = 0;
  313. return 0;
  314. out_err:
  315. new_man = &bdev->man[bo->mem.mem_type];
  316. if (new_man->flags & TTM_MEMTYPE_FLAG_FIXED) {
  317. ttm_tt_destroy(bo->ttm);
  318. bo->ttm = NULL;
  319. }
  320. return ret;
  321. }
  322. /**
  323. * Call bo::reserved.
  324. * Will release GPU memory type usage on destruction.
  325. * This is the place to put in driver specific hooks to release
  326. * driver private resources.
  327. * Will release the bo::reserved lock.
  328. */
  329. static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
  330. {
  331. if (bo->bdev->driver->move_notify)
  332. bo->bdev->driver->move_notify(bo, false, NULL);
  333. ttm_tt_destroy(bo->ttm);
  334. bo->ttm = NULL;
  335. ttm_bo_mem_put(bo, &bo->mem);
  336. ww_mutex_unlock (&bo->resv->lock);
  337. }
  338. static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo)
  339. {
  340. struct reservation_object_list *fobj;
  341. struct dma_fence *fence;
  342. int i;
  343. fobj = reservation_object_get_list(bo->resv);
  344. fence = reservation_object_get_excl(bo->resv);
  345. if (fence && !fence->ops->signaled)
  346. dma_fence_enable_sw_signaling(fence);
  347. for (i = 0; fobj && i < fobj->shared_count; ++i) {
  348. fence = rcu_dereference_protected(fobj->shared[i],
  349. reservation_object_held(bo->resv));
  350. if (!fence->ops->signaled)
  351. dma_fence_enable_sw_signaling(fence);
  352. }
  353. }
  354. static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
  355. {
  356. struct ttm_bo_device *bdev = bo->bdev;
  357. struct ttm_bo_global *glob = bo->glob;
  358. int ret;
  359. spin_lock(&glob->lru_lock);
  360. ret = __ttm_bo_reserve(bo, false, true, NULL);
  361. if (!ret) {
  362. if (!ttm_bo_wait(bo, false, true)) {
  363. ttm_bo_del_from_lru(bo);
  364. spin_unlock(&glob->lru_lock);
  365. ttm_bo_cleanup_memtype_use(bo);
  366. return;
  367. } else
  368. ttm_bo_flush_all_fences(bo);
  369. /*
  370. * Make NO_EVICT bos immediately available to
  371. * shrinkers, now that they are queued for
  372. * destruction.
  373. */
  374. if (bo->mem.placement & TTM_PL_FLAG_NO_EVICT) {
  375. bo->mem.placement &= ~TTM_PL_FLAG_NO_EVICT;
  376. ttm_bo_add_to_lru(bo);
  377. }
  378. __ttm_bo_unreserve(bo);
  379. }
  380. kref_get(&bo->list_kref);
  381. list_add_tail(&bo->ddestroy, &bdev->ddestroy);
  382. spin_unlock(&glob->lru_lock);
  383. schedule_delayed_work(&bdev->wq,
  384. ((HZ / 100) < 1) ? 1 : HZ / 100);
  385. }
  386. /**
  387. * function ttm_bo_cleanup_refs_and_unlock
  388. * If bo idle, remove from delayed- and lru lists, and unref.
  389. * If not idle, do nothing.
  390. *
  391. * Must be called with lru_lock and reservation held, this function
  392. * will drop both before returning.
  393. *
  394. * @interruptible Any sleeps should occur interruptibly.
  395. * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
  396. */
  397. static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo,
  398. bool interruptible,
  399. bool no_wait_gpu)
  400. {
  401. struct ttm_bo_global *glob = bo->glob;
  402. int ret;
  403. ret = ttm_bo_wait(bo, false, true);
  404. if (ret && !no_wait_gpu) {
  405. long lret;
  406. ww_mutex_unlock(&bo->resv->lock);
  407. spin_unlock(&glob->lru_lock);
  408. lret = reservation_object_wait_timeout_rcu(bo->resv,
  409. true,
  410. interruptible,
  411. 30 * HZ);
  412. if (lret < 0)
  413. return lret;
  414. else if (lret == 0)
  415. return -EBUSY;
  416. spin_lock(&glob->lru_lock);
  417. ret = __ttm_bo_reserve(bo, false, true, NULL);
  418. /*
  419. * We raced, and lost, someone else holds the reservation now,
  420. * and is probably busy in ttm_bo_cleanup_memtype_use.
  421. *
  422. * Even if it's not the case, because we finished waiting any
  423. * delayed destruction would succeed, so just return success
  424. * here.
  425. */
  426. if (ret) {
  427. spin_unlock(&glob->lru_lock);
  428. return 0;
  429. }
  430. /*
  431. * remove sync_obj with ttm_bo_wait, the wait should be
  432. * finished, and no new wait object should have been added.
  433. */
  434. ret = ttm_bo_wait(bo, false, true);
  435. WARN_ON(ret);
  436. }
  437. if (ret || unlikely(list_empty(&bo->ddestroy))) {
  438. __ttm_bo_unreserve(bo);
  439. spin_unlock(&glob->lru_lock);
  440. return ret;
  441. }
  442. ttm_bo_del_from_lru(bo);
  443. list_del_init(&bo->ddestroy);
  444. kref_put(&bo->list_kref, ttm_bo_ref_bug);
  445. spin_unlock(&glob->lru_lock);
  446. ttm_bo_cleanup_memtype_use(bo);
  447. return 0;
  448. }
  449. /**
  450. * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
  451. * encountered buffers.
  452. */
  453. static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
  454. {
  455. struct ttm_bo_global *glob = bdev->glob;
  456. struct ttm_buffer_object *entry = NULL;
  457. int ret = 0;
  458. spin_lock(&glob->lru_lock);
  459. if (list_empty(&bdev->ddestroy))
  460. goto out_unlock;
  461. entry = list_first_entry(&bdev->ddestroy,
  462. struct ttm_buffer_object, ddestroy);
  463. kref_get(&entry->list_kref);
  464. for (;;) {
  465. struct ttm_buffer_object *nentry = NULL;
  466. if (entry->ddestroy.next != &bdev->ddestroy) {
  467. nentry = list_first_entry(&entry->ddestroy,
  468. struct ttm_buffer_object, ddestroy);
  469. kref_get(&nentry->list_kref);
  470. }
  471. ret = __ttm_bo_reserve(entry, false, true, NULL);
  472. if (remove_all && ret) {
  473. spin_unlock(&glob->lru_lock);
  474. ret = __ttm_bo_reserve(entry, false, false, NULL);
  475. spin_lock(&glob->lru_lock);
  476. }
  477. if (!ret)
  478. ret = ttm_bo_cleanup_refs_and_unlock(entry, false,
  479. !remove_all);
  480. else
  481. spin_unlock(&glob->lru_lock);
  482. kref_put(&entry->list_kref, ttm_bo_release_list);
  483. entry = nentry;
  484. if (ret || !entry)
  485. goto out;
  486. spin_lock(&glob->lru_lock);
  487. if (list_empty(&entry->ddestroy))
  488. break;
  489. }
  490. out_unlock:
  491. spin_unlock(&glob->lru_lock);
  492. out:
  493. if (entry)
  494. kref_put(&entry->list_kref, ttm_bo_release_list);
  495. return ret;
  496. }
  497. static void ttm_bo_delayed_workqueue(struct work_struct *work)
  498. {
  499. struct ttm_bo_device *bdev =
  500. container_of(work, struct ttm_bo_device, wq.work);
  501. if (ttm_bo_delayed_delete(bdev, false)) {
  502. schedule_delayed_work(&bdev->wq,
  503. ((HZ / 100) < 1) ? 1 : HZ / 100);
  504. }
  505. }
  506. static void ttm_bo_release(struct kref *kref)
  507. {
  508. struct ttm_buffer_object *bo =
  509. container_of(kref, struct ttm_buffer_object, kref);
  510. struct ttm_bo_device *bdev = bo->bdev;
  511. struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
  512. drm_vma_offset_remove(&bdev->vma_manager, &bo->vma_node);
  513. ttm_mem_io_lock(man, false);
  514. ttm_mem_io_free_vm(bo);
  515. ttm_mem_io_unlock(man);
  516. ttm_bo_cleanup_refs_or_queue(bo);
  517. kref_put(&bo->list_kref, ttm_bo_release_list);
  518. }
  519. void ttm_bo_unref(struct ttm_buffer_object **p_bo)
  520. {
  521. struct ttm_buffer_object *bo = *p_bo;
  522. *p_bo = NULL;
  523. kref_put(&bo->kref, ttm_bo_release);
  524. }
  525. EXPORT_SYMBOL(ttm_bo_unref);
  526. int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
  527. {
  528. return cancel_delayed_work_sync(&bdev->wq);
  529. }
  530. EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
  531. void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
  532. {
  533. if (resched)
  534. schedule_delayed_work(&bdev->wq,
  535. ((HZ / 100) < 1) ? 1 : HZ / 100);
  536. }
  537. EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
  538. static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
  539. bool no_wait_gpu)
  540. {
  541. struct ttm_bo_device *bdev = bo->bdev;
  542. struct ttm_mem_reg evict_mem;
  543. struct ttm_placement placement;
  544. int ret = 0;
  545. lockdep_assert_held(&bo->resv->lock.base);
  546. evict_mem = bo->mem;
  547. evict_mem.mm_node = NULL;
  548. evict_mem.bus.io_reserved_vm = false;
  549. evict_mem.bus.io_reserved_count = 0;
  550. placement.num_placement = 0;
  551. placement.num_busy_placement = 0;
  552. bdev->driver->evict_flags(bo, &placement);
  553. ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
  554. no_wait_gpu);
  555. if (ret) {
  556. if (ret != -ERESTARTSYS) {
  557. pr_err("Failed to find memory space for buffer 0x%p eviction\n",
  558. bo);
  559. ttm_bo_mem_space_debug(bo, &placement);
  560. }
  561. goto out;
  562. }
  563. ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
  564. no_wait_gpu);
  565. if (unlikely(ret)) {
  566. if (ret != -ERESTARTSYS)
  567. pr_err("Buffer eviction failed\n");
  568. ttm_bo_mem_put(bo, &evict_mem);
  569. goto out;
  570. }
  571. bo->evicted = true;
  572. out:
  573. return ret;
  574. }
  575. bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
  576. const struct ttm_place *place)
  577. {
  578. /* Don't evict this BO if it's outside of the
  579. * requested placement range
  580. */
  581. if (place->fpfn >= (bo->mem.start + bo->mem.size) ||
  582. (place->lpfn && place->lpfn <= bo->mem.start))
  583. return false;
  584. return true;
  585. }
  586. EXPORT_SYMBOL(ttm_bo_eviction_valuable);
  587. static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
  588. uint32_t mem_type,
  589. const struct ttm_place *place,
  590. bool interruptible,
  591. bool no_wait_gpu)
  592. {
  593. struct ttm_bo_global *glob = bdev->glob;
  594. struct ttm_mem_type_manager *man = &bdev->man[mem_type];
  595. struct ttm_buffer_object *bo;
  596. int ret = -EBUSY;
  597. unsigned i;
  598. spin_lock(&glob->lru_lock);
  599. for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
  600. list_for_each_entry(bo, &man->lru[i], lru) {
  601. ret = __ttm_bo_reserve(bo, false, true, NULL);
  602. if (ret)
  603. continue;
  604. if (place && !bdev->driver->eviction_valuable(bo,
  605. place)) {
  606. __ttm_bo_unreserve(bo);
  607. ret = -EBUSY;
  608. continue;
  609. }
  610. break;
  611. }
  612. if (!ret)
  613. break;
  614. }
  615. if (ret) {
  616. spin_unlock(&glob->lru_lock);
  617. return ret;
  618. }
  619. kref_get(&bo->list_kref);
  620. if (!list_empty(&bo->ddestroy)) {
  621. ret = ttm_bo_cleanup_refs_and_unlock(bo, interruptible,
  622. no_wait_gpu);
  623. kref_put(&bo->list_kref, ttm_bo_release_list);
  624. return ret;
  625. }
  626. ttm_bo_del_from_lru(bo);
  627. spin_unlock(&glob->lru_lock);
  628. BUG_ON(ret != 0);
  629. ret = ttm_bo_evict(bo, interruptible, no_wait_gpu);
  630. ttm_bo_unreserve(bo);
  631. kref_put(&bo->list_kref, ttm_bo_release_list);
  632. return ret;
  633. }
  634. void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
  635. {
  636. struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
  637. if (mem->mm_node)
  638. (*man->func->put_node)(man, mem);
  639. }
  640. EXPORT_SYMBOL(ttm_bo_mem_put);
  641. /**
  642. * Add the last move fence to the BO and reserve a new shared slot.
  643. */
  644. static int ttm_bo_add_move_fence(struct ttm_buffer_object *bo,
  645. struct ttm_mem_type_manager *man,
  646. struct ttm_mem_reg *mem)
  647. {
  648. struct dma_fence *fence;
  649. int ret;
  650. spin_lock(&man->move_lock);
  651. fence = dma_fence_get(man->move);
  652. spin_unlock(&man->move_lock);
  653. if (fence) {
  654. reservation_object_add_shared_fence(bo->resv, fence);
  655. ret = reservation_object_reserve_shared(bo->resv);
  656. if (unlikely(ret))
  657. return ret;
  658. dma_fence_put(bo->moving);
  659. bo->moving = fence;
  660. }
  661. return 0;
  662. }
  663. /**
  664. * Repeatedly evict memory from the LRU for @mem_type until we create enough
  665. * space, or we've evicted everything and there isn't enough space.
  666. */
  667. static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
  668. uint32_t mem_type,
  669. const struct ttm_place *place,
  670. struct ttm_mem_reg *mem,
  671. bool interruptible,
  672. bool no_wait_gpu)
  673. {
  674. struct ttm_bo_device *bdev = bo->bdev;
  675. struct ttm_mem_type_manager *man = &bdev->man[mem_type];
  676. int ret;
  677. do {
  678. ret = (*man->func->get_node)(man, bo, place, mem);
  679. if (unlikely(ret != 0))
  680. return ret;
  681. if (mem->mm_node)
  682. break;
  683. ret = ttm_mem_evict_first(bdev, mem_type, place,
  684. interruptible, no_wait_gpu);
  685. if (unlikely(ret != 0))
  686. return ret;
  687. } while (1);
  688. mem->mem_type = mem_type;
  689. return ttm_bo_add_move_fence(bo, man, mem);
  690. }
  691. static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
  692. uint32_t cur_placement,
  693. uint32_t proposed_placement)
  694. {
  695. uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
  696. uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
  697. /**
  698. * Keep current caching if possible.
  699. */
  700. if ((cur_placement & caching) != 0)
  701. result |= (cur_placement & caching);
  702. else if ((man->default_caching & caching) != 0)
  703. result |= man->default_caching;
  704. else if ((TTM_PL_FLAG_CACHED & caching) != 0)
  705. result |= TTM_PL_FLAG_CACHED;
  706. else if ((TTM_PL_FLAG_WC & caching) != 0)
  707. result |= TTM_PL_FLAG_WC;
  708. else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
  709. result |= TTM_PL_FLAG_UNCACHED;
  710. return result;
  711. }
  712. static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
  713. uint32_t mem_type,
  714. const struct ttm_place *place,
  715. uint32_t *masked_placement)
  716. {
  717. uint32_t cur_flags = ttm_bo_type_flags(mem_type);
  718. if ((cur_flags & place->flags & TTM_PL_MASK_MEM) == 0)
  719. return false;
  720. if ((place->flags & man->available_caching) == 0)
  721. return false;
  722. cur_flags |= (place->flags & man->available_caching);
  723. *masked_placement = cur_flags;
  724. return true;
  725. }
  726. /**
  727. * Creates space for memory region @mem according to its type.
  728. *
  729. * This function first searches for free space in compatible memory types in
  730. * the priority order defined by the driver. If free space isn't found, then
  731. * ttm_bo_mem_force_space is attempted in priority order to evict and find
  732. * space.
  733. */
  734. int ttm_bo_mem_space(struct ttm_buffer_object *bo,
  735. struct ttm_placement *placement,
  736. struct ttm_mem_reg *mem,
  737. bool interruptible,
  738. bool no_wait_gpu)
  739. {
  740. struct ttm_bo_device *bdev = bo->bdev;
  741. struct ttm_mem_type_manager *man;
  742. uint32_t mem_type = TTM_PL_SYSTEM;
  743. uint32_t cur_flags = 0;
  744. bool type_found = false;
  745. bool type_ok = false;
  746. bool has_erestartsys = false;
  747. int i, ret;
  748. ret = reservation_object_reserve_shared(bo->resv);
  749. if (unlikely(ret))
  750. return ret;
  751. mem->mm_node = NULL;
  752. for (i = 0; i < placement->num_placement; ++i) {
  753. const struct ttm_place *place = &placement->placement[i];
  754. ret = ttm_mem_type_from_place(place, &mem_type);
  755. if (ret)
  756. return ret;
  757. man = &bdev->man[mem_type];
  758. if (!man->has_type || !man->use_type)
  759. continue;
  760. type_ok = ttm_bo_mt_compatible(man, mem_type, place,
  761. &cur_flags);
  762. if (!type_ok)
  763. continue;
  764. type_found = true;
  765. cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
  766. cur_flags);
  767. /*
  768. * Use the access and other non-mapping-related flag bits from
  769. * the memory placement flags to the current flags
  770. */
  771. ttm_flag_masked(&cur_flags, place->flags,
  772. ~TTM_PL_MASK_MEMTYPE);
  773. if (mem_type == TTM_PL_SYSTEM)
  774. break;
  775. ret = (*man->func->get_node)(man, bo, place, mem);
  776. if (unlikely(ret))
  777. return ret;
  778. if (mem->mm_node) {
  779. ret = ttm_bo_add_move_fence(bo, man, mem);
  780. if (unlikely(ret)) {
  781. (*man->func->put_node)(man, mem);
  782. return ret;
  783. }
  784. break;
  785. }
  786. }
  787. if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
  788. mem->mem_type = mem_type;
  789. mem->placement = cur_flags;
  790. return 0;
  791. }
  792. for (i = 0; i < placement->num_busy_placement; ++i) {
  793. const struct ttm_place *place = &placement->busy_placement[i];
  794. ret = ttm_mem_type_from_place(place, &mem_type);
  795. if (ret)
  796. return ret;
  797. man = &bdev->man[mem_type];
  798. if (!man->has_type || !man->use_type)
  799. continue;
  800. if (!ttm_bo_mt_compatible(man, mem_type, place, &cur_flags))
  801. continue;
  802. type_found = true;
  803. cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
  804. cur_flags);
  805. /*
  806. * Use the access and other non-mapping-related flag bits from
  807. * the memory placement flags to the current flags
  808. */
  809. ttm_flag_masked(&cur_flags, place->flags,
  810. ~TTM_PL_MASK_MEMTYPE);
  811. if (mem_type == TTM_PL_SYSTEM) {
  812. mem->mem_type = mem_type;
  813. mem->placement = cur_flags;
  814. mem->mm_node = NULL;
  815. return 0;
  816. }
  817. ret = ttm_bo_mem_force_space(bo, mem_type, place, mem,
  818. interruptible, no_wait_gpu);
  819. if (ret == 0 && mem->mm_node) {
  820. mem->placement = cur_flags;
  821. return 0;
  822. }
  823. if (ret == -ERESTARTSYS)
  824. has_erestartsys = true;
  825. }
  826. if (!type_found) {
  827. printk(KERN_ERR TTM_PFX "No compatible memory type found.\n");
  828. return -EINVAL;
  829. }
  830. return (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
  831. }
  832. EXPORT_SYMBOL(ttm_bo_mem_space);
  833. static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
  834. struct ttm_placement *placement,
  835. bool interruptible,
  836. bool no_wait_gpu)
  837. {
  838. int ret = 0;
  839. struct ttm_mem_reg mem;
  840. lockdep_assert_held(&bo->resv->lock.base);
  841. mem.num_pages = bo->num_pages;
  842. mem.size = mem.num_pages << PAGE_SHIFT;
  843. mem.page_alignment = bo->mem.page_alignment;
  844. mem.bus.io_reserved_vm = false;
  845. mem.bus.io_reserved_count = 0;
  846. /*
  847. * Determine where to move the buffer.
  848. */
  849. ret = ttm_bo_mem_space(bo, placement, &mem,
  850. interruptible, no_wait_gpu);
  851. if (ret)
  852. goto out_unlock;
  853. ret = ttm_bo_handle_move_mem(bo, &mem, false,
  854. interruptible, no_wait_gpu);
  855. out_unlock:
  856. if (ret && mem.mm_node)
  857. ttm_bo_mem_put(bo, &mem);
  858. return ret;
  859. }
  860. bool ttm_bo_mem_compat(struct ttm_placement *placement,
  861. struct ttm_mem_reg *mem,
  862. uint32_t *new_flags)
  863. {
  864. int i;
  865. for (i = 0; i < placement->num_placement; i++) {
  866. const struct ttm_place *heap = &placement->placement[i];
  867. if (mem->mm_node &&
  868. (mem->start < heap->fpfn ||
  869. (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
  870. continue;
  871. *new_flags = heap->flags;
  872. if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
  873. (*new_flags & mem->placement & TTM_PL_MASK_MEM))
  874. return true;
  875. }
  876. for (i = 0; i < placement->num_busy_placement; i++) {
  877. const struct ttm_place *heap = &placement->busy_placement[i];
  878. if (mem->mm_node &&
  879. (mem->start < heap->fpfn ||
  880. (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
  881. continue;
  882. *new_flags = heap->flags;
  883. if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
  884. (*new_flags & mem->placement & TTM_PL_MASK_MEM))
  885. return true;
  886. }
  887. return false;
  888. }
  889. EXPORT_SYMBOL(ttm_bo_mem_compat);
  890. int ttm_bo_validate(struct ttm_buffer_object *bo,
  891. struct ttm_placement *placement,
  892. bool interruptible,
  893. bool no_wait_gpu)
  894. {
  895. int ret;
  896. uint32_t new_flags;
  897. lockdep_assert_held(&bo->resv->lock.base);
  898. /*
  899. * Check whether we need to move buffer.
  900. */
  901. if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) {
  902. ret = ttm_bo_move_buffer(bo, placement, interruptible,
  903. no_wait_gpu);
  904. if (ret)
  905. return ret;
  906. } else {
  907. /*
  908. * Use the access and other non-mapping-related flag bits from
  909. * the compatible memory placement flags to the active flags
  910. */
  911. ttm_flag_masked(&bo->mem.placement, new_flags,
  912. ~TTM_PL_MASK_MEMTYPE);
  913. }
  914. /*
  915. * We might need to add a TTM.
  916. */
  917. if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
  918. ret = ttm_bo_add_ttm(bo, true);
  919. if (ret)
  920. return ret;
  921. }
  922. return 0;
  923. }
  924. EXPORT_SYMBOL(ttm_bo_validate);
  925. int ttm_bo_init(struct ttm_bo_device *bdev,
  926. struct ttm_buffer_object *bo,
  927. unsigned long size,
  928. enum ttm_bo_type type,
  929. struct ttm_placement *placement,
  930. uint32_t page_alignment,
  931. bool interruptible,
  932. struct file *persistent_swap_storage,
  933. size_t acc_size,
  934. struct sg_table *sg,
  935. struct reservation_object *resv,
  936. void (*destroy) (struct ttm_buffer_object *))
  937. {
  938. int ret = 0;
  939. unsigned long num_pages;
  940. struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
  941. bool locked;
  942. ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
  943. if (ret) {
  944. pr_err("Out of kernel memory\n");
  945. if (destroy)
  946. (*destroy)(bo);
  947. else
  948. kfree(bo);
  949. return -ENOMEM;
  950. }
  951. num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  952. if (num_pages == 0) {
  953. pr_err("Illegal buffer object size\n");
  954. if (destroy)
  955. (*destroy)(bo);
  956. else
  957. kfree(bo);
  958. ttm_mem_global_free(mem_glob, acc_size);
  959. return -EINVAL;
  960. }
  961. bo->destroy = destroy;
  962. kref_init(&bo->kref);
  963. kref_init(&bo->list_kref);
  964. atomic_set(&bo->cpu_writers, 0);
  965. INIT_LIST_HEAD(&bo->lru);
  966. INIT_LIST_HEAD(&bo->ddestroy);
  967. INIT_LIST_HEAD(&bo->swap);
  968. INIT_LIST_HEAD(&bo->io_reserve_lru);
  969. mutex_init(&bo->wu_mutex);
  970. bo->bdev = bdev;
  971. bo->glob = bdev->glob;
  972. bo->type = type;
  973. bo->num_pages = num_pages;
  974. bo->mem.size = num_pages << PAGE_SHIFT;
  975. bo->mem.mem_type = TTM_PL_SYSTEM;
  976. bo->mem.num_pages = bo->num_pages;
  977. bo->mem.mm_node = NULL;
  978. bo->mem.page_alignment = page_alignment;
  979. bo->mem.bus.io_reserved_vm = false;
  980. bo->mem.bus.io_reserved_count = 0;
  981. bo->moving = NULL;
  982. bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
  983. bo->persistent_swap_storage = persistent_swap_storage;
  984. bo->acc_size = acc_size;
  985. bo->sg = sg;
  986. if (resv) {
  987. bo->resv = resv;
  988. lockdep_assert_held(&bo->resv->lock.base);
  989. } else {
  990. bo->resv = &bo->ttm_resv;
  991. reservation_object_init(&bo->ttm_resv);
  992. }
  993. atomic_inc(&bo->glob->bo_count);
  994. drm_vma_node_reset(&bo->vma_node);
  995. bo->priority = 0;
  996. /*
  997. * For ttm_bo_type_device buffers, allocate
  998. * address space from the device.
  999. */
  1000. if (bo->type == ttm_bo_type_device ||
  1001. bo->type == ttm_bo_type_sg)
  1002. ret = drm_vma_offset_add(&bdev->vma_manager, &bo->vma_node,
  1003. bo->mem.num_pages);
  1004. /* passed reservation objects should already be locked,
  1005. * since otherwise lockdep will be angered in radeon.
  1006. */
  1007. if (!resv) {
  1008. locked = ww_mutex_trylock(&bo->resv->lock);
  1009. WARN_ON(!locked);
  1010. }
  1011. if (likely(!ret))
  1012. ret = ttm_bo_validate(bo, placement, interruptible, false);
  1013. if (!resv) {
  1014. ttm_bo_unreserve(bo);
  1015. } else if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
  1016. spin_lock(&bo->glob->lru_lock);
  1017. ttm_bo_add_to_lru(bo);
  1018. spin_unlock(&bo->glob->lru_lock);
  1019. }
  1020. if (unlikely(ret))
  1021. ttm_bo_unref(&bo);
  1022. return ret;
  1023. }
  1024. EXPORT_SYMBOL(ttm_bo_init);
  1025. size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
  1026. unsigned long bo_size,
  1027. unsigned struct_size)
  1028. {
  1029. unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
  1030. size_t size = 0;
  1031. size += ttm_round_pot(struct_size);
  1032. size += ttm_round_pot(npages * sizeof(void *));
  1033. size += ttm_round_pot(sizeof(struct ttm_tt));
  1034. return size;
  1035. }
  1036. EXPORT_SYMBOL(ttm_bo_acc_size);
  1037. size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
  1038. unsigned long bo_size,
  1039. unsigned struct_size)
  1040. {
  1041. unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
  1042. size_t size = 0;
  1043. size += ttm_round_pot(struct_size);
  1044. size += ttm_round_pot(npages * (2*sizeof(void *) + sizeof(dma_addr_t)));
  1045. size += ttm_round_pot(sizeof(struct ttm_dma_tt));
  1046. return size;
  1047. }
  1048. EXPORT_SYMBOL(ttm_bo_dma_acc_size);
  1049. int ttm_bo_create(struct ttm_bo_device *bdev,
  1050. unsigned long size,
  1051. enum ttm_bo_type type,
  1052. struct ttm_placement *placement,
  1053. uint32_t page_alignment,
  1054. bool interruptible,
  1055. struct file *persistent_swap_storage,
  1056. struct ttm_buffer_object **p_bo)
  1057. {
  1058. struct ttm_buffer_object *bo;
  1059. size_t acc_size;
  1060. int ret;
  1061. bo = kzalloc(sizeof(*bo), GFP_KERNEL);
  1062. if (unlikely(bo == NULL))
  1063. return -ENOMEM;
  1064. acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
  1065. ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
  1066. interruptible, persistent_swap_storage, acc_size,
  1067. NULL, NULL, NULL);
  1068. if (likely(ret == 0))
  1069. *p_bo = bo;
  1070. return ret;
  1071. }
  1072. EXPORT_SYMBOL(ttm_bo_create);
  1073. static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
  1074. unsigned mem_type)
  1075. {
  1076. struct ttm_mem_type_manager *man = &bdev->man[mem_type];
  1077. struct ttm_bo_global *glob = bdev->glob;
  1078. struct dma_fence *fence;
  1079. int ret;
  1080. unsigned i;
  1081. /*
  1082. * Can't use standard list traversal since we're unlocking.
  1083. */
  1084. spin_lock(&glob->lru_lock);
  1085. for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
  1086. while (!list_empty(&man->lru[i])) {
  1087. spin_unlock(&glob->lru_lock);
  1088. ret = ttm_mem_evict_first(bdev, mem_type, NULL, false, false);
  1089. if (ret)
  1090. return ret;
  1091. spin_lock(&glob->lru_lock);
  1092. }
  1093. }
  1094. spin_unlock(&glob->lru_lock);
  1095. spin_lock(&man->move_lock);
  1096. fence = dma_fence_get(man->move);
  1097. spin_unlock(&man->move_lock);
  1098. if (fence) {
  1099. ret = dma_fence_wait(fence, false);
  1100. dma_fence_put(fence);
  1101. if (ret)
  1102. return ret;
  1103. }
  1104. return 0;
  1105. }
  1106. int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
  1107. {
  1108. struct ttm_mem_type_manager *man;
  1109. int ret = -EINVAL;
  1110. if (mem_type >= TTM_NUM_MEM_TYPES) {
  1111. pr_err("Illegal memory type %d\n", mem_type);
  1112. return ret;
  1113. }
  1114. man = &bdev->man[mem_type];
  1115. if (!man->has_type) {
  1116. pr_err("Trying to take down uninitialized memory manager type %u\n",
  1117. mem_type);
  1118. return ret;
  1119. }
  1120. dma_fence_put(man->move);
  1121. man->use_type = false;
  1122. man->has_type = false;
  1123. ret = 0;
  1124. if (mem_type > 0) {
  1125. ret = ttm_bo_force_list_clean(bdev, mem_type);
  1126. if (ret) {
  1127. pr_err("Cleanup eviction failed\n");
  1128. return ret;
  1129. }
  1130. ret = (*man->func->takedown)(man);
  1131. }
  1132. return ret;
  1133. }
  1134. EXPORT_SYMBOL(ttm_bo_clean_mm);
  1135. int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
  1136. {
  1137. struct ttm_mem_type_manager *man = &bdev->man[mem_type];
  1138. if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
  1139. pr_err("Illegal memory manager memory type %u\n", mem_type);
  1140. return -EINVAL;
  1141. }
  1142. if (!man->has_type) {
  1143. pr_err("Memory type %u has not been initialized\n", mem_type);
  1144. return 0;
  1145. }
  1146. return ttm_bo_force_list_clean(bdev, mem_type);
  1147. }
  1148. EXPORT_SYMBOL(ttm_bo_evict_mm);
  1149. int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
  1150. unsigned long p_size)
  1151. {
  1152. int ret = -EINVAL;
  1153. struct ttm_mem_type_manager *man;
  1154. unsigned i;
  1155. BUG_ON(type >= TTM_NUM_MEM_TYPES);
  1156. man = &bdev->man[type];
  1157. BUG_ON(man->has_type);
  1158. man->io_reserve_fastpath = true;
  1159. man->use_io_reserve_lru = false;
  1160. mutex_init(&man->io_reserve_mutex);
  1161. spin_lock_init(&man->move_lock);
  1162. INIT_LIST_HEAD(&man->io_reserve_lru);
  1163. ret = bdev->driver->init_mem_type(bdev, type, man);
  1164. if (ret)
  1165. return ret;
  1166. man->bdev = bdev;
  1167. ret = 0;
  1168. if (type != TTM_PL_SYSTEM) {
  1169. ret = (*man->func->init)(man, p_size);
  1170. if (ret)
  1171. return ret;
  1172. }
  1173. man->has_type = true;
  1174. man->use_type = true;
  1175. man->size = p_size;
  1176. for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
  1177. INIT_LIST_HEAD(&man->lru[i]);
  1178. man->move = NULL;
  1179. return 0;
  1180. }
  1181. EXPORT_SYMBOL(ttm_bo_init_mm);
  1182. static void ttm_bo_global_kobj_release(struct kobject *kobj)
  1183. {
  1184. struct ttm_bo_global *glob =
  1185. container_of(kobj, struct ttm_bo_global, kobj);
  1186. ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
  1187. __free_page(glob->dummy_read_page);
  1188. kfree(glob);
  1189. }
  1190. void ttm_bo_global_release(struct drm_global_reference *ref)
  1191. {
  1192. struct ttm_bo_global *glob = ref->object;
  1193. kobject_del(&glob->kobj);
  1194. kobject_put(&glob->kobj);
  1195. }
  1196. EXPORT_SYMBOL(ttm_bo_global_release);
  1197. int ttm_bo_global_init(struct drm_global_reference *ref)
  1198. {
  1199. struct ttm_bo_global_ref *bo_ref =
  1200. container_of(ref, struct ttm_bo_global_ref, ref);
  1201. struct ttm_bo_global *glob = ref->object;
  1202. int ret;
  1203. unsigned i;
  1204. mutex_init(&glob->device_list_mutex);
  1205. spin_lock_init(&glob->lru_lock);
  1206. glob->mem_glob = bo_ref->mem_glob;
  1207. glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
  1208. if (unlikely(glob->dummy_read_page == NULL)) {
  1209. ret = -ENOMEM;
  1210. goto out_no_drp;
  1211. }
  1212. for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
  1213. INIT_LIST_HEAD(&glob->swap_lru[i]);
  1214. INIT_LIST_HEAD(&glob->device_list);
  1215. ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
  1216. ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
  1217. if (unlikely(ret != 0)) {
  1218. pr_err("Could not register buffer object swapout\n");
  1219. goto out_no_shrink;
  1220. }
  1221. atomic_set(&glob->bo_count, 0);
  1222. ret = kobject_init_and_add(
  1223. &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
  1224. if (unlikely(ret != 0))
  1225. kobject_put(&glob->kobj);
  1226. return ret;
  1227. out_no_shrink:
  1228. __free_page(glob->dummy_read_page);
  1229. out_no_drp:
  1230. kfree(glob);
  1231. return ret;
  1232. }
  1233. EXPORT_SYMBOL(ttm_bo_global_init);
  1234. int ttm_bo_device_release(struct ttm_bo_device *bdev)
  1235. {
  1236. int ret = 0;
  1237. unsigned i = TTM_NUM_MEM_TYPES;
  1238. struct ttm_mem_type_manager *man;
  1239. struct ttm_bo_global *glob = bdev->glob;
  1240. while (i--) {
  1241. man = &bdev->man[i];
  1242. if (man->has_type) {
  1243. man->use_type = false;
  1244. if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
  1245. ret = -EBUSY;
  1246. pr_err("DRM memory manager type %d is not clean\n",
  1247. i);
  1248. }
  1249. man->has_type = false;
  1250. }
  1251. }
  1252. mutex_lock(&glob->device_list_mutex);
  1253. list_del(&bdev->device_list);
  1254. mutex_unlock(&glob->device_list_mutex);
  1255. cancel_delayed_work_sync(&bdev->wq);
  1256. while (ttm_bo_delayed_delete(bdev, true))
  1257. ;
  1258. spin_lock(&glob->lru_lock);
  1259. if (list_empty(&bdev->ddestroy))
  1260. TTM_DEBUG("Delayed destroy list was clean\n");
  1261. for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
  1262. if (list_empty(&bdev->man[0].lru[0]))
  1263. TTM_DEBUG("Swap list %d was clean\n", i);
  1264. spin_unlock(&glob->lru_lock);
  1265. drm_vma_offset_manager_destroy(&bdev->vma_manager);
  1266. return ret;
  1267. }
  1268. EXPORT_SYMBOL(ttm_bo_device_release);
  1269. int ttm_bo_device_init(struct ttm_bo_device *bdev,
  1270. struct ttm_bo_global *glob,
  1271. struct ttm_bo_driver *driver,
  1272. struct address_space *mapping,
  1273. uint64_t file_page_offset,
  1274. bool need_dma32)
  1275. {
  1276. int ret = -EINVAL;
  1277. bdev->driver = driver;
  1278. memset(bdev->man, 0, sizeof(bdev->man));
  1279. /*
  1280. * Initialize the system memory buffer type.
  1281. * Other types need to be driver / IOCTL initialized.
  1282. */
  1283. ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
  1284. if (unlikely(ret != 0))
  1285. goto out_no_sys;
  1286. drm_vma_offset_manager_init(&bdev->vma_manager, file_page_offset,
  1287. 0x10000000);
  1288. INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
  1289. INIT_LIST_HEAD(&bdev->ddestroy);
  1290. bdev->dev_mapping = mapping;
  1291. bdev->glob = glob;
  1292. bdev->need_dma32 = need_dma32;
  1293. mutex_lock(&glob->device_list_mutex);
  1294. list_add_tail(&bdev->device_list, &glob->device_list);
  1295. mutex_unlock(&glob->device_list_mutex);
  1296. return 0;
  1297. out_no_sys:
  1298. return ret;
  1299. }
  1300. EXPORT_SYMBOL(ttm_bo_device_init);
  1301. /*
  1302. * buffer object vm functions.
  1303. */
  1304. bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
  1305. {
  1306. struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
  1307. if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
  1308. if (mem->mem_type == TTM_PL_SYSTEM)
  1309. return false;
  1310. if (man->flags & TTM_MEMTYPE_FLAG_CMA)
  1311. return false;
  1312. if (mem->placement & TTM_PL_FLAG_CACHED)
  1313. return false;
  1314. }
  1315. return true;
  1316. }
  1317. void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
  1318. {
  1319. struct ttm_bo_device *bdev = bo->bdev;
  1320. drm_vma_node_unmap(&bo->vma_node, bdev->dev_mapping);
  1321. ttm_mem_io_free_vm(bo);
  1322. }
  1323. void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
  1324. {
  1325. struct ttm_bo_device *bdev = bo->bdev;
  1326. struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
  1327. ttm_mem_io_lock(man, false);
  1328. ttm_bo_unmap_virtual_locked(bo);
  1329. ttm_mem_io_unlock(man);
  1330. }
  1331. EXPORT_SYMBOL(ttm_bo_unmap_virtual);
  1332. int ttm_bo_wait(struct ttm_buffer_object *bo,
  1333. bool interruptible, bool no_wait)
  1334. {
  1335. long timeout = 15 * HZ;
  1336. if (no_wait) {
  1337. if (reservation_object_test_signaled_rcu(bo->resv, true))
  1338. return 0;
  1339. else
  1340. return -EBUSY;
  1341. }
  1342. timeout = reservation_object_wait_timeout_rcu(bo->resv, true,
  1343. interruptible, timeout);
  1344. if (timeout < 0)
  1345. return timeout;
  1346. if (timeout == 0)
  1347. return -EBUSY;
  1348. reservation_object_add_excl_fence(bo->resv, NULL);
  1349. return 0;
  1350. }
  1351. EXPORT_SYMBOL(ttm_bo_wait);
  1352. int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
  1353. {
  1354. int ret = 0;
  1355. /*
  1356. * Using ttm_bo_reserve makes sure the lru lists are updated.
  1357. */
  1358. ret = ttm_bo_reserve(bo, true, no_wait, NULL);
  1359. if (unlikely(ret != 0))
  1360. return ret;
  1361. ret = ttm_bo_wait(bo, true, no_wait);
  1362. if (likely(ret == 0))
  1363. atomic_inc(&bo->cpu_writers);
  1364. ttm_bo_unreserve(bo);
  1365. return ret;
  1366. }
  1367. EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
  1368. void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
  1369. {
  1370. atomic_dec(&bo->cpu_writers);
  1371. }
  1372. EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
  1373. /**
  1374. * A buffer object shrink method that tries to swap out the first
  1375. * buffer object on the bo_global::swap_lru list.
  1376. */
  1377. static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
  1378. {
  1379. struct ttm_bo_global *glob =
  1380. container_of(shrink, struct ttm_bo_global, shrink);
  1381. struct ttm_buffer_object *bo;
  1382. int ret = -EBUSY;
  1383. unsigned i;
  1384. spin_lock(&glob->lru_lock);
  1385. for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
  1386. list_for_each_entry(bo, &glob->swap_lru[i], swap) {
  1387. ret = __ttm_bo_reserve(bo, false, true, NULL);
  1388. if (!ret)
  1389. break;
  1390. }
  1391. if (!ret)
  1392. break;
  1393. }
  1394. if (ret) {
  1395. spin_unlock(&glob->lru_lock);
  1396. return ret;
  1397. }
  1398. kref_get(&bo->list_kref);
  1399. if (!list_empty(&bo->ddestroy)) {
  1400. ret = ttm_bo_cleanup_refs_and_unlock(bo, false, false);
  1401. kref_put(&bo->list_kref, ttm_bo_release_list);
  1402. return ret;
  1403. }
  1404. ttm_bo_del_from_lru(bo);
  1405. spin_unlock(&glob->lru_lock);
  1406. /**
  1407. * Move to system cached
  1408. */
  1409. if (bo->mem.mem_type != TTM_PL_SYSTEM ||
  1410. bo->ttm->caching_state != tt_cached) {
  1411. struct ttm_mem_reg evict_mem;
  1412. evict_mem = bo->mem;
  1413. evict_mem.mm_node = NULL;
  1414. evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
  1415. evict_mem.mem_type = TTM_PL_SYSTEM;
  1416. ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
  1417. false, false);
  1418. if (unlikely(ret != 0))
  1419. goto out;
  1420. }
  1421. /**
  1422. * Make sure BO is idle.
  1423. */
  1424. ret = ttm_bo_wait(bo, false, false);
  1425. if (unlikely(ret != 0))
  1426. goto out;
  1427. ttm_bo_unmap_virtual(bo);
  1428. /**
  1429. * Swap out. Buffer will be swapped in again as soon as
  1430. * anyone tries to access a ttm page.
  1431. */
  1432. if (bo->bdev->driver->swap_notify)
  1433. bo->bdev->driver->swap_notify(bo);
  1434. ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
  1435. out:
  1436. /**
  1437. *
  1438. * Unreserve without putting on LRU to avoid swapping out an
  1439. * already swapped buffer.
  1440. */
  1441. __ttm_bo_unreserve(bo);
  1442. kref_put(&bo->list_kref, ttm_bo_release_list);
  1443. return ret;
  1444. }
  1445. void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
  1446. {
  1447. while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
  1448. ;
  1449. }
  1450. EXPORT_SYMBOL(ttm_bo_swapout_all);
  1451. /**
  1452. * ttm_bo_wait_unreserved - interruptible wait for a buffer object to become
  1453. * unreserved
  1454. *
  1455. * @bo: Pointer to buffer
  1456. */
  1457. int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo)
  1458. {
  1459. int ret;
  1460. /*
  1461. * In the absense of a wait_unlocked API,
  1462. * Use the bo::wu_mutex to avoid triggering livelocks due to
  1463. * concurrent use of this function. Note that this use of
  1464. * bo::wu_mutex can go away if we change locking order to
  1465. * mmap_sem -> bo::reserve.
  1466. */
  1467. ret = mutex_lock_interruptible(&bo->wu_mutex);
  1468. if (unlikely(ret != 0))
  1469. return -ERESTARTSYS;
  1470. if (!ww_mutex_is_locked(&bo->resv->lock))
  1471. goto out_unlock;
  1472. ret = __ttm_bo_reserve(bo, true, false, NULL);
  1473. if (unlikely(ret != 0))
  1474. goto out_unlock;
  1475. __ttm_bo_unreserve(bo);
  1476. out_unlock:
  1477. mutex_unlock(&bo->wu_mutex);
  1478. return ret;
  1479. }