nouveau_bo.c 41 KB

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