ttm_bo.c 42 KB

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