ttm_bo.c 47 KB

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