ttm_bo.c 44 KB

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