ttm_bo.c 44 KB

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