nouveau_bo.c 39 KB

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