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