nouveau_bo.c 42 KB

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