ttm_bo.c 43 KB

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