nouveau_bo.c 39 KB

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