ttm_bo.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704
  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_setup_vm(struct ttm_buffer_object *bo);
  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_flags(uint32_t flags, uint32_t *mem_type)
  52. {
  53. int i;
  54. for (i = 0; i <= TTM_PL_PRIV5; i++)
  55. if (flags & (1 << i)) {
  56. *mem_type = i;
  57. return 0;
  58. }
  59. return -EINVAL;
  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%08lX\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_flags(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], 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->sync_obj != NULL);
  126. BUG_ON(bo->mem.mm_node != NULL);
  127. BUG_ON(!list_empty(&bo->lru));
  128. BUG_ON(!list_empty(&bo->ddestroy));
  129. if (bo->ttm)
  130. ttm_tt_destroy(bo->ttm);
  131. atomic_dec(&bo->glob->bo_count);
  132. if (bo->resv == &bo->ttm_resv)
  133. reservation_object_fini(&bo->ttm_resv);
  134. if (bo->destroy)
  135. bo->destroy(bo);
  136. else {
  137. kfree(bo);
  138. }
  139. ttm_mem_global_free(bdev->glob->mem_glob, acc_size);
  140. }
  141. void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
  142. {
  143. struct ttm_bo_device *bdev = bo->bdev;
  144. struct ttm_mem_type_manager *man;
  145. lockdep_assert_held(&bo->resv->lock.base);
  146. if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
  147. BUG_ON(!list_empty(&bo->lru));
  148. man = &bdev->man[bo->mem.mem_type];
  149. list_add_tail(&bo->lru, &man->lru);
  150. kref_get(&bo->list_kref);
  151. if (bo->ttm != NULL) {
  152. list_add_tail(&bo->swap, &bo->glob->swap_lru);
  153. kref_get(&bo->list_kref);
  154. }
  155. }
  156. }
  157. EXPORT_SYMBOL(ttm_bo_add_to_lru);
  158. int ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
  159. {
  160. int put_count = 0;
  161. if (!list_empty(&bo->swap)) {
  162. list_del_init(&bo->swap);
  163. ++put_count;
  164. }
  165. if (!list_empty(&bo->lru)) {
  166. list_del_init(&bo->lru);
  167. ++put_count;
  168. }
  169. /*
  170. * TODO: Add a driver hook to delete from
  171. * driver-specific LRU's here.
  172. */
  173. return put_count;
  174. }
  175. static void ttm_bo_ref_bug(struct kref *list_kref)
  176. {
  177. BUG();
  178. }
  179. void ttm_bo_list_ref_sub(struct ttm_buffer_object *bo, int count,
  180. bool never_free)
  181. {
  182. kref_sub(&bo->list_kref, count,
  183. (never_free) ? ttm_bo_ref_bug : ttm_bo_release_list);
  184. }
  185. void ttm_bo_del_sub_from_lru(struct ttm_buffer_object *bo)
  186. {
  187. int put_count;
  188. spin_lock(&bo->glob->lru_lock);
  189. put_count = ttm_bo_del_from_lru(bo);
  190. spin_unlock(&bo->glob->lru_lock);
  191. ttm_bo_list_ref_sub(bo, put_count, true);
  192. }
  193. EXPORT_SYMBOL(ttm_bo_del_sub_from_lru);
  194. /*
  195. * Call bo->mutex locked.
  196. */
  197. static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
  198. {
  199. struct ttm_bo_device *bdev = bo->bdev;
  200. struct ttm_bo_global *glob = bo->glob;
  201. int ret = 0;
  202. uint32_t page_flags = 0;
  203. TTM_ASSERT_LOCKED(&bo->mutex);
  204. bo->ttm = NULL;
  205. if (bdev->need_dma32)
  206. page_flags |= TTM_PAGE_FLAG_DMA32;
  207. switch (bo->type) {
  208. case ttm_bo_type_device:
  209. if (zero_alloc)
  210. page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
  211. case ttm_bo_type_kernel:
  212. bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
  213. page_flags, glob->dummy_read_page);
  214. if (unlikely(bo->ttm == NULL))
  215. ret = -ENOMEM;
  216. break;
  217. case ttm_bo_type_sg:
  218. bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
  219. page_flags | TTM_PAGE_FLAG_SG,
  220. glob->dummy_read_page);
  221. if (unlikely(bo->ttm == NULL)) {
  222. ret = -ENOMEM;
  223. break;
  224. }
  225. bo->ttm->sg = bo->sg;
  226. break;
  227. default:
  228. pr_err("Illegal buffer object type\n");
  229. ret = -EINVAL;
  230. break;
  231. }
  232. return ret;
  233. }
  234. static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
  235. struct ttm_mem_reg *mem,
  236. bool evict, bool interruptible,
  237. bool no_wait_gpu)
  238. {
  239. struct ttm_bo_device *bdev = bo->bdev;
  240. bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
  241. bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
  242. struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
  243. struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
  244. int ret = 0;
  245. if (old_is_pci || new_is_pci ||
  246. ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) {
  247. ret = ttm_mem_io_lock(old_man, true);
  248. if (unlikely(ret != 0))
  249. goto out_err;
  250. ttm_bo_unmap_virtual_locked(bo);
  251. ttm_mem_io_unlock(old_man);
  252. }
  253. /*
  254. * Create and bind a ttm if required.
  255. */
  256. if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
  257. if (bo->ttm == NULL) {
  258. bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED);
  259. ret = ttm_bo_add_ttm(bo, zero);
  260. if (ret)
  261. goto out_err;
  262. }
  263. ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
  264. if (ret)
  265. goto out_err;
  266. if (mem->mem_type != TTM_PL_SYSTEM) {
  267. ret = ttm_tt_bind(bo->ttm, mem);
  268. if (ret)
  269. goto out_err;
  270. }
  271. if (bo->mem.mem_type == TTM_PL_SYSTEM) {
  272. if (bdev->driver->move_notify)
  273. bdev->driver->move_notify(bo, mem);
  274. bo->mem = *mem;
  275. mem->mm_node = NULL;
  276. goto moved;
  277. }
  278. }
  279. if (bdev->driver->move_notify)
  280. bdev->driver->move_notify(bo, mem);
  281. if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
  282. !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
  283. ret = ttm_bo_move_ttm(bo, evict, no_wait_gpu, mem);
  284. else if (bdev->driver->move)
  285. ret = bdev->driver->move(bo, evict, interruptible,
  286. no_wait_gpu, mem);
  287. else
  288. ret = ttm_bo_move_memcpy(bo, evict, no_wait_gpu, mem);
  289. if (ret) {
  290. if (bdev->driver->move_notify) {
  291. struct ttm_mem_reg tmp_mem = *mem;
  292. *mem = bo->mem;
  293. bo->mem = tmp_mem;
  294. bdev->driver->move_notify(bo, mem);
  295. bo->mem = *mem;
  296. *mem = tmp_mem;
  297. }
  298. goto out_err;
  299. }
  300. moved:
  301. if (bo->evicted) {
  302. ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
  303. if (ret)
  304. pr_err("Can not flush read caches\n");
  305. bo->evicted = false;
  306. }
  307. if (bo->mem.mm_node) {
  308. bo->offset = (bo->mem.start << PAGE_SHIFT) +
  309. bdev->man[bo->mem.mem_type].gpu_offset;
  310. bo->cur_placement = bo->mem.placement;
  311. } else
  312. bo->offset = 0;
  313. return 0;
  314. out_err:
  315. new_man = &bdev->man[bo->mem.mem_type];
  316. if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
  317. ttm_tt_unbind(bo->ttm);
  318. ttm_tt_destroy(bo->ttm);
  319. bo->ttm = NULL;
  320. }
  321. return ret;
  322. }
  323. /**
  324. * Call bo::reserved.
  325. * Will release GPU memory type usage on destruction.
  326. * This is the place to put in driver specific hooks to release
  327. * driver private resources.
  328. * Will release the bo::reserved lock.
  329. */
  330. static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
  331. {
  332. if (bo->bdev->driver->move_notify)
  333. bo->bdev->driver->move_notify(bo, NULL);
  334. if (bo->ttm) {
  335. ttm_tt_unbind(bo->ttm);
  336. ttm_tt_destroy(bo->ttm);
  337. bo->ttm = NULL;
  338. }
  339. ttm_bo_mem_put(bo, &bo->mem);
  340. ww_mutex_unlock (&bo->resv->lock);
  341. }
  342. static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
  343. {
  344. struct ttm_bo_device *bdev = bo->bdev;
  345. struct ttm_bo_global *glob = bo->glob;
  346. struct ttm_bo_driver *driver = bdev->driver;
  347. void *sync_obj = NULL;
  348. int put_count;
  349. int ret;
  350. spin_lock(&glob->lru_lock);
  351. ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
  352. spin_lock(&bdev->fence_lock);
  353. (void) ttm_bo_wait(bo, false, false, true);
  354. if (!ret && !bo->sync_obj) {
  355. spin_unlock(&bdev->fence_lock);
  356. put_count = ttm_bo_del_from_lru(bo);
  357. spin_unlock(&glob->lru_lock);
  358. ttm_bo_cleanup_memtype_use(bo);
  359. ttm_bo_list_ref_sub(bo, put_count, true);
  360. return;
  361. }
  362. if (bo->sync_obj)
  363. sync_obj = driver->sync_obj_ref(bo->sync_obj);
  364. spin_unlock(&bdev->fence_lock);
  365. if (!ret)
  366. ww_mutex_unlock(&bo->resv->lock);
  367. kref_get(&bo->list_kref);
  368. list_add_tail(&bo->ddestroy, &bdev->ddestroy);
  369. spin_unlock(&glob->lru_lock);
  370. if (sync_obj) {
  371. driver->sync_obj_flush(sync_obj);
  372. driver->sync_obj_unref(&sync_obj);
  373. }
  374. schedule_delayed_work(&bdev->wq,
  375. ((HZ / 100) < 1) ? 1 : HZ / 100);
  376. }
  377. /**
  378. * function ttm_bo_cleanup_refs_and_unlock
  379. * If bo idle, remove from delayed- and lru lists, and unref.
  380. * If not idle, do nothing.
  381. *
  382. * Must be called with lru_lock and reservation held, this function
  383. * will drop both before returning.
  384. *
  385. * @interruptible Any sleeps should occur interruptibly.
  386. * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
  387. */
  388. static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo,
  389. bool interruptible,
  390. bool no_wait_gpu)
  391. {
  392. struct ttm_bo_device *bdev = bo->bdev;
  393. struct ttm_bo_driver *driver = bdev->driver;
  394. struct ttm_bo_global *glob = bo->glob;
  395. int put_count;
  396. int ret;
  397. spin_lock(&bdev->fence_lock);
  398. ret = ttm_bo_wait(bo, false, false, true);
  399. if (ret && !no_wait_gpu) {
  400. void *sync_obj;
  401. /*
  402. * Take a reference to the fence and unreserve,
  403. * at this point the buffer should be dead, so
  404. * no new sync objects can be attached.
  405. */
  406. sync_obj = driver->sync_obj_ref(bo->sync_obj);
  407. spin_unlock(&bdev->fence_lock);
  408. ww_mutex_unlock(&bo->resv->lock);
  409. spin_unlock(&glob->lru_lock);
  410. ret = driver->sync_obj_wait(sync_obj, false, interruptible);
  411. driver->sync_obj_unref(&sync_obj);
  412. if (ret)
  413. return ret;
  414. /*
  415. * remove sync_obj with ttm_bo_wait, the wait should be
  416. * finished, and no new wait object should have been added.
  417. */
  418. spin_lock(&bdev->fence_lock);
  419. ret = ttm_bo_wait(bo, false, false, true);
  420. WARN_ON(ret);
  421. spin_unlock(&bdev->fence_lock);
  422. if (ret)
  423. return ret;
  424. spin_lock(&glob->lru_lock);
  425. ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
  426. /*
  427. * We raced, and lost, someone else holds the reservation now,
  428. * and is probably busy in ttm_bo_cleanup_memtype_use.
  429. *
  430. * Even if it's not the case, because we finished waiting any
  431. * delayed destruction would succeed, so just return success
  432. * here.
  433. */
  434. if (ret) {
  435. spin_unlock(&glob->lru_lock);
  436. return 0;
  437. }
  438. } else
  439. spin_unlock(&bdev->fence_lock);
  440. if (ret || unlikely(list_empty(&bo->ddestroy))) {
  441. ww_mutex_unlock(&bo->resv->lock);
  442. spin_unlock(&glob->lru_lock);
  443. return ret;
  444. }
  445. put_count = ttm_bo_del_from_lru(bo);
  446. list_del_init(&bo->ddestroy);
  447. ++put_count;
  448. spin_unlock(&glob->lru_lock);
  449. ttm_bo_cleanup_memtype_use(bo);
  450. ttm_bo_list_ref_sub(bo, put_count, true);
  451. return 0;
  452. }
  453. /**
  454. * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
  455. * encountered buffers.
  456. */
  457. static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
  458. {
  459. struct ttm_bo_global *glob = bdev->glob;
  460. struct ttm_buffer_object *entry = NULL;
  461. int ret = 0;
  462. spin_lock(&glob->lru_lock);
  463. if (list_empty(&bdev->ddestroy))
  464. goto out_unlock;
  465. entry = list_first_entry(&bdev->ddestroy,
  466. struct ttm_buffer_object, ddestroy);
  467. kref_get(&entry->list_kref);
  468. for (;;) {
  469. struct ttm_buffer_object *nentry = NULL;
  470. if (entry->ddestroy.next != &bdev->ddestroy) {
  471. nentry = list_first_entry(&entry->ddestroy,
  472. struct ttm_buffer_object, ddestroy);
  473. kref_get(&nentry->list_kref);
  474. }
  475. ret = ttm_bo_reserve_nolru(entry, false, true, false, 0);
  476. if (remove_all && ret) {
  477. spin_unlock(&glob->lru_lock);
  478. ret = ttm_bo_reserve_nolru(entry, false, false,
  479. false, 0);
  480. spin_lock(&glob->lru_lock);
  481. }
  482. if (!ret)
  483. ret = ttm_bo_cleanup_refs_and_unlock(entry, false,
  484. !remove_all);
  485. else
  486. spin_unlock(&glob->lru_lock);
  487. kref_put(&entry->list_kref, ttm_bo_release_list);
  488. entry = nentry;
  489. if (ret || !entry)
  490. goto out;
  491. spin_lock(&glob->lru_lock);
  492. if (list_empty(&entry->ddestroy))
  493. break;
  494. }
  495. out_unlock:
  496. spin_unlock(&glob->lru_lock);
  497. out:
  498. if (entry)
  499. kref_put(&entry->list_kref, ttm_bo_release_list);
  500. return ret;
  501. }
  502. static void ttm_bo_delayed_workqueue(struct work_struct *work)
  503. {
  504. struct ttm_bo_device *bdev =
  505. container_of(work, struct ttm_bo_device, wq.work);
  506. if (ttm_bo_delayed_delete(bdev, false)) {
  507. schedule_delayed_work(&bdev->wq,
  508. ((HZ / 100) < 1) ? 1 : HZ / 100);
  509. }
  510. }
  511. static void ttm_bo_release(struct kref *kref)
  512. {
  513. struct ttm_buffer_object *bo =
  514. container_of(kref, struct ttm_buffer_object, kref);
  515. struct ttm_bo_device *bdev = bo->bdev;
  516. struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
  517. drm_vma_offset_remove(&bdev->vma_manager, &bo->vma_node);
  518. ttm_mem_io_lock(man, false);
  519. ttm_mem_io_free_vm(bo);
  520. ttm_mem_io_unlock(man);
  521. ttm_bo_cleanup_refs_or_queue(bo);
  522. kref_put(&bo->list_kref, ttm_bo_release_list);
  523. }
  524. void ttm_bo_unref(struct ttm_buffer_object **p_bo)
  525. {
  526. struct ttm_buffer_object *bo = *p_bo;
  527. *p_bo = NULL;
  528. kref_put(&bo->kref, ttm_bo_release);
  529. }
  530. EXPORT_SYMBOL(ttm_bo_unref);
  531. int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
  532. {
  533. return cancel_delayed_work_sync(&bdev->wq);
  534. }
  535. EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
  536. void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
  537. {
  538. if (resched)
  539. schedule_delayed_work(&bdev->wq,
  540. ((HZ / 100) < 1) ? 1 : HZ / 100);
  541. }
  542. EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
  543. static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
  544. bool no_wait_gpu)
  545. {
  546. struct ttm_bo_device *bdev = bo->bdev;
  547. struct ttm_mem_reg evict_mem;
  548. struct ttm_placement placement;
  549. int ret = 0;
  550. spin_lock(&bdev->fence_lock);
  551. ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
  552. spin_unlock(&bdev->fence_lock);
  553. if (unlikely(ret != 0)) {
  554. if (ret != -ERESTARTSYS) {
  555. pr_err("Failed to expire sync object before buffer eviction\n");
  556. }
  557. goto out;
  558. }
  559. lockdep_assert_held(&bo->resv->lock.base);
  560. evict_mem = bo->mem;
  561. evict_mem.mm_node = NULL;
  562. evict_mem.bus.io_reserved_vm = false;
  563. evict_mem.bus.io_reserved_count = 0;
  564. placement.fpfn = 0;
  565. placement.lpfn = 0;
  566. placement.num_placement = 0;
  567. placement.num_busy_placement = 0;
  568. bdev->driver->evict_flags(bo, &placement);
  569. ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
  570. no_wait_gpu);
  571. if (ret) {
  572. if (ret != -ERESTARTSYS) {
  573. pr_err("Failed to find memory space for buffer 0x%p eviction\n",
  574. bo);
  575. ttm_bo_mem_space_debug(bo, &placement);
  576. }
  577. goto out;
  578. }
  579. ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
  580. no_wait_gpu);
  581. if (ret) {
  582. if (ret != -ERESTARTSYS)
  583. pr_err("Buffer eviction failed\n");
  584. ttm_bo_mem_put(bo, &evict_mem);
  585. goto out;
  586. }
  587. bo->evicted = true;
  588. out:
  589. return ret;
  590. }
  591. static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
  592. uint32_t mem_type,
  593. bool interruptible,
  594. bool no_wait_gpu)
  595. {
  596. struct ttm_bo_global *glob = bdev->glob;
  597. struct ttm_mem_type_manager *man = &bdev->man[mem_type];
  598. struct ttm_buffer_object *bo;
  599. int ret = -EBUSY, put_count;
  600. spin_lock(&glob->lru_lock);
  601. list_for_each_entry(bo, &man->lru, lru) {
  602. ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
  603. if (!ret)
  604. break;
  605. }
  606. if (ret) {
  607. spin_unlock(&glob->lru_lock);
  608. return ret;
  609. }
  610. kref_get(&bo->list_kref);
  611. if (!list_empty(&bo->ddestroy)) {
  612. ret = ttm_bo_cleanup_refs_and_unlock(bo, interruptible,
  613. no_wait_gpu);
  614. kref_put(&bo->list_kref, ttm_bo_release_list);
  615. return ret;
  616. }
  617. put_count = ttm_bo_del_from_lru(bo);
  618. spin_unlock(&glob->lru_lock);
  619. BUG_ON(ret != 0);
  620. ttm_bo_list_ref_sub(bo, put_count, true);
  621. ret = ttm_bo_evict(bo, interruptible, no_wait_gpu);
  622. ttm_bo_unreserve(bo);
  623. kref_put(&bo->list_kref, ttm_bo_release_list);
  624. return ret;
  625. }
  626. void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
  627. {
  628. struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
  629. if (mem->mm_node)
  630. (*man->func->put_node)(man, mem);
  631. }
  632. EXPORT_SYMBOL(ttm_bo_mem_put);
  633. /**
  634. * Repeatedly evict memory from the LRU for @mem_type until we create enough
  635. * space, or we've evicted everything and there isn't enough space.
  636. */
  637. static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
  638. uint32_t mem_type,
  639. struct ttm_placement *placement,
  640. struct ttm_mem_reg *mem,
  641. bool interruptible,
  642. bool no_wait_gpu)
  643. {
  644. struct ttm_bo_device *bdev = bo->bdev;
  645. struct ttm_mem_type_manager *man = &bdev->man[mem_type];
  646. int ret;
  647. do {
  648. ret = (*man->func->get_node)(man, bo, placement, mem);
  649. if (unlikely(ret != 0))
  650. return ret;
  651. if (mem->mm_node)
  652. break;
  653. ret = ttm_mem_evict_first(bdev, mem_type,
  654. interruptible, no_wait_gpu);
  655. if (unlikely(ret != 0))
  656. return ret;
  657. } while (1);
  658. if (mem->mm_node == NULL)
  659. return -ENOMEM;
  660. mem->mem_type = mem_type;
  661. return 0;
  662. }
  663. static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
  664. uint32_t cur_placement,
  665. uint32_t proposed_placement)
  666. {
  667. uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
  668. uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
  669. /**
  670. * Keep current caching if possible.
  671. */
  672. if ((cur_placement & caching) != 0)
  673. result |= (cur_placement & caching);
  674. else if ((man->default_caching & caching) != 0)
  675. result |= man->default_caching;
  676. else if ((TTM_PL_FLAG_CACHED & caching) != 0)
  677. result |= TTM_PL_FLAG_CACHED;
  678. else if ((TTM_PL_FLAG_WC & caching) != 0)
  679. result |= TTM_PL_FLAG_WC;
  680. else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
  681. result |= TTM_PL_FLAG_UNCACHED;
  682. return result;
  683. }
  684. static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
  685. uint32_t mem_type,
  686. uint32_t proposed_placement,
  687. uint32_t *masked_placement)
  688. {
  689. uint32_t cur_flags = ttm_bo_type_flags(mem_type);
  690. if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
  691. return false;
  692. if ((proposed_placement & man->available_caching) == 0)
  693. return false;
  694. cur_flags |= (proposed_placement & man->available_caching);
  695. *masked_placement = cur_flags;
  696. return true;
  697. }
  698. /**
  699. * Creates space for memory region @mem according to its type.
  700. *
  701. * This function first searches for free space in compatible memory types in
  702. * the priority order defined by the driver. If free space isn't found, then
  703. * ttm_bo_mem_force_space is attempted in priority order to evict and find
  704. * space.
  705. */
  706. int ttm_bo_mem_space(struct ttm_buffer_object *bo,
  707. struct ttm_placement *placement,
  708. struct ttm_mem_reg *mem,
  709. bool interruptible,
  710. bool no_wait_gpu)
  711. {
  712. struct ttm_bo_device *bdev = bo->bdev;
  713. struct ttm_mem_type_manager *man;
  714. uint32_t mem_type = TTM_PL_SYSTEM;
  715. uint32_t cur_flags = 0;
  716. bool type_found = false;
  717. bool type_ok = false;
  718. bool has_erestartsys = false;
  719. int i, ret;
  720. mem->mm_node = NULL;
  721. for (i = 0; i < placement->num_placement; ++i) {
  722. ret = ttm_mem_type_from_flags(placement->placement[i],
  723. &mem_type);
  724. if (ret)
  725. return ret;
  726. man = &bdev->man[mem_type];
  727. type_ok = ttm_bo_mt_compatible(man,
  728. mem_type,
  729. placement->placement[i],
  730. &cur_flags);
  731. if (!type_ok)
  732. continue;
  733. cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
  734. cur_flags);
  735. /*
  736. * Use the access and other non-mapping-related flag bits from
  737. * the memory placement flags to the current flags
  738. */
  739. ttm_flag_masked(&cur_flags, placement->placement[i],
  740. ~TTM_PL_MASK_MEMTYPE);
  741. if (mem_type == TTM_PL_SYSTEM)
  742. break;
  743. if (man->has_type && man->use_type) {
  744. type_found = true;
  745. ret = (*man->func->get_node)(man, bo, placement, mem);
  746. if (unlikely(ret))
  747. return ret;
  748. }
  749. if (mem->mm_node)
  750. break;
  751. }
  752. if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
  753. mem->mem_type = mem_type;
  754. mem->placement = cur_flags;
  755. return 0;
  756. }
  757. if (!type_found)
  758. return -EINVAL;
  759. for (i = 0; i < placement->num_busy_placement; ++i) {
  760. ret = ttm_mem_type_from_flags(placement->busy_placement[i],
  761. &mem_type);
  762. if (ret)
  763. return ret;
  764. man = &bdev->man[mem_type];
  765. if (!man->has_type)
  766. continue;
  767. if (!ttm_bo_mt_compatible(man,
  768. mem_type,
  769. placement->busy_placement[i],
  770. &cur_flags))
  771. continue;
  772. cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
  773. cur_flags);
  774. /*
  775. * Use the access and other non-mapping-related flag bits from
  776. * the memory placement flags to the current flags
  777. */
  778. ttm_flag_masked(&cur_flags, placement->busy_placement[i],
  779. ~TTM_PL_MASK_MEMTYPE);
  780. if (mem_type == TTM_PL_SYSTEM) {
  781. mem->mem_type = mem_type;
  782. mem->placement = cur_flags;
  783. mem->mm_node = NULL;
  784. return 0;
  785. }
  786. ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
  787. interruptible, no_wait_gpu);
  788. if (ret == 0 && mem->mm_node) {
  789. mem->placement = cur_flags;
  790. return 0;
  791. }
  792. if (ret == -ERESTARTSYS)
  793. has_erestartsys = true;
  794. }
  795. ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
  796. return ret;
  797. }
  798. EXPORT_SYMBOL(ttm_bo_mem_space);
  799. int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
  800. struct ttm_placement *placement,
  801. bool interruptible,
  802. bool no_wait_gpu)
  803. {
  804. int ret = 0;
  805. struct ttm_mem_reg mem;
  806. struct ttm_bo_device *bdev = bo->bdev;
  807. lockdep_assert_held(&bo->resv->lock.base);
  808. /*
  809. * FIXME: It's possible to pipeline buffer moves.
  810. * Have the driver move function wait for idle when necessary,
  811. * instead of doing it here.
  812. */
  813. spin_lock(&bdev->fence_lock);
  814. ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
  815. spin_unlock(&bdev->fence_lock);
  816. if (ret)
  817. return ret;
  818. mem.num_pages = bo->num_pages;
  819. mem.size = mem.num_pages << PAGE_SHIFT;
  820. mem.page_alignment = bo->mem.page_alignment;
  821. mem.bus.io_reserved_vm = false;
  822. mem.bus.io_reserved_count = 0;
  823. /*
  824. * Determine where to move the buffer.
  825. */
  826. ret = ttm_bo_mem_space(bo, placement, &mem,
  827. interruptible, no_wait_gpu);
  828. if (ret)
  829. goto out_unlock;
  830. ret = ttm_bo_handle_move_mem(bo, &mem, false,
  831. interruptible, no_wait_gpu);
  832. out_unlock:
  833. if (ret && mem.mm_node)
  834. ttm_bo_mem_put(bo, &mem);
  835. return ret;
  836. }
  837. static int ttm_bo_mem_compat(struct ttm_placement *placement,
  838. struct ttm_mem_reg *mem)
  839. {
  840. int i;
  841. if (mem->mm_node && placement->lpfn != 0 &&
  842. (mem->start < placement->fpfn ||
  843. mem->start + mem->num_pages > placement->lpfn))
  844. return -1;
  845. for (i = 0; i < placement->num_placement; i++) {
  846. if ((placement->placement[i] & mem->placement &
  847. TTM_PL_MASK_CACHING) &&
  848. (placement->placement[i] & mem->placement &
  849. TTM_PL_MASK_MEM))
  850. return i;
  851. }
  852. return -1;
  853. }
  854. int ttm_bo_validate(struct ttm_buffer_object *bo,
  855. struct ttm_placement *placement,
  856. bool interruptible,
  857. bool no_wait_gpu)
  858. {
  859. int ret;
  860. lockdep_assert_held(&bo->resv->lock.base);
  861. /* Check that range is valid */
  862. if (placement->lpfn || placement->fpfn)
  863. if (placement->fpfn > placement->lpfn ||
  864. (placement->lpfn - placement->fpfn) < bo->num_pages)
  865. return -EINVAL;
  866. /*
  867. * Check whether we need to move buffer.
  868. */
  869. ret = ttm_bo_mem_compat(placement, &bo->mem);
  870. if (ret < 0) {
  871. ret = ttm_bo_move_buffer(bo, placement, interruptible,
  872. no_wait_gpu);
  873. if (ret)
  874. return ret;
  875. } else {
  876. /*
  877. * Use the access and other non-mapping-related flag bits from
  878. * the compatible memory placement flags to the active flags
  879. */
  880. ttm_flag_masked(&bo->mem.placement, placement->placement[ret],
  881. ~TTM_PL_MASK_MEMTYPE);
  882. }
  883. /*
  884. * We might need to add a TTM.
  885. */
  886. if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
  887. ret = ttm_bo_add_ttm(bo, true);
  888. if (ret)
  889. return ret;
  890. }
  891. return 0;
  892. }
  893. EXPORT_SYMBOL(ttm_bo_validate);
  894. int ttm_bo_check_placement(struct ttm_buffer_object *bo,
  895. struct ttm_placement *placement)
  896. {
  897. BUG_ON((placement->fpfn || placement->lpfn) &&
  898. (bo->mem.num_pages > (placement->lpfn - placement->fpfn)));
  899. return 0;
  900. }
  901. int ttm_bo_init(struct ttm_bo_device *bdev,
  902. struct ttm_buffer_object *bo,
  903. unsigned long size,
  904. enum ttm_bo_type type,
  905. struct ttm_placement *placement,
  906. uint32_t page_alignment,
  907. bool interruptible,
  908. struct file *persistent_swap_storage,
  909. size_t acc_size,
  910. struct sg_table *sg,
  911. void (*destroy) (struct ttm_buffer_object *))
  912. {
  913. int ret = 0;
  914. unsigned long num_pages;
  915. struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
  916. bool locked;
  917. ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
  918. if (ret) {
  919. pr_err("Out of kernel memory\n");
  920. if (destroy)
  921. (*destroy)(bo);
  922. else
  923. kfree(bo);
  924. return -ENOMEM;
  925. }
  926. num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  927. if (num_pages == 0) {
  928. pr_err("Illegal buffer object size\n");
  929. if (destroy)
  930. (*destroy)(bo);
  931. else
  932. kfree(bo);
  933. ttm_mem_global_free(mem_glob, acc_size);
  934. return -EINVAL;
  935. }
  936. bo->destroy = destroy;
  937. kref_init(&bo->kref);
  938. kref_init(&bo->list_kref);
  939. atomic_set(&bo->cpu_writers, 0);
  940. INIT_LIST_HEAD(&bo->lru);
  941. INIT_LIST_HEAD(&bo->ddestroy);
  942. INIT_LIST_HEAD(&bo->swap);
  943. INIT_LIST_HEAD(&bo->io_reserve_lru);
  944. bo->bdev = bdev;
  945. bo->glob = bdev->glob;
  946. bo->type = type;
  947. bo->num_pages = num_pages;
  948. bo->mem.size = num_pages << PAGE_SHIFT;
  949. bo->mem.mem_type = TTM_PL_SYSTEM;
  950. bo->mem.num_pages = bo->num_pages;
  951. bo->mem.mm_node = NULL;
  952. bo->mem.page_alignment = page_alignment;
  953. bo->mem.bus.io_reserved_vm = false;
  954. bo->mem.bus.io_reserved_count = 0;
  955. bo->priv_flags = 0;
  956. bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
  957. bo->persistent_swap_storage = persistent_swap_storage;
  958. bo->acc_size = acc_size;
  959. bo->sg = sg;
  960. bo->resv = &bo->ttm_resv;
  961. reservation_object_init(bo->resv);
  962. atomic_inc(&bo->glob->bo_count);
  963. drm_vma_node_reset(&bo->vma_node);
  964. ret = ttm_bo_check_placement(bo, placement);
  965. /*
  966. * For ttm_bo_type_device buffers, allocate
  967. * address space from the device.
  968. */
  969. if (likely(!ret) &&
  970. (bo->type == ttm_bo_type_device ||
  971. bo->type == ttm_bo_type_sg))
  972. ret = ttm_bo_setup_vm(bo);
  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. /**
  1273. * ttm_bo_setup_vm:
  1274. *
  1275. * @bo: the buffer to allocate address space for
  1276. *
  1277. * Allocate address space in the drm device so that applications
  1278. * can mmap the buffer and access the contents. This only
  1279. * applies to ttm_bo_type_device objects as others are not
  1280. * placed in the drm device address space.
  1281. */
  1282. static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
  1283. {
  1284. struct ttm_bo_device *bdev = bo->bdev;
  1285. return drm_vma_offset_add(&bdev->vma_manager, &bo->vma_node,
  1286. bo->mem.num_pages);
  1287. }
  1288. int ttm_bo_wait(struct ttm_buffer_object *bo,
  1289. bool lazy, bool interruptible, bool no_wait)
  1290. {
  1291. struct ttm_bo_driver *driver = bo->bdev->driver;
  1292. struct ttm_bo_device *bdev = bo->bdev;
  1293. void *sync_obj;
  1294. int ret = 0;
  1295. if (likely(bo->sync_obj == NULL))
  1296. return 0;
  1297. while (bo->sync_obj) {
  1298. if (driver->sync_obj_signaled(bo->sync_obj)) {
  1299. void *tmp_obj = bo->sync_obj;
  1300. bo->sync_obj = NULL;
  1301. clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
  1302. spin_unlock(&bdev->fence_lock);
  1303. driver->sync_obj_unref(&tmp_obj);
  1304. spin_lock(&bdev->fence_lock);
  1305. continue;
  1306. }
  1307. if (no_wait)
  1308. return -EBUSY;
  1309. sync_obj = driver->sync_obj_ref(bo->sync_obj);
  1310. spin_unlock(&bdev->fence_lock);
  1311. ret = driver->sync_obj_wait(sync_obj,
  1312. lazy, interruptible);
  1313. if (unlikely(ret != 0)) {
  1314. driver->sync_obj_unref(&sync_obj);
  1315. spin_lock(&bdev->fence_lock);
  1316. return ret;
  1317. }
  1318. spin_lock(&bdev->fence_lock);
  1319. if (likely(bo->sync_obj == sync_obj)) {
  1320. void *tmp_obj = bo->sync_obj;
  1321. bo->sync_obj = NULL;
  1322. clear_bit(TTM_BO_PRIV_FLAG_MOVING,
  1323. &bo->priv_flags);
  1324. spin_unlock(&bdev->fence_lock);
  1325. driver->sync_obj_unref(&sync_obj);
  1326. driver->sync_obj_unref(&tmp_obj);
  1327. spin_lock(&bdev->fence_lock);
  1328. } else {
  1329. spin_unlock(&bdev->fence_lock);
  1330. driver->sync_obj_unref(&sync_obj);
  1331. spin_lock(&bdev->fence_lock);
  1332. }
  1333. }
  1334. return 0;
  1335. }
  1336. EXPORT_SYMBOL(ttm_bo_wait);
  1337. int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
  1338. {
  1339. struct ttm_bo_device *bdev = bo->bdev;
  1340. int ret = 0;
  1341. /*
  1342. * Using ttm_bo_reserve makes sure the lru lists are updated.
  1343. */
  1344. ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
  1345. if (unlikely(ret != 0))
  1346. return ret;
  1347. spin_lock(&bdev->fence_lock);
  1348. ret = ttm_bo_wait(bo, false, true, no_wait);
  1349. spin_unlock(&bdev->fence_lock);
  1350. if (likely(ret == 0))
  1351. atomic_inc(&bo->cpu_writers);
  1352. ttm_bo_unreserve(bo);
  1353. return ret;
  1354. }
  1355. EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
  1356. void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
  1357. {
  1358. atomic_dec(&bo->cpu_writers);
  1359. }
  1360. EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
  1361. /**
  1362. * A buffer object shrink method that tries to swap out the first
  1363. * buffer object on the bo_global::swap_lru list.
  1364. */
  1365. static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
  1366. {
  1367. struct ttm_bo_global *glob =
  1368. container_of(shrink, struct ttm_bo_global, shrink);
  1369. struct ttm_buffer_object *bo;
  1370. int ret = -EBUSY;
  1371. int put_count;
  1372. uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
  1373. spin_lock(&glob->lru_lock);
  1374. list_for_each_entry(bo, &glob->swap_lru, swap) {
  1375. ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
  1376. if (!ret)
  1377. break;
  1378. }
  1379. if (ret) {
  1380. spin_unlock(&glob->lru_lock);
  1381. return ret;
  1382. }
  1383. kref_get(&bo->list_kref);
  1384. if (!list_empty(&bo->ddestroy)) {
  1385. ret = ttm_bo_cleanup_refs_and_unlock(bo, false, false);
  1386. kref_put(&bo->list_kref, ttm_bo_release_list);
  1387. return ret;
  1388. }
  1389. put_count = ttm_bo_del_from_lru(bo);
  1390. spin_unlock(&glob->lru_lock);
  1391. ttm_bo_list_ref_sub(bo, put_count, true);
  1392. /**
  1393. * Wait for GPU, then move to system cached.
  1394. */
  1395. spin_lock(&bo->bdev->fence_lock);
  1396. ret = ttm_bo_wait(bo, false, false, false);
  1397. spin_unlock(&bo->bdev->fence_lock);
  1398. if (unlikely(ret != 0))
  1399. goto out;
  1400. if ((bo->mem.placement & swap_placement) != swap_placement) {
  1401. struct ttm_mem_reg evict_mem;
  1402. evict_mem = bo->mem;
  1403. evict_mem.mm_node = NULL;
  1404. evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
  1405. evict_mem.mem_type = TTM_PL_SYSTEM;
  1406. ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
  1407. false, false);
  1408. if (unlikely(ret != 0))
  1409. goto out;
  1410. }
  1411. ttm_bo_unmap_virtual(bo);
  1412. /**
  1413. * Swap out. Buffer will be swapped in again as soon as
  1414. * anyone tries to access a ttm page.
  1415. */
  1416. if (bo->bdev->driver->swap_notify)
  1417. bo->bdev->driver->swap_notify(bo);
  1418. ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
  1419. out:
  1420. /**
  1421. *
  1422. * Unreserve without putting on LRU to avoid swapping out an
  1423. * already swapped buffer.
  1424. */
  1425. ww_mutex_unlock(&bo->resv->lock);
  1426. kref_put(&bo->list_kref, ttm_bo_release_list);
  1427. return ret;
  1428. }
  1429. void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
  1430. {
  1431. while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
  1432. ;
  1433. }
  1434. EXPORT_SYMBOL(ttm_bo_swapout_all);