nouveau_bo.c 39 KB

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