ttm_bo.c 43 KB

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