ttm_bo.c 47 KB

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