nouveau_bo.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562
  1. /*
  2. * Copyright 2007 Dave Airlied
  3. * All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice (including the next
  13. * paragraph) shall be included in all copies or substantial portions of the
  14. * Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22. * OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. /*
  25. * Authors: Dave Airlied <airlied@linux.ie>
  26. * Ben Skeggs <darktama@iinet.net.au>
  27. * Jeremy Kolb <jkolb@brandeis.edu>
  28. */
  29. #include <core/engine.h>
  30. #include <linux/swiotlb.h>
  31. #include <subdev/fb.h>
  32. #include <subdev/vm.h>
  33. #include <subdev/bar.h>
  34. #include "nouveau_drm.h"
  35. #include "nouveau_dma.h"
  36. #include "nouveau_fence.h"
  37. #include "nouveau_bo.h"
  38. #include "nouveau_ttm.h"
  39. #include "nouveau_gem.h"
  40. /*
  41. * NV10-NV40 tiling helpers
  42. */
  43. static void
  44. nv10_bo_update_tile_region(struct drm_device *dev, struct nouveau_drm_tile *reg,
  45. u32 addr, u32 size, u32 pitch, u32 flags)
  46. {
  47. struct nouveau_drm *drm = nouveau_drm(dev);
  48. int i = reg - drm->tile.reg;
  49. struct nouveau_fb *pfb = nouveau_fb(drm->device);
  50. struct nouveau_fb_tile *tile = &pfb->tile.region[i];
  51. struct nouveau_engine *engine;
  52. nouveau_fence_unref(&reg->fence);
  53. if (tile->pitch)
  54. pfb->tile.fini(pfb, i, tile);
  55. if (pitch)
  56. pfb->tile.init(pfb, i, addr, size, pitch, flags, tile);
  57. pfb->tile.prog(pfb, i, tile);
  58. if ((engine = nouveau_engine(pfb, NVDEV_ENGINE_GR)))
  59. engine->tile_prog(engine, i);
  60. if ((engine = nouveau_engine(pfb, NVDEV_ENGINE_MPEG)))
  61. engine->tile_prog(engine, i);
  62. }
  63. static struct nouveau_drm_tile *
  64. nv10_bo_get_tile_region(struct drm_device *dev, int i)
  65. {
  66. struct nouveau_drm *drm = nouveau_drm(dev);
  67. struct nouveau_drm_tile *tile = &drm->tile.reg[i];
  68. spin_lock(&drm->tile.lock);
  69. if (!tile->used &&
  70. (!tile->fence || nouveau_fence_done(tile->fence)))
  71. tile->used = true;
  72. else
  73. tile = NULL;
  74. spin_unlock(&drm->tile.lock);
  75. return tile;
  76. }
  77. static void
  78. nv10_bo_put_tile_region(struct drm_device *dev, struct nouveau_drm_tile *tile,
  79. struct nouveau_fence *fence)
  80. {
  81. struct nouveau_drm *drm = nouveau_drm(dev);
  82. if (tile) {
  83. spin_lock(&drm->tile.lock);
  84. if (fence) {
  85. /* Mark it as pending. */
  86. tile->fence = fence;
  87. nouveau_fence_ref(fence);
  88. }
  89. tile->used = false;
  90. spin_unlock(&drm->tile.lock);
  91. }
  92. }
  93. static struct nouveau_drm_tile *
  94. nv10_bo_set_tiling(struct drm_device *dev, u32 addr,
  95. u32 size, u32 pitch, u32 flags)
  96. {
  97. struct nouveau_drm *drm = nouveau_drm(dev);
  98. struct nouveau_fb *pfb = nouveau_fb(drm->device);
  99. struct nouveau_drm_tile *tile, *found = NULL;
  100. int i;
  101. for (i = 0; i < pfb->tile.regions; i++) {
  102. tile = nv10_bo_get_tile_region(dev, i);
  103. if (pitch && !found) {
  104. found = tile;
  105. continue;
  106. } else if (tile && pfb->tile.region[i].pitch) {
  107. /* Kill an unused tile region. */
  108. nv10_bo_update_tile_region(dev, tile, 0, 0, 0, 0);
  109. }
  110. nv10_bo_put_tile_region(dev, tile, NULL);
  111. }
  112. if (found)
  113. nv10_bo_update_tile_region(dev, found, addr, size,
  114. pitch, flags);
  115. return found;
  116. }
  117. static void
  118. nouveau_bo_del_ttm(struct ttm_buffer_object *bo)
  119. {
  120. struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
  121. struct drm_device *dev = drm->dev;
  122. struct nouveau_bo *nvbo = nouveau_bo(bo);
  123. if (unlikely(nvbo->gem))
  124. DRM_ERROR("bo %p still attached to GEM object\n", bo);
  125. WARN_ON(nvbo->pin_refcnt > 0);
  126. nv10_bo_put_tile_region(dev, nvbo->tile, NULL);
  127. kfree(nvbo);
  128. }
  129. static void
  130. nouveau_bo_fixup_align(struct nouveau_bo *nvbo, u32 flags,
  131. int *align, int *size)
  132. {
  133. struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
  134. struct nouveau_device *device = nv_device(drm->device);
  135. if (device->card_type < NV_50) {
  136. if (nvbo->tile_mode) {
  137. if (device->chipset >= 0x40) {
  138. *align = 65536;
  139. *size = roundup(*size, 64 * nvbo->tile_mode);
  140. } else if (device->chipset >= 0x30) {
  141. *align = 32768;
  142. *size = roundup(*size, 64 * nvbo->tile_mode);
  143. } else if (device->chipset >= 0x20) {
  144. *align = 16384;
  145. *size = roundup(*size, 64 * nvbo->tile_mode);
  146. } else if (device->chipset >= 0x10) {
  147. *align = 16384;
  148. *size = roundup(*size, 32 * nvbo->tile_mode);
  149. }
  150. }
  151. } else {
  152. *size = roundup(*size, (1 << nvbo->page_shift));
  153. *align = max((1 << nvbo->page_shift), *align);
  154. }
  155. *size = roundup(*size, PAGE_SIZE);
  156. }
  157. int
  158. nouveau_bo_new(struct drm_device *dev, int size, int align,
  159. uint32_t flags, uint32_t tile_mode, uint32_t tile_flags,
  160. struct sg_table *sg,
  161. struct nouveau_bo **pnvbo)
  162. {
  163. struct nouveau_drm *drm = nouveau_drm(dev);
  164. struct nouveau_bo *nvbo;
  165. size_t acc_size;
  166. int ret;
  167. int type = ttm_bo_type_device;
  168. if (sg)
  169. type = ttm_bo_type_sg;
  170. nvbo = kzalloc(sizeof(struct nouveau_bo), GFP_KERNEL);
  171. if (!nvbo)
  172. return -ENOMEM;
  173. INIT_LIST_HEAD(&nvbo->head);
  174. INIT_LIST_HEAD(&nvbo->entry);
  175. INIT_LIST_HEAD(&nvbo->vma_list);
  176. nvbo->tile_mode = tile_mode;
  177. nvbo->tile_flags = tile_flags;
  178. nvbo->bo.bdev = &drm->ttm.bdev;
  179. nvbo->page_shift = 12;
  180. if (drm->client.base.vm) {
  181. if (!(flags & TTM_PL_FLAG_TT) && size > 256 * 1024)
  182. nvbo->page_shift = drm->client.base.vm->vmm->lpg_shift;
  183. }
  184. nouveau_bo_fixup_align(nvbo, flags, &align, &size);
  185. nvbo->bo.mem.num_pages = size >> PAGE_SHIFT;
  186. nouveau_bo_placement_set(nvbo, flags, 0);
  187. acc_size = ttm_bo_dma_acc_size(&drm->ttm.bdev, size,
  188. sizeof(struct nouveau_bo));
  189. ret = ttm_bo_init(&drm->ttm.bdev, &nvbo->bo, size,
  190. type, &nvbo->placement,
  191. align >> PAGE_SHIFT, false, NULL, acc_size, sg,
  192. nouveau_bo_del_ttm);
  193. if (ret) {
  194. /* ttm will call nouveau_bo_del_ttm if it fails.. */
  195. return ret;
  196. }
  197. *pnvbo = nvbo;
  198. return 0;
  199. }
  200. static void
  201. set_placement_list(uint32_t *pl, unsigned *n, uint32_t type, uint32_t flags)
  202. {
  203. *n = 0;
  204. if (type & TTM_PL_FLAG_VRAM)
  205. pl[(*n)++] = TTM_PL_FLAG_VRAM | flags;
  206. if (type & TTM_PL_FLAG_TT)
  207. pl[(*n)++] = TTM_PL_FLAG_TT | flags;
  208. if (type & TTM_PL_FLAG_SYSTEM)
  209. pl[(*n)++] = TTM_PL_FLAG_SYSTEM | flags;
  210. }
  211. static void
  212. set_placement_range(struct nouveau_bo *nvbo, uint32_t type)
  213. {
  214. struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
  215. struct nouveau_fb *pfb = nouveau_fb(drm->device);
  216. u32 vram_pages = pfb->ram->size >> PAGE_SHIFT;
  217. if (nv_device(drm->device)->card_type == NV_10 &&
  218. nvbo->tile_mode && (type & TTM_PL_FLAG_VRAM) &&
  219. nvbo->bo.mem.num_pages < vram_pages / 4) {
  220. /*
  221. * Make sure that the color and depth buffers are handled
  222. * by independent memory controller units. Up to a 9x
  223. * speed up when alpha-blending and depth-test are enabled
  224. * at the same time.
  225. */
  226. if (nvbo->tile_flags & NOUVEAU_GEM_TILE_ZETA) {
  227. nvbo->placement.fpfn = vram_pages / 2;
  228. nvbo->placement.lpfn = ~0;
  229. } else {
  230. nvbo->placement.fpfn = 0;
  231. nvbo->placement.lpfn = vram_pages / 2;
  232. }
  233. }
  234. }
  235. void
  236. nouveau_bo_placement_set(struct nouveau_bo *nvbo, uint32_t type, uint32_t busy)
  237. {
  238. struct ttm_placement *pl = &nvbo->placement;
  239. uint32_t flags = TTM_PL_MASK_CACHING |
  240. (nvbo->pin_refcnt ? TTM_PL_FLAG_NO_EVICT : 0);
  241. pl->placement = nvbo->placements;
  242. set_placement_list(nvbo->placements, &pl->num_placement,
  243. type, flags);
  244. pl->busy_placement = nvbo->busy_placements;
  245. set_placement_list(nvbo->busy_placements, &pl->num_busy_placement,
  246. type | busy, flags);
  247. set_placement_range(nvbo, type);
  248. }
  249. int
  250. nouveau_bo_pin(struct nouveau_bo *nvbo, uint32_t memtype)
  251. {
  252. struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
  253. struct ttm_buffer_object *bo = &nvbo->bo;
  254. int ret;
  255. ret = ttm_bo_reserve(bo, false, false, false, 0);
  256. if (ret)
  257. goto out;
  258. if (nvbo->pin_refcnt && !(memtype & (1 << bo->mem.mem_type))) {
  259. NV_ERROR(drm, "bo %p pinned elsewhere: 0x%08x vs 0x%08x\n", bo,
  260. 1 << bo->mem.mem_type, memtype);
  261. ret = -EINVAL;
  262. goto out;
  263. }
  264. if (nvbo->pin_refcnt++)
  265. goto out;
  266. nouveau_bo_placement_set(nvbo, memtype, 0);
  267. ret = nouveau_bo_validate(nvbo, false, false);
  268. if (ret == 0) {
  269. switch (bo->mem.mem_type) {
  270. case TTM_PL_VRAM:
  271. drm->gem.vram_available -= bo->mem.size;
  272. break;
  273. case TTM_PL_TT:
  274. drm->gem.gart_available -= bo->mem.size;
  275. break;
  276. default:
  277. break;
  278. }
  279. }
  280. out:
  281. ttm_bo_unreserve(bo);
  282. return ret;
  283. }
  284. int
  285. nouveau_bo_unpin(struct nouveau_bo *nvbo)
  286. {
  287. struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
  288. struct ttm_buffer_object *bo = &nvbo->bo;
  289. int ret, ref;
  290. ret = ttm_bo_reserve(bo, false, false, false, 0);
  291. if (ret)
  292. return ret;
  293. ref = --nvbo->pin_refcnt;
  294. WARN_ON_ONCE(ref < 0);
  295. if (ref)
  296. goto out;
  297. nouveau_bo_placement_set(nvbo, bo->mem.placement, 0);
  298. ret = nouveau_bo_validate(nvbo, false, false);
  299. if (ret == 0) {
  300. switch (bo->mem.mem_type) {
  301. case TTM_PL_VRAM:
  302. drm->gem.vram_available += bo->mem.size;
  303. break;
  304. case TTM_PL_TT:
  305. drm->gem.gart_available += bo->mem.size;
  306. break;
  307. default:
  308. break;
  309. }
  310. }
  311. out:
  312. ttm_bo_unreserve(bo);
  313. return ret;
  314. }
  315. int
  316. nouveau_bo_map(struct nouveau_bo *nvbo)
  317. {
  318. int ret;
  319. ret = ttm_bo_reserve(&nvbo->bo, false, false, false, 0);
  320. if (ret)
  321. return ret;
  322. ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.mem.num_pages, &nvbo->kmap);
  323. ttm_bo_unreserve(&nvbo->bo);
  324. return ret;
  325. }
  326. void
  327. nouveau_bo_unmap(struct nouveau_bo *nvbo)
  328. {
  329. if (nvbo)
  330. ttm_bo_kunmap(&nvbo->kmap);
  331. }
  332. int
  333. nouveau_bo_validate(struct nouveau_bo *nvbo, bool interruptible,
  334. bool no_wait_gpu)
  335. {
  336. int ret;
  337. ret = ttm_bo_validate(&nvbo->bo, &nvbo->placement,
  338. interruptible, no_wait_gpu);
  339. if (ret)
  340. return ret;
  341. return 0;
  342. }
  343. u16
  344. nouveau_bo_rd16(struct nouveau_bo *nvbo, unsigned index)
  345. {
  346. bool is_iomem;
  347. u16 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
  348. mem = &mem[index];
  349. if (is_iomem)
  350. return ioread16_native((void __force __iomem *)mem);
  351. else
  352. return *mem;
  353. }
  354. void
  355. nouveau_bo_wr16(struct nouveau_bo *nvbo, unsigned index, u16 val)
  356. {
  357. bool is_iomem;
  358. u16 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
  359. mem = &mem[index];
  360. if (is_iomem)
  361. iowrite16_native(val, (void __force __iomem *)mem);
  362. else
  363. *mem = val;
  364. }
  365. u32
  366. nouveau_bo_rd32(struct nouveau_bo *nvbo, unsigned index)
  367. {
  368. bool is_iomem;
  369. u32 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
  370. mem = &mem[index];
  371. if (is_iomem)
  372. return ioread32_native((void __force __iomem *)mem);
  373. else
  374. return *mem;
  375. }
  376. void
  377. nouveau_bo_wr32(struct nouveau_bo *nvbo, unsigned index, u32 val)
  378. {
  379. bool is_iomem;
  380. u32 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
  381. mem = &mem[index];
  382. if (is_iomem)
  383. iowrite32_native(val, (void __force __iomem *)mem);
  384. else
  385. *mem = val;
  386. }
  387. static struct ttm_tt *
  388. nouveau_ttm_tt_create(struct ttm_bo_device *bdev, unsigned long size,
  389. uint32_t page_flags, struct page *dummy_read)
  390. {
  391. #if __OS_HAS_AGP
  392. struct nouveau_drm *drm = nouveau_bdev(bdev);
  393. struct drm_device *dev = drm->dev;
  394. if (drm->agp.stat == ENABLED) {
  395. return ttm_agp_tt_create(bdev, dev->agp->bridge, size,
  396. page_flags, dummy_read);
  397. }
  398. #endif
  399. return nouveau_sgdma_create_ttm(bdev, size, page_flags, dummy_read);
  400. }
  401. static int
  402. nouveau_bo_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags)
  403. {
  404. /* We'll do this from user space. */
  405. return 0;
  406. }
  407. static int
  408. nouveau_bo_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
  409. struct ttm_mem_type_manager *man)
  410. {
  411. struct nouveau_drm *drm = nouveau_bdev(bdev);
  412. switch (type) {
  413. case TTM_PL_SYSTEM:
  414. man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
  415. man->available_caching = TTM_PL_MASK_CACHING;
  416. man->default_caching = TTM_PL_FLAG_CACHED;
  417. break;
  418. case TTM_PL_VRAM:
  419. if (nv_device(drm->device)->card_type >= NV_50) {
  420. man->func = &nouveau_vram_manager;
  421. man->io_reserve_fastpath = false;
  422. man->use_io_reserve_lru = true;
  423. } else {
  424. man->func = &ttm_bo_manager_func;
  425. }
  426. man->flags = TTM_MEMTYPE_FLAG_FIXED |
  427. TTM_MEMTYPE_FLAG_MAPPABLE;
  428. man->available_caching = TTM_PL_FLAG_UNCACHED |
  429. TTM_PL_FLAG_WC;
  430. man->default_caching = TTM_PL_FLAG_WC;
  431. break;
  432. case TTM_PL_TT:
  433. if (nv_device(drm->device)->card_type >= NV_50)
  434. man->func = &nouveau_gart_manager;
  435. else
  436. if (drm->agp.stat != ENABLED)
  437. man->func = &nv04_gart_manager;
  438. else
  439. man->func = &ttm_bo_manager_func;
  440. if (drm->agp.stat == ENABLED) {
  441. man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
  442. man->available_caching = TTM_PL_FLAG_UNCACHED |
  443. TTM_PL_FLAG_WC;
  444. man->default_caching = TTM_PL_FLAG_WC;
  445. } else {
  446. man->flags = TTM_MEMTYPE_FLAG_MAPPABLE |
  447. TTM_MEMTYPE_FLAG_CMA;
  448. man->available_caching = TTM_PL_MASK_CACHING;
  449. man->default_caching = TTM_PL_FLAG_CACHED;
  450. }
  451. break;
  452. default:
  453. return -EINVAL;
  454. }
  455. return 0;
  456. }
  457. static void
  458. nouveau_bo_evict_flags(struct ttm_buffer_object *bo, struct ttm_placement *pl)
  459. {
  460. struct nouveau_bo *nvbo = nouveau_bo(bo);
  461. switch (bo->mem.mem_type) {
  462. case TTM_PL_VRAM:
  463. nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_TT,
  464. TTM_PL_FLAG_SYSTEM);
  465. break;
  466. default:
  467. nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_SYSTEM, 0);
  468. break;
  469. }
  470. *pl = nvbo->placement;
  471. }
  472. /* GPU-assisted copy using NV_MEMORY_TO_MEMORY_FORMAT, can access
  473. * TTM_PL_{VRAM,TT} directly.
  474. */
  475. static int
  476. nouveau_bo_move_accel_cleanup(struct nouveau_channel *chan,
  477. struct nouveau_bo *nvbo, bool evict,
  478. bool no_wait_gpu, struct ttm_mem_reg *new_mem)
  479. {
  480. struct nouveau_fence *fence = NULL;
  481. int ret;
  482. ret = nouveau_fence_new(chan, false, &fence);
  483. if (ret)
  484. return ret;
  485. ret = ttm_bo_move_accel_cleanup(&nvbo->bo, fence, evict,
  486. no_wait_gpu, new_mem);
  487. nouveau_fence_unref(&fence);
  488. return ret;
  489. }
  490. static int
  491. nve0_bo_move_init(struct nouveau_channel *chan, u32 handle)
  492. {
  493. int ret = RING_SPACE(chan, 2);
  494. if (ret == 0) {
  495. BEGIN_NVC0(chan, NvSubCopy, 0x0000, 1);
  496. OUT_RING (chan, handle);
  497. FIRE_RING (chan);
  498. }
  499. return ret;
  500. }
  501. static int
  502. nve0_bo_move_copy(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
  503. struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
  504. {
  505. struct nouveau_mem *node = old_mem->mm_node;
  506. int ret = RING_SPACE(chan, 10);
  507. if (ret == 0) {
  508. BEGIN_NVC0(chan, NvSubCopy, 0x0400, 8);
  509. OUT_RING (chan, upper_32_bits(node->vma[0].offset));
  510. OUT_RING (chan, lower_32_bits(node->vma[0].offset));
  511. OUT_RING (chan, upper_32_bits(node->vma[1].offset));
  512. OUT_RING (chan, lower_32_bits(node->vma[1].offset));
  513. OUT_RING (chan, PAGE_SIZE);
  514. OUT_RING (chan, PAGE_SIZE);
  515. OUT_RING (chan, PAGE_SIZE);
  516. OUT_RING (chan, new_mem->num_pages);
  517. BEGIN_IMC0(chan, NvSubCopy, 0x0300, 0x0386);
  518. }
  519. return ret;
  520. }
  521. static int
  522. nvc0_bo_move_init(struct nouveau_channel *chan, u32 handle)
  523. {
  524. int ret = RING_SPACE(chan, 2);
  525. if (ret == 0) {
  526. BEGIN_NVC0(chan, NvSubCopy, 0x0000, 1);
  527. OUT_RING (chan, handle);
  528. }
  529. return ret;
  530. }
  531. static int
  532. nvc0_bo_move_copy(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
  533. struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
  534. {
  535. struct nouveau_mem *node = old_mem->mm_node;
  536. u64 src_offset = node->vma[0].offset;
  537. u64 dst_offset = node->vma[1].offset;
  538. u32 page_count = new_mem->num_pages;
  539. int ret;
  540. page_count = new_mem->num_pages;
  541. while (page_count) {
  542. int line_count = (page_count > 8191) ? 8191 : page_count;
  543. ret = RING_SPACE(chan, 11);
  544. if (ret)
  545. return ret;
  546. BEGIN_NVC0(chan, NvSubCopy, 0x030c, 8);
  547. OUT_RING (chan, upper_32_bits(src_offset));
  548. OUT_RING (chan, lower_32_bits(src_offset));
  549. OUT_RING (chan, upper_32_bits(dst_offset));
  550. OUT_RING (chan, lower_32_bits(dst_offset));
  551. OUT_RING (chan, PAGE_SIZE);
  552. OUT_RING (chan, PAGE_SIZE);
  553. OUT_RING (chan, PAGE_SIZE);
  554. OUT_RING (chan, line_count);
  555. BEGIN_NVC0(chan, NvSubCopy, 0x0300, 1);
  556. OUT_RING (chan, 0x00000110);
  557. page_count -= line_count;
  558. src_offset += (PAGE_SIZE * line_count);
  559. dst_offset += (PAGE_SIZE * line_count);
  560. }
  561. return 0;
  562. }
  563. static int
  564. nvc0_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
  565. struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
  566. {
  567. struct nouveau_mem *node = old_mem->mm_node;
  568. u64 src_offset = node->vma[0].offset;
  569. u64 dst_offset = node->vma[1].offset;
  570. u32 page_count = new_mem->num_pages;
  571. int ret;
  572. page_count = new_mem->num_pages;
  573. while (page_count) {
  574. int line_count = (page_count > 2047) ? 2047 : page_count;
  575. ret = RING_SPACE(chan, 12);
  576. if (ret)
  577. return ret;
  578. BEGIN_NVC0(chan, NvSubCopy, 0x0238, 2);
  579. OUT_RING (chan, upper_32_bits(dst_offset));
  580. OUT_RING (chan, lower_32_bits(dst_offset));
  581. BEGIN_NVC0(chan, NvSubCopy, 0x030c, 6);
  582. OUT_RING (chan, upper_32_bits(src_offset));
  583. OUT_RING (chan, lower_32_bits(src_offset));
  584. OUT_RING (chan, PAGE_SIZE); /* src_pitch */
  585. OUT_RING (chan, PAGE_SIZE); /* dst_pitch */
  586. OUT_RING (chan, PAGE_SIZE); /* line_length */
  587. OUT_RING (chan, line_count);
  588. BEGIN_NVC0(chan, NvSubCopy, 0x0300, 1);
  589. OUT_RING (chan, 0x00100110);
  590. page_count -= line_count;
  591. src_offset += (PAGE_SIZE * line_count);
  592. dst_offset += (PAGE_SIZE * line_count);
  593. }
  594. return 0;
  595. }
  596. static int
  597. nva3_bo_move_copy(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
  598. struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
  599. {
  600. struct nouveau_mem *node = old_mem->mm_node;
  601. u64 src_offset = node->vma[0].offset;
  602. u64 dst_offset = node->vma[1].offset;
  603. u32 page_count = new_mem->num_pages;
  604. int ret;
  605. page_count = new_mem->num_pages;
  606. while (page_count) {
  607. int line_count = (page_count > 8191) ? 8191 : page_count;
  608. ret = RING_SPACE(chan, 11);
  609. if (ret)
  610. return ret;
  611. BEGIN_NV04(chan, NvSubCopy, 0x030c, 8);
  612. OUT_RING (chan, upper_32_bits(src_offset));
  613. OUT_RING (chan, lower_32_bits(src_offset));
  614. OUT_RING (chan, upper_32_bits(dst_offset));
  615. OUT_RING (chan, lower_32_bits(dst_offset));
  616. OUT_RING (chan, PAGE_SIZE);
  617. OUT_RING (chan, PAGE_SIZE);
  618. OUT_RING (chan, PAGE_SIZE);
  619. OUT_RING (chan, line_count);
  620. BEGIN_NV04(chan, NvSubCopy, 0x0300, 1);
  621. OUT_RING (chan, 0x00000110);
  622. page_count -= line_count;
  623. src_offset += (PAGE_SIZE * line_count);
  624. dst_offset += (PAGE_SIZE * line_count);
  625. }
  626. return 0;
  627. }
  628. static int
  629. nv98_bo_move_exec(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
  630. struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
  631. {
  632. struct nouveau_mem *node = old_mem->mm_node;
  633. int ret = RING_SPACE(chan, 7);
  634. if (ret == 0) {
  635. BEGIN_NV04(chan, NvSubCopy, 0x0320, 6);
  636. OUT_RING (chan, upper_32_bits(node->vma[0].offset));
  637. OUT_RING (chan, lower_32_bits(node->vma[0].offset));
  638. OUT_RING (chan, upper_32_bits(node->vma[1].offset));
  639. OUT_RING (chan, lower_32_bits(node->vma[1].offset));
  640. OUT_RING (chan, 0x00000000 /* COPY */);
  641. OUT_RING (chan, new_mem->num_pages << PAGE_SHIFT);
  642. }
  643. return ret;
  644. }
  645. static int
  646. nv84_bo_move_exec(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
  647. struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
  648. {
  649. struct nouveau_mem *node = old_mem->mm_node;
  650. int ret = RING_SPACE(chan, 7);
  651. if (ret == 0) {
  652. BEGIN_NV04(chan, NvSubCopy, 0x0304, 6);
  653. OUT_RING (chan, new_mem->num_pages << PAGE_SHIFT);
  654. OUT_RING (chan, upper_32_bits(node->vma[0].offset));
  655. OUT_RING (chan, lower_32_bits(node->vma[0].offset));
  656. OUT_RING (chan, upper_32_bits(node->vma[1].offset));
  657. OUT_RING (chan, lower_32_bits(node->vma[1].offset));
  658. OUT_RING (chan, 0x00000000 /* MODE_COPY, QUERY_NONE */);
  659. }
  660. return ret;
  661. }
  662. static int
  663. nv50_bo_move_init(struct nouveau_channel *chan, u32 handle)
  664. {
  665. int ret = RING_SPACE(chan, 6);
  666. if (ret == 0) {
  667. BEGIN_NV04(chan, NvSubCopy, 0x0000, 1);
  668. OUT_RING (chan, handle);
  669. BEGIN_NV04(chan, NvSubCopy, 0x0180, 3);
  670. OUT_RING (chan, NvNotify0);
  671. OUT_RING (chan, NvDmaFB);
  672. OUT_RING (chan, NvDmaFB);
  673. }
  674. return ret;
  675. }
  676. static int
  677. nv50_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
  678. struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
  679. {
  680. struct nouveau_mem *node = old_mem->mm_node;
  681. struct nouveau_bo *nvbo = nouveau_bo(bo);
  682. u64 length = (new_mem->num_pages << PAGE_SHIFT);
  683. u64 src_offset = node->vma[0].offset;
  684. u64 dst_offset = node->vma[1].offset;
  685. int ret;
  686. while (length) {
  687. u32 amount, stride, height;
  688. amount = min(length, (u64)(4 * 1024 * 1024));
  689. stride = 16 * 4;
  690. height = amount / stride;
  691. if (old_mem->mem_type == TTM_PL_VRAM &&
  692. nouveau_bo_tile_layout(nvbo)) {
  693. ret = RING_SPACE(chan, 8);
  694. if (ret)
  695. return ret;
  696. BEGIN_NV04(chan, NvSubCopy, 0x0200, 7);
  697. OUT_RING (chan, 0);
  698. OUT_RING (chan, 0);
  699. OUT_RING (chan, stride);
  700. OUT_RING (chan, height);
  701. OUT_RING (chan, 1);
  702. OUT_RING (chan, 0);
  703. OUT_RING (chan, 0);
  704. } else {
  705. ret = RING_SPACE(chan, 2);
  706. if (ret)
  707. return ret;
  708. BEGIN_NV04(chan, NvSubCopy, 0x0200, 1);
  709. OUT_RING (chan, 1);
  710. }
  711. if (new_mem->mem_type == TTM_PL_VRAM &&
  712. nouveau_bo_tile_layout(nvbo)) {
  713. ret = RING_SPACE(chan, 8);
  714. if (ret)
  715. return ret;
  716. BEGIN_NV04(chan, NvSubCopy, 0x021c, 7);
  717. OUT_RING (chan, 0);
  718. OUT_RING (chan, 0);
  719. OUT_RING (chan, stride);
  720. OUT_RING (chan, height);
  721. OUT_RING (chan, 1);
  722. OUT_RING (chan, 0);
  723. OUT_RING (chan, 0);
  724. } else {
  725. ret = RING_SPACE(chan, 2);
  726. if (ret)
  727. return ret;
  728. BEGIN_NV04(chan, NvSubCopy, 0x021c, 1);
  729. OUT_RING (chan, 1);
  730. }
  731. ret = RING_SPACE(chan, 14);
  732. if (ret)
  733. return ret;
  734. BEGIN_NV04(chan, NvSubCopy, 0x0238, 2);
  735. OUT_RING (chan, upper_32_bits(src_offset));
  736. OUT_RING (chan, upper_32_bits(dst_offset));
  737. BEGIN_NV04(chan, NvSubCopy, 0x030c, 8);
  738. OUT_RING (chan, lower_32_bits(src_offset));
  739. OUT_RING (chan, lower_32_bits(dst_offset));
  740. OUT_RING (chan, stride);
  741. OUT_RING (chan, stride);
  742. OUT_RING (chan, stride);
  743. OUT_RING (chan, height);
  744. OUT_RING (chan, 0x00000101);
  745. OUT_RING (chan, 0x00000000);
  746. BEGIN_NV04(chan, NvSubCopy, NV_MEMORY_TO_MEMORY_FORMAT_NOP, 1);
  747. OUT_RING (chan, 0);
  748. length -= amount;
  749. src_offset += amount;
  750. dst_offset += amount;
  751. }
  752. return 0;
  753. }
  754. static int
  755. nv04_bo_move_init(struct nouveau_channel *chan, u32 handle)
  756. {
  757. int ret = RING_SPACE(chan, 4);
  758. if (ret == 0) {
  759. BEGIN_NV04(chan, NvSubCopy, 0x0000, 1);
  760. OUT_RING (chan, handle);
  761. BEGIN_NV04(chan, NvSubCopy, 0x0180, 1);
  762. OUT_RING (chan, NvNotify0);
  763. }
  764. return ret;
  765. }
  766. static inline uint32_t
  767. nouveau_bo_mem_ctxdma(struct ttm_buffer_object *bo,
  768. struct nouveau_channel *chan, struct ttm_mem_reg *mem)
  769. {
  770. if (mem->mem_type == TTM_PL_TT)
  771. return NvDmaTT;
  772. return NvDmaFB;
  773. }
  774. static int
  775. nv04_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
  776. struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
  777. {
  778. u32 src_offset = old_mem->start << PAGE_SHIFT;
  779. u32 dst_offset = new_mem->start << PAGE_SHIFT;
  780. u32 page_count = new_mem->num_pages;
  781. int ret;
  782. ret = RING_SPACE(chan, 3);
  783. if (ret)
  784. return ret;
  785. BEGIN_NV04(chan, NvSubCopy, NV_MEMORY_TO_MEMORY_FORMAT_DMA_SOURCE, 2);
  786. OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, old_mem));
  787. OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, new_mem));
  788. page_count = new_mem->num_pages;
  789. while (page_count) {
  790. int line_count = (page_count > 2047) ? 2047 : page_count;
  791. ret = RING_SPACE(chan, 11);
  792. if (ret)
  793. return ret;
  794. BEGIN_NV04(chan, NvSubCopy,
  795. NV_MEMORY_TO_MEMORY_FORMAT_OFFSET_IN, 8);
  796. OUT_RING (chan, src_offset);
  797. OUT_RING (chan, dst_offset);
  798. OUT_RING (chan, PAGE_SIZE); /* src_pitch */
  799. OUT_RING (chan, PAGE_SIZE); /* dst_pitch */
  800. OUT_RING (chan, PAGE_SIZE); /* line_length */
  801. OUT_RING (chan, line_count);
  802. OUT_RING (chan, 0x00000101);
  803. OUT_RING (chan, 0x00000000);
  804. BEGIN_NV04(chan, NvSubCopy, NV_MEMORY_TO_MEMORY_FORMAT_NOP, 1);
  805. OUT_RING (chan, 0);
  806. page_count -= line_count;
  807. src_offset += (PAGE_SIZE * line_count);
  808. dst_offset += (PAGE_SIZE * line_count);
  809. }
  810. return 0;
  811. }
  812. static int
  813. nouveau_vma_getmap(struct nouveau_channel *chan, struct nouveau_bo *nvbo,
  814. struct ttm_mem_reg *mem, struct nouveau_vma *vma)
  815. {
  816. struct nouveau_mem *node = mem->mm_node;
  817. int ret;
  818. ret = nouveau_vm_get(nv_client(chan->cli)->vm, mem->num_pages <<
  819. PAGE_SHIFT, node->page_shift,
  820. NV_MEM_ACCESS_RW, vma);
  821. if (ret)
  822. return ret;
  823. if (mem->mem_type == TTM_PL_VRAM)
  824. nouveau_vm_map(vma, node);
  825. else
  826. nouveau_vm_map_sg(vma, 0, mem->num_pages << PAGE_SHIFT, node);
  827. return 0;
  828. }
  829. static int
  830. nouveau_bo_move_m2mf(struct ttm_buffer_object *bo, int evict, bool intr,
  831. bool no_wait_gpu, struct ttm_mem_reg *new_mem)
  832. {
  833. struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
  834. struct nouveau_channel *chan = chan = drm->ttm.chan;
  835. struct nouveau_bo *nvbo = nouveau_bo(bo);
  836. struct ttm_mem_reg *old_mem = &bo->mem;
  837. int ret;
  838. mutex_lock_nested(&chan->cli->mutex, SINGLE_DEPTH_NESTING);
  839. /* create temporary vmas for the transfer and attach them to the
  840. * old nouveau_mem node, these will get cleaned up after ttm has
  841. * destroyed the ttm_mem_reg
  842. */
  843. if (nv_device(drm->device)->card_type >= NV_50) {
  844. struct nouveau_mem *node = old_mem->mm_node;
  845. ret = nouveau_vma_getmap(chan, nvbo, old_mem, &node->vma[0]);
  846. if (ret)
  847. goto out;
  848. ret = nouveau_vma_getmap(chan, nvbo, new_mem, &node->vma[1]);
  849. if (ret)
  850. goto out;
  851. }
  852. ret = drm->ttm.move(chan, bo, &bo->mem, new_mem);
  853. if (ret == 0) {
  854. ret = nouveau_bo_move_accel_cleanup(chan, nvbo, evict,
  855. no_wait_gpu, new_mem);
  856. }
  857. out:
  858. mutex_unlock(&chan->cli->mutex);
  859. return ret;
  860. }
  861. void
  862. nouveau_bo_move_init(struct nouveau_drm *drm)
  863. {
  864. static const struct {
  865. const char *name;
  866. int engine;
  867. u32 oclass;
  868. int (*exec)(struct nouveau_channel *,
  869. struct ttm_buffer_object *,
  870. struct ttm_mem_reg *, struct ttm_mem_reg *);
  871. int (*init)(struct nouveau_channel *, u32 handle);
  872. } _methods[] = {
  873. { "COPY", 0, 0xa0b5, nve0_bo_move_copy, nve0_bo_move_init },
  874. { "GRCE", 0, 0xa0b5, nve0_bo_move_copy, nvc0_bo_move_init },
  875. { "COPY1", 5, 0x90b8, nvc0_bo_move_copy, nvc0_bo_move_init },
  876. { "COPY0", 4, 0x90b5, nvc0_bo_move_copy, nvc0_bo_move_init },
  877. { "COPY", 0, 0x85b5, nva3_bo_move_copy, nv50_bo_move_init },
  878. { "CRYPT", 0, 0x74c1, nv84_bo_move_exec, nv50_bo_move_init },
  879. { "M2MF", 0, 0x9039, nvc0_bo_move_m2mf, nvc0_bo_move_init },
  880. { "M2MF", 0, 0x5039, nv50_bo_move_m2mf, nv50_bo_move_init },
  881. { "M2MF", 0, 0x0039, nv04_bo_move_m2mf, nv04_bo_move_init },
  882. {},
  883. { "CRYPT", 0, 0x88b4, nv98_bo_move_exec, nv50_bo_move_init },
  884. }, *mthd = _methods;
  885. const char *name = "CPU";
  886. int ret;
  887. do {
  888. struct nouveau_object *object;
  889. struct nouveau_channel *chan;
  890. u32 handle = (mthd->engine << 16) | mthd->oclass;
  891. if (mthd->init == nve0_bo_move_init)
  892. chan = drm->cechan;
  893. else
  894. chan = drm->channel;
  895. if (chan == NULL)
  896. continue;
  897. ret = nouveau_object_new(nv_object(drm), chan->handle, handle,
  898. mthd->oclass, NULL, 0, &object);
  899. if (ret == 0) {
  900. ret = mthd->init(chan, handle);
  901. if (ret) {
  902. nouveau_object_del(nv_object(drm),
  903. chan->handle, handle);
  904. continue;
  905. }
  906. drm->ttm.move = mthd->exec;
  907. drm->ttm.chan = chan;
  908. name = mthd->name;
  909. break;
  910. }
  911. } while ((++mthd)->exec);
  912. NV_INFO(drm, "MM: using %s for buffer copies\n", name);
  913. }
  914. static int
  915. nouveau_bo_move_flipd(struct ttm_buffer_object *bo, bool evict, bool intr,
  916. bool no_wait_gpu, struct ttm_mem_reg *new_mem)
  917. {
  918. u32 placement_memtype = TTM_PL_FLAG_TT | TTM_PL_MASK_CACHING;
  919. struct ttm_placement placement;
  920. struct ttm_mem_reg tmp_mem;
  921. int ret;
  922. placement.fpfn = placement.lpfn = 0;
  923. placement.num_placement = placement.num_busy_placement = 1;
  924. placement.placement = placement.busy_placement = &placement_memtype;
  925. tmp_mem = *new_mem;
  926. tmp_mem.mm_node = NULL;
  927. ret = ttm_bo_mem_space(bo, &placement, &tmp_mem, intr, no_wait_gpu);
  928. if (ret)
  929. return ret;
  930. ret = ttm_tt_bind(bo->ttm, &tmp_mem);
  931. if (ret)
  932. goto out;
  933. ret = nouveau_bo_move_m2mf(bo, true, intr, no_wait_gpu, &tmp_mem);
  934. if (ret)
  935. goto out;
  936. ret = ttm_bo_move_ttm(bo, true, no_wait_gpu, new_mem);
  937. out:
  938. ttm_bo_mem_put(bo, &tmp_mem);
  939. return ret;
  940. }
  941. static int
  942. nouveau_bo_move_flips(struct ttm_buffer_object *bo, bool evict, bool intr,
  943. bool no_wait_gpu, struct ttm_mem_reg *new_mem)
  944. {
  945. u32 placement_memtype = TTM_PL_FLAG_TT | TTM_PL_MASK_CACHING;
  946. struct ttm_placement placement;
  947. struct ttm_mem_reg tmp_mem;
  948. int ret;
  949. placement.fpfn = placement.lpfn = 0;
  950. placement.num_placement = placement.num_busy_placement = 1;
  951. placement.placement = placement.busy_placement = &placement_memtype;
  952. tmp_mem = *new_mem;
  953. tmp_mem.mm_node = NULL;
  954. ret = ttm_bo_mem_space(bo, &placement, &tmp_mem, intr, no_wait_gpu);
  955. if (ret)
  956. return ret;
  957. ret = ttm_bo_move_ttm(bo, true, no_wait_gpu, &tmp_mem);
  958. if (ret)
  959. goto out;
  960. ret = nouveau_bo_move_m2mf(bo, true, intr, no_wait_gpu, new_mem);
  961. if (ret)
  962. goto out;
  963. out:
  964. ttm_bo_mem_put(bo, &tmp_mem);
  965. return ret;
  966. }
  967. static void
  968. nouveau_bo_move_ntfy(struct ttm_buffer_object *bo, struct ttm_mem_reg *new_mem)
  969. {
  970. struct nouveau_bo *nvbo = nouveau_bo(bo);
  971. struct nouveau_vma *vma;
  972. /* ttm can now (stupidly) pass the driver bos it didn't create... */
  973. if (bo->destroy != nouveau_bo_del_ttm)
  974. return;
  975. list_for_each_entry(vma, &nvbo->vma_list, head) {
  976. if (new_mem && new_mem->mem_type == TTM_PL_VRAM) {
  977. nouveau_vm_map(vma, new_mem->mm_node);
  978. } else
  979. if (new_mem && new_mem->mem_type == TTM_PL_TT &&
  980. nvbo->page_shift == vma->vm->vmm->spg_shift) {
  981. if (((struct nouveau_mem *)new_mem->mm_node)->sg)
  982. nouveau_vm_map_sg_table(vma, 0, new_mem->
  983. num_pages << PAGE_SHIFT,
  984. new_mem->mm_node);
  985. else
  986. nouveau_vm_map_sg(vma, 0, new_mem->
  987. num_pages << PAGE_SHIFT,
  988. new_mem->mm_node);
  989. } else {
  990. nouveau_vm_unmap(vma);
  991. }
  992. }
  993. }
  994. static int
  995. nouveau_bo_vm_bind(struct ttm_buffer_object *bo, struct ttm_mem_reg *new_mem,
  996. struct nouveau_drm_tile **new_tile)
  997. {
  998. struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
  999. struct drm_device *dev = drm->dev;
  1000. struct nouveau_bo *nvbo = nouveau_bo(bo);
  1001. u64 offset = new_mem->start << PAGE_SHIFT;
  1002. *new_tile = NULL;
  1003. if (new_mem->mem_type != TTM_PL_VRAM)
  1004. return 0;
  1005. if (nv_device(drm->device)->card_type >= NV_10) {
  1006. *new_tile = nv10_bo_set_tiling(dev, offset, new_mem->size,
  1007. nvbo->tile_mode,
  1008. nvbo->tile_flags);
  1009. }
  1010. return 0;
  1011. }
  1012. static void
  1013. nouveau_bo_vm_cleanup(struct ttm_buffer_object *bo,
  1014. struct nouveau_drm_tile *new_tile,
  1015. struct nouveau_drm_tile **old_tile)
  1016. {
  1017. struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
  1018. struct drm_device *dev = drm->dev;
  1019. nv10_bo_put_tile_region(dev, *old_tile, bo->sync_obj);
  1020. *old_tile = new_tile;
  1021. }
  1022. static int
  1023. nouveau_bo_move(struct ttm_buffer_object *bo, bool evict, bool intr,
  1024. bool no_wait_gpu, struct ttm_mem_reg *new_mem)
  1025. {
  1026. struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
  1027. struct nouveau_bo *nvbo = nouveau_bo(bo);
  1028. struct ttm_mem_reg *old_mem = &bo->mem;
  1029. struct nouveau_drm_tile *new_tile = NULL;
  1030. int ret = 0;
  1031. if (nv_device(drm->device)->card_type < NV_50) {
  1032. ret = nouveau_bo_vm_bind(bo, new_mem, &new_tile);
  1033. if (ret)
  1034. return ret;
  1035. }
  1036. /* Fake bo copy. */
  1037. if (old_mem->mem_type == TTM_PL_SYSTEM && !bo->ttm) {
  1038. BUG_ON(bo->mem.mm_node != NULL);
  1039. bo->mem = *new_mem;
  1040. new_mem->mm_node = NULL;
  1041. goto out;
  1042. }
  1043. /* CPU copy if we have no accelerated method available */
  1044. if (!drm->ttm.move) {
  1045. ret = ttm_bo_move_memcpy(bo, evict, no_wait_gpu, new_mem);
  1046. goto out;
  1047. }
  1048. /* Hardware assisted copy. */
  1049. if (new_mem->mem_type == TTM_PL_SYSTEM)
  1050. ret = nouveau_bo_move_flipd(bo, evict, intr,
  1051. no_wait_gpu, new_mem);
  1052. else if (old_mem->mem_type == TTM_PL_SYSTEM)
  1053. ret = nouveau_bo_move_flips(bo, evict, intr,
  1054. no_wait_gpu, new_mem);
  1055. else
  1056. ret = nouveau_bo_move_m2mf(bo, evict, intr,
  1057. no_wait_gpu, new_mem);
  1058. if (!ret)
  1059. goto out;
  1060. /* Fallback to software copy. */
  1061. ret = ttm_bo_move_memcpy(bo, evict, no_wait_gpu, new_mem);
  1062. out:
  1063. if (nv_device(drm->device)->card_type < NV_50) {
  1064. if (ret)
  1065. nouveau_bo_vm_cleanup(bo, NULL, &new_tile);
  1066. else
  1067. nouveau_bo_vm_cleanup(bo, new_tile, &nvbo->tile);
  1068. }
  1069. return ret;
  1070. }
  1071. static int
  1072. nouveau_bo_verify_access(struct ttm_buffer_object *bo, struct file *filp)
  1073. {
  1074. return 0;
  1075. }
  1076. static int
  1077. nouveau_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
  1078. {
  1079. struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
  1080. struct nouveau_drm *drm = nouveau_bdev(bdev);
  1081. struct drm_device *dev = drm->dev;
  1082. int ret;
  1083. mem->bus.addr = NULL;
  1084. mem->bus.offset = 0;
  1085. mem->bus.size = mem->num_pages << PAGE_SHIFT;
  1086. mem->bus.base = 0;
  1087. mem->bus.is_iomem = false;
  1088. if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
  1089. return -EINVAL;
  1090. switch (mem->mem_type) {
  1091. case TTM_PL_SYSTEM:
  1092. /* System memory */
  1093. return 0;
  1094. case TTM_PL_TT:
  1095. #if __OS_HAS_AGP
  1096. if (drm->agp.stat == ENABLED) {
  1097. mem->bus.offset = mem->start << PAGE_SHIFT;
  1098. mem->bus.base = drm->agp.base;
  1099. mem->bus.is_iomem = !dev->agp->cant_use_aperture;
  1100. }
  1101. #endif
  1102. break;
  1103. case TTM_PL_VRAM:
  1104. mem->bus.offset = mem->start << PAGE_SHIFT;
  1105. mem->bus.base = pci_resource_start(dev->pdev, 1);
  1106. mem->bus.is_iomem = true;
  1107. if (nv_device(drm->device)->card_type >= NV_50) {
  1108. struct nouveau_bar *bar = nouveau_bar(drm->device);
  1109. struct nouveau_mem *node = mem->mm_node;
  1110. ret = bar->umap(bar, node, NV_MEM_ACCESS_RW,
  1111. &node->bar_vma);
  1112. if (ret)
  1113. return ret;
  1114. mem->bus.offset = node->bar_vma.offset;
  1115. }
  1116. break;
  1117. default:
  1118. return -EINVAL;
  1119. }
  1120. return 0;
  1121. }
  1122. static void
  1123. nouveau_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
  1124. {
  1125. struct nouveau_drm *drm = nouveau_bdev(bdev);
  1126. struct nouveau_bar *bar = nouveau_bar(drm->device);
  1127. struct nouveau_mem *node = mem->mm_node;
  1128. if (!node->bar_vma.node)
  1129. return;
  1130. bar->unmap(bar, &node->bar_vma);
  1131. }
  1132. static int
  1133. nouveau_ttm_fault_reserve_notify(struct ttm_buffer_object *bo)
  1134. {
  1135. struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
  1136. struct nouveau_bo *nvbo = nouveau_bo(bo);
  1137. struct nouveau_device *device = nv_device(drm->device);
  1138. u32 mappable = pci_resource_len(device->pdev, 1) >> PAGE_SHIFT;
  1139. /* as long as the bo isn't in vram, and isn't tiled, we've got
  1140. * nothing to do here.
  1141. */
  1142. if (bo->mem.mem_type != TTM_PL_VRAM) {
  1143. if (nv_device(drm->device)->card_type < NV_50 ||
  1144. !nouveau_bo_tile_layout(nvbo))
  1145. return 0;
  1146. }
  1147. /* make sure bo is in mappable vram */
  1148. if (bo->mem.start + bo->mem.num_pages < mappable)
  1149. return 0;
  1150. nvbo->placement.fpfn = 0;
  1151. nvbo->placement.lpfn = mappable;
  1152. nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_VRAM, 0);
  1153. return nouveau_bo_validate(nvbo, false, false);
  1154. }
  1155. static int
  1156. nouveau_ttm_tt_populate(struct ttm_tt *ttm)
  1157. {
  1158. struct ttm_dma_tt *ttm_dma = (void *)ttm;
  1159. struct nouveau_drm *drm;
  1160. struct drm_device *dev;
  1161. unsigned i;
  1162. int r;
  1163. bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);
  1164. if (ttm->state != tt_unpopulated)
  1165. return 0;
  1166. if (slave && ttm->sg) {
  1167. /* make userspace faulting work */
  1168. drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages,
  1169. ttm_dma->dma_address, ttm->num_pages);
  1170. ttm->state = tt_unbound;
  1171. return 0;
  1172. }
  1173. drm = nouveau_bdev(ttm->bdev);
  1174. dev = drm->dev;
  1175. #if __OS_HAS_AGP
  1176. if (drm->agp.stat == ENABLED) {
  1177. return ttm_agp_tt_populate(ttm);
  1178. }
  1179. #endif
  1180. #ifdef CONFIG_SWIOTLB
  1181. if (swiotlb_nr_tbl()) {
  1182. return ttm_dma_populate((void *)ttm, dev->dev);
  1183. }
  1184. #endif
  1185. r = ttm_pool_populate(ttm);
  1186. if (r) {
  1187. return r;
  1188. }
  1189. for (i = 0; i < ttm->num_pages; i++) {
  1190. ttm_dma->dma_address[i] = pci_map_page(dev->pdev, ttm->pages[i],
  1191. 0, PAGE_SIZE,
  1192. PCI_DMA_BIDIRECTIONAL);
  1193. if (pci_dma_mapping_error(dev->pdev, ttm_dma->dma_address[i])) {
  1194. while (--i) {
  1195. pci_unmap_page(dev->pdev, ttm_dma->dma_address[i],
  1196. PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  1197. ttm_dma->dma_address[i] = 0;
  1198. }
  1199. ttm_pool_unpopulate(ttm);
  1200. return -EFAULT;
  1201. }
  1202. }
  1203. return 0;
  1204. }
  1205. static void
  1206. nouveau_ttm_tt_unpopulate(struct ttm_tt *ttm)
  1207. {
  1208. struct ttm_dma_tt *ttm_dma = (void *)ttm;
  1209. struct nouveau_drm *drm;
  1210. struct drm_device *dev;
  1211. unsigned i;
  1212. bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);
  1213. if (slave)
  1214. return;
  1215. drm = nouveau_bdev(ttm->bdev);
  1216. dev = drm->dev;
  1217. #if __OS_HAS_AGP
  1218. if (drm->agp.stat == ENABLED) {
  1219. ttm_agp_tt_unpopulate(ttm);
  1220. return;
  1221. }
  1222. #endif
  1223. #ifdef CONFIG_SWIOTLB
  1224. if (swiotlb_nr_tbl()) {
  1225. ttm_dma_unpopulate((void *)ttm, dev->dev);
  1226. return;
  1227. }
  1228. #endif
  1229. for (i = 0; i < ttm->num_pages; i++) {
  1230. if (ttm_dma->dma_address[i]) {
  1231. pci_unmap_page(dev->pdev, ttm_dma->dma_address[i],
  1232. PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  1233. }
  1234. }
  1235. ttm_pool_unpopulate(ttm);
  1236. }
  1237. void
  1238. nouveau_bo_fence(struct nouveau_bo *nvbo, struct nouveau_fence *fence)
  1239. {
  1240. struct nouveau_fence *old_fence = NULL;
  1241. if (likely(fence))
  1242. nouveau_fence_ref(fence);
  1243. spin_lock(&nvbo->bo.bdev->fence_lock);
  1244. old_fence = nvbo->bo.sync_obj;
  1245. nvbo->bo.sync_obj = fence;
  1246. spin_unlock(&nvbo->bo.bdev->fence_lock);
  1247. nouveau_fence_unref(&old_fence);
  1248. }
  1249. static void
  1250. nouveau_bo_fence_unref(void **sync_obj)
  1251. {
  1252. nouveau_fence_unref((struct nouveau_fence **)sync_obj);
  1253. }
  1254. static void *
  1255. nouveau_bo_fence_ref(void *sync_obj)
  1256. {
  1257. return nouveau_fence_ref(sync_obj);
  1258. }
  1259. static bool
  1260. nouveau_bo_fence_signalled(void *sync_obj)
  1261. {
  1262. return nouveau_fence_done(sync_obj);
  1263. }
  1264. static int
  1265. nouveau_bo_fence_wait(void *sync_obj, bool lazy, bool intr)
  1266. {
  1267. return nouveau_fence_wait(sync_obj, lazy, intr);
  1268. }
  1269. static int
  1270. nouveau_bo_fence_flush(void *sync_obj)
  1271. {
  1272. return 0;
  1273. }
  1274. struct ttm_bo_driver nouveau_bo_driver = {
  1275. .ttm_tt_create = &nouveau_ttm_tt_create,
  1276. .ttm_tt_populate = &nouveau_ttm_tt_populate,
  1277. .ttm_tt_unpopulate = &nouveau_ttm_tt_unpopulate,
  1278. .invalidate_caches = nouveau_bo_invalidate_caches,
  1279. .init_mem_type = nouveau_bo_init_mem_type,
  1280. .evict_flags = nouveau_bo_evict_flags,
  1281. .move_notify = nouveau_bo_move_ntfy,
  1282. .move = nouveau_bo_move,
  1283. .verify_access = nouveau_bo_verify_access,
  1284. .sync_obj_signaled = nouveau_bo_fence_signalled,
  1285. .sync_obj_wait = nouveau_bo_fence_wait,
  1286. .sync_obj_flush = nouveau_bo_fence_flush,
  1287. .sync_obj_unref = nouveau_bo_fence_unref,
  1288. .sync_obj_ref = nouveau_bo_fence_ref,
  1289. .fault_reserve_notify = &nouveau_ttm_fault_reserve_notify,
  1290. .io_mem_reserve = &nouveau_ttm_io_mem_reserve,
  1291. .io_mem_free = &nouveau_ttm_io_mem_free,
  1292. };
  1293. struct nouveau_vma *
  1294. nouveau_bo_vma_find(struct nouveau_bo *nvbo, struct nouveau_vm *vm)
  1295. {
  1296. struct nouveau_vma *vma;
  1297. list_for_each_entry(vma, &nvbo->vma_list, head) {
  1298. if (vma->vm == vm)
  1299. return vma;
  1300. }
  1301. return NULL;
  1302. }
  1303. int
  1304. nouveau_bo_vma_add(struct nouveau_bo *nvbo, struct nouveau_vm *vm,
  1305. struct nouveau_vma *vma)
  1306. {
  1307. const u32 size = nvbo->bo.mem.num_pages << PAGE_SHIFT;
  1308. struct nouveau_mem *node = nvbo->bo.mem.mm_node;
  1309. int ret;
  1310. ret = nouveau_vm_get(vm, size, nvbo->page_shift,
  1311. NV_MEM_ACCESS_RW, vma);
  1312. if (ret)
  1313. return ret;
  1314. if (nvbo->bo.mem.mem_type == TTM_PL_VRAM)
  1315. nouveau_vm_map(vma, nvbo->bo.mem.mm_node);
  1316. else if (nvbo->bo.mem.mem_type == TTM_PL_TT) {
  1317. if (node->sg)
  1318. nouveau_vm_map_sg_table(vma, 0, size, node);
  1319. else
  1320. nouveau_vm_map_sg(vma, 0, size, node);
  1321. }
  1322. list_add_tail(&vma->head, &nvbo->vma_list);
  1323. vma->refcount = 1;
  1324. return 0;
  1325. }
  1326. void
  1327. nouveau_bo_vma_del(struct nouveau_bo *nvbo, struct nouveau_vma *vma)
  1328. {
  1329. if (vma->node) {
  1330. if (nvbo->bo.mem.mem_type != TTM_PL_SYSTEM)
  1331. nouveau_vm_unmap(vma);
  1332. nouveau_vm_put(vma);
  1333. list_del(&vma->head);
  1334. }
  1335. }