ttm_bo.c 44 KB

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