nouveau_bo.c 38 KB

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