ttm_bo.c 41 KB

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