nouveau_bo.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  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 "drmP.h"
  30. #include "nouveau_drm.h"
  31. #include "nouveau_drv.h"
  32. #include "nouveau_dma.h"
  33. #include <linux/log2.h>
  34. #include <linux/slab.h>
  35. static void
  36. nouveau_bo_del_ttm(struct ttm_buffer_object *bo)
  37. {
  38. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  39. struct drm_device *dev = dev_priv->dev;
  40. struct nouveau_bo *nvbo = nouveau_bo(bo);
  41. if (unlikely(nvbo->gem))
  42. DRM_ERROR("bo %p still attached to GEM object\n", bo);
  43. if (nvbo->tile)
  44. nv10_mem_expire_tiling(dev, nvbo->tile, NULL);
  45. kfree(nvbo);
  46. }
  47. static void
  48. nouveau_bo_fixup_align(struct drm_device *dev,
  49. uint32_t tile_mode, uint32_t tile_flags,
  50. int *align, int *size)
  51. {
  52. struct drm_nouveau_private *dev_priv = dev->dev_private;
  53. /*
  54. * Some of the tile_flags have a periodic structure of N*4096 bytes,
  55. * align to to that as well as the page size. Align the size to the
  56. * appropriate boundaries. This does imply that sizes are rounded up
  57. * 3-7 pages, so be aware of this and do not waste memory by allocating
  58. * many small buffers.
  59. */
  60. if (dev_priv->card_type == NV_50) {
  61. uint32_t block_size = dev_priv->vram_size >> 15;
  62. int i;
  63. switch (tile_flags) {
  64. case 0x1800:
  65. case 0x2800:
  66. case 0x4800:
  67. case 0x7a00:
  68. if (is_power_of_2(block_size)) {
  69. for (i = 1; i < 10; i++) {
  70. *align = 12 * i * block_size;
  71. if (!(*align % 65536))
  72. break;
  73. }
  74. } else {
  75. for (i = 1; i < 10; i++) {
  76. *align = 8 * i * block_size;
  77. if (!(*align % 65536))
  78. break;
  79. }
  80. }
  81. *size = roundup(*size, *align);
  82. break;
  83. default:
  84. break;
  85. }
  86. } else {
  87. if (tile_mode) {
  88. if (dev_priv->chipset >= 0x40) {
  89. *align = 65536;
  90. *size = roundup(*size, 64 * tile_mode);
  91. } else if (dev_priv->chipset >= 0x30) {
  92. *align = 32768;
  93. *size = roundup(*size, 64 * tile_mode);
  94. } else if (dev_priv->chipset >= 0x20) {
  95. *align = 16384;
  96. *size = roundup(*size, 64 * tile_mode);
  97. } else if (dev_priv->chipset >= 0x10) {
  98. *align = 16384;
  99. *size = roundup(*size, 32 * tile_mode);
  100. }
  101. }
  102. }
  103. /* ALIGN works only on powers of two. */
  104. *size = roundup(*size, PAGE_SIZE);
  105. if (dev_priv->card_type == NV_50) {
  106. *size = roundup(*size, 65536);
  107. *align = max(65536, *align);
  108. }
  109. }
  110. int
  111. nouveau_bo_new(struct drm_device *dev, struct nouveau_channel *chan,
  112. int size, int align, uint32_t flags, uint32_t tile_mode,
  113. uint32_t tile_flags, bool no_vm, bool mappable,
  114. struct nouveau_bo **pnvbo)
  115. {
  116. struct drm_nouveau_private *dev_priv = dev->dev_private;
  117. struct nouveau_bo *nvbo;
  118. int ret = 0;
  119. nvbo = kzalloc(sizeof(struct nouveau_bo), GFP_KERNEL);
  120. if (!nvbo)
  121. return -ENOMEM;
  122. INIT_LIST_HEAD(&nvbo->head);
  123. INIT_LIST_HEAD(&nvbo->entry);
  124. nvbo->mappable = mappable;
  125. nvbo->no_vm = no_vm;
  126. nvbo->tile_mode = tile_mode;
  127. nvbo->tile_flags = tile_flags;
  128. nvbo->bo.bdev = &dev_priv->ttm.bdev;
  129. nouveau_bo_fixup_align(dev, tile_mode, nouveau_bo_tile_layout(nvbo),
  130. &align, &size);
  131. align >>= PAGE_SHIFT;
  132. nouveau_bo_placement_set(nvbo, flags, 0);
  133. nvbo->channel = chan;
  134. ret = ttm_bo_init(&dev_priv->ttm.bdev, &nvbo->bo, size,
  135. ttm_bo_type_device, &nvbo->placement, align, 0,
  136. false, NULL, size, nouveau_bo_del_ttm);
  137. if (ret) {
  138. /* ttm will call nouveau_bo_del_ttm if it fails.. */
  139. return ret;
  140. }
  141. nvbo->channel = NULL;
  142. *pnvbo = nvbo;
  143. return 0;
  144. }
  145. static void
  146. set_placement_list(uint32_t *pl, unsigned *n, uint32_t type, uint32_t flags)
  147. {
  148. *n = 0;
  149. if (type & TTM_PL_FLAG_VRAM)
  150. pl[(*n)++] = TTM_PL_FLAG_VRAM | flags;
  151. if (type & TTM_PL_FLAG_TT)
  152. pl[(*n)++] = TTM_PL_FLAG_TT | flags;
  153. if (type & TTM_PL_FLAG_SYSTEM)
  154. pl[(*n)++] = TTM_PL_FLAG_SYSTEM | flags;
  155. }
  156. static void
  157. set_placement_range(struct nouveau_bo *nvbo, uint32_t type)
  158. {
  159. struct drm_nouveau_private *dev_priv = nouveau_bdev(nvbo->bo.bdev);
  160. if (dev_priv->card_type == NV_10 &&
  161. nvbo->tile_mode && (type & TTM_PL_FLAG_VRAM)) {
  162. /*
  163. * Make sure that the color and depth buffers are handled
  164. * by independent memory controller units. Up to a 9x
  165. * speed up when alpha-blending and depth-test are enabled
  166. * at the same time.
  167. */
  168. int vram_pages = dev_priv->vram_size >> PAGE_SHIFT;
  169. if (nvbo->tile_flags & NOUVEAU_GEM_TILE_ZETA) {
  170. nvbo->placement.fpfn = vram_pages / 2;
  171. nvbo->placement.lpfn = ~0;
  172. } else {
  173. nvbo->placement.fpfn = 0;
  174. nvbo->placement.lpfn = vram_pages / 2;
  175. }
  176. }
  177. }
  178. void
  179. nouveau_bo_placement_set(struct nouveau_bo *nvbo, uint32_t type, uint32_t busy)
  180. {
  181. struct ttm_placement *pl = &nvbo->placement;
  182. uint32_t flags = TTM_PL_MASK_CACHING |
  183. (nvbo->pin_refcnt ? TTM_PL_FLAG_NO_EVICT : 0);
  184. pl->placement = nvbo->placements;
  185. set_placement_list(nvbo->placements, &pl->num_placement,
  186. type, flags);
  187. pl->busy_placement = nvbo->busy_placements;
  188. set_placement_list(nvbo->busy_placements, &pl->num_busy_placement,
  189. type | busy, flags);
  190. set_placement_range(nvbo, type);
  191. }
  192. int
  193. nouveau_bo_pin(struct nouveau_bo *nvbo, uint32_t memtype)
  194. {
  195. struct drm_nouveau_private *dev_priv = nouveau_bdev(nvbo->bo.bdev);
  196. struct ttm_buffer_object *bo = &nvbo->bo;
  197. int ret;
  198. if (nvbo->pin_refcnt && !(memtype & (1 << bo->mem.mem_type))) {
  199. NV_ERROR(nouveau_bdev(bo->bdev)->dev,
  200. "bo %p pinned elsewhere: 0x%08x vs 0x%08x\n", bo,
  201. 1 << bo->mem.mem_type, memtype);
  202. return -EINVAL;
  203. }
  204. if (nvbo->pin_refcnt++)
  205. return 0;
  206. ret = ttm_bo_reserve(bo, false, false, false, 0);
  207. if (ret)
  208. goto out;
  209. nouveau_bo_placement_set(nvbo, memtype, 0);
  210. ret = ttm_bo_validate(bo, &nvbo->placement, false, false, false);
  211. if (ret == 0) {
  212. switch (bo->mem.mem_type) {
  213. case TTM_PL_VRAM:
  214. dev_priv->fb_aper_free -= bo->mem.size;
  215. break;
  216. case TTM_PL_TT:
  217. dev_priv->gart_info.aper_free -= bo->mem.size;
  218. break;
  219. default:
  220. break;
  221. }
  222. }
  223. ttm_bo_unreserve(bo);
  224. out:
  225. if (unlikely(ret))
  226. nvbo->pin_refcnt--;
  227. return ret;
  228. }
  229. int
  230. nouveau_bo_unpin(struct nouveau_bo *nvbo)
  231. {
  232. struct drm_nouveau_private *dev_priv = nouveau_bdev(nvbo->bo.bdev);
  233. struct ttm_buffer_object *bo = &nvbo->bo;
  234. int ret;
  235. if (--nvbo->pin_refcnt)
  236. return 0;
  237. ret = ttm_bo_reserve(bo, false, false, false, 0);
  238. if (ret)
  239. return ret;
  240. nouveau_bo_placement_set(nvbo, bo->mem.placement, 0);
  241. ret = ttm_bo_validate(bo, &nvbo->placement, false, false, false);
  242. if (ret == 0) {
  243. switch (bo->mem.mem_type) {
  244. case TTM_PL_VRAM:
  245. dev_priv->fb_aper_free += bo->mem.size;
  246. break;
  247. case TTM_PL_TT:
  248. dev_priv->gart_info.aper_free += bo->mem.size;
  249. break;
  250. default:
  251. break;
  252. }
  253. }
  254. ttm_bo_unreserve(bo);
  255. return ret;
  256. }
  257. int
  258. nouveau_bo_map(struct nouveau_bo *nvbo)
  259. {
  260. int ret;
  261. ret = ttm_bo_reserve(&nvbo->bo, false, false, false, 0);
  262. if (ret)
  263. return ret;
  264. ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.mem.num_pages, &nvbo->kmap);
  265. ttm_bo_unreserve(&nvbo->bo);
  266. return ret;
  267. }
  268. void
  269. nouveau_bo_unmap(struct nouveau_bo *nvbo)
  270. {
  271. if (nvbo)
  272. ttm_bo_kunmap(&nvbo->kmap);
  273. }
  274. u16
  275. nouveau_bo_rd16(struct nouveau_bo *nvbo, unsigned index)
  276. {
  277. bool is_iomem;
  278. u16 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
  279. mem = &mem[index];
  280. if (is_iomem)
  281. return ioread16_native((void __force __iomem *)mem);
  282. else
  283. return *mem;
  284. }
  285. void
  286. nouveau_bo_wr16(struct nouveau_bo *nvbo, unsigned index, u16 val)
  287. {
  288. bool is_iomem;
  289. u16 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
  290. mem = &mem[index];
  291. if (is_iomem)
  292. iowrite16_native(val, (void __force __iomem *)mem);
  293. else
  294. *mem = val;
  295. }
  296. u32
  297. nouveau_bo_rd32(struct nouveau_bo *nvbo, unsigned index)
  298. {
  299. bool is_iomem;
  300. u32 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
  301. mem = &mem[index];
  302. if (is_iomem)
  303. return ioread32_native((void __force __iomem *)mem);
  304. else
  305. return *mem;
  306. }
  307. void
  308. nouveau_bo_wr32(struct nouveau_bo *nvbo, unsigned index, u32 val)
  309. {
  310. bool is_iomem;
  311. u32 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
  312. mem = &mem[index];
  313. if (is_iomem)
  314. iowrite32_native(val, (void __force __iomem *)mem);
  315. else
  316. *mem = val;
  317. }
  318. static struct ttm_backend *
  319. nouveau_bo_create_ttm_backend_entry(struct ttm_bo_device *bdev)
  320. {
  321. struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev);
  322. struct drm_device *dev = dev_priv->dev;
  323. switch (dev_priv->gart_info.type) {
  324. #if __OS_HAS_AGP
  325. case NOUVEAU_GART_AGP:
  326. return ttm_agp_backend_init(bdev, dev->agp->bridge);
  327. #endif
  328. case NOUVEAU_GART_SGDMA:
  329. return nouveau_sgdma_init_ttm(dev);
  330. default:
  331. NV_ERROR(dev, "Unknown GART type %d\n",
  332. dev_priv->gart_info.type);
  333. break;
  334. }
  335. return NULL;
  336. }
  337. static int
  338. nouveau_bo_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags)
  339. {
  340. /* We'll do this from user space. */
  341. return 0;
  342. }
  343. static int
  344. nouveau_bo_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
  345. struct ttm_mem_type_manager *man)
  346. {
  347. struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev);
  348. struct drm_device *dev = dev_priv->dev;
  349. switch (type) {
  350. case TTM_PL_SYSTEM:
  351. man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
  352. man->available_caching = TTM_PL_MASK_CACHING;
  353. man->default_caching = TTM_PL_FLAG_CACHED;
  354. break;
  355. case TTM_PL_VRAM:
  356. man->func = &ttm_bo_manager_func;
  357. man->flags = TTM_MEMTYPE_FLAG_FIXED |
  358. TTM_MEMTYPE_FLAG_MAPPABLE;
  359. man->available_caching = TTM_PL_FLAG_UNCACHED |
  360. TTM_PL_FLAG_WC;
  361. man->default_caching = TTM_PL_FLAG_WC;
  362. if (dev_priv->card_type == NV_50)
  363. man->gpu_offset = 0x40000000;
  364. else
  365. man->gpu_offset = 0;
  366. break;
  367. case TTM_PL_TT:
  368. man->func = &ttm_bo_manager_func;
  369. switch (dev_priv->gart_info.type) {
  370. case NOUVEAU_GART_AGP:
  371. man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
  372. man->available_caching = TTM_PL_FLAG_UNCACHED;
  373. man->default_caching = TTM_PL_FLAG_UNCACHED;
  374. break;
  375. case NOUVEAU_GART_SGDMA:
  376. man->flags = TTM_MEMTYPE_FLAG_MAPPABLE |
  377. TTM_MEMTYPE_FLAG_CMA;
  378. man->available_caching = TTM_PL_MASK_CACHING;
  379. man->default_caching = TTM_PL_FLAG_CACHED;
  380. break;
  381. default:
  382. NV_ERROR(dev, "Unknown GART type: %d\n",
  383. dev_priv->gart_info.type);
  384. return -EINVAL;
  385. }
  386. man->gpu_offset = dev_priv->vm_gart_base;
  387. break;
  388. default:
  389. NV_ERROR(dev, "Unsupported memory type %u\n", (unsigned)type);
  390. return -EINVAL;
  391. }
  392. return 0;
  393. }
  394. static void
  395. nouveau_bo_evict_flags(struct ttm_buffer_object *bo, struct ttm_placement *pl)
  396. {
  397. struct nouveau_bo *nvbo = nouveau_bo(bo);
  398. switch (bo->mem.mem_type) {
  399. case TTM_PL_VRAM:
  400. nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_TT,
  401. TTM_PL_FLAG_SYSTEM);
  402. break;
  403. default:
  404. nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_SYSTEM, 0);
  405. break;
  406. }
  407. *pl = nvbo->placement;
  408. }
  409. /* GPU-assisted copy using NV_MEMORY_TO_MEMORY_FORMAT, can access
  410. * TTM_PL_{VRAM,TT} directly.
  411. */
  412. static int
  413. nouveau_bo_move_accel_cleanup(struct nouveau_channel *chan,
  414. struct nouveau_bo *nvbo, bool evict,
  415. bool no_wait_reserve, bool no_wait_gpu,
  416. struct ttm_mem_reg *new_mem)
  417. {
  418. struct nouveau_fence *fence = NULL;
  419. int ret;
  420. ret = nouveau_fence_new(chan, &fence, true);
  421. if (ret)
  422. return ret;
  423. if (nvbo->channel) {
  424. ret = nouveau_fence_sync(fence, nvbo->channel);
  425. if (ret)
  426. goto out;
  427. }
  428. ret = ttm_bo_move_accel_cleanup(&nvbo->bo, fence, NULL, evict,
  429. no_wait_reserve, no_wait_gpu, new_mem);
  430. out:
  431. nouveau_fence_unref((void *)&fence);
  432. return ret;
  433. }
  434. static inline uint32_t
  435. nouveau_bo_mem_ctxdma(struct ttm_buffer_object *bo,
  436. struct nouveau_channel *chan, struct ttm_mem_reg *mem)
  437. {
  438. struct nouveau_bo *nvbo = nouveau_bo(bo);
  439. if (nvbo->no_vm) {
  440. if (mem->mem_type == TTM_PL_TT)
  441. return NvDmaGART;
  442. return NvDmaVRAM;
  443. }
  444. if (mem->mem_type == TTM_PL_TT)
  445. return chan->gart_handle;
  446. return chan->vram_handle;
  447. }
  448. static int
  449. nv50_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
  450. struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
  451. {
  452. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  453. struct nouveau_bo *nvbo = nouveau_bo(bo);
  454. u64 length = (new_mem->num_pages << PAGE_SHIFT);
  455. u64 src_offset, dst_offset;
  456. int ret;
  457. src_offset = old_mem->start << PAGE_SHIFT;
  458. dst_offset = new_mem->start << PAGE_SHIFT;
  459. if (!nvbo->no_vm) {
  460. if (old_mem->mem_type == TTM_PL_VRAM)
  461. src_offset += dev_priv->vm_vram_base;
  462. else
  463. src_offset += dev_priv->vm_gart_base;
  464. if (new_mem->mem_type == TTM_PL_VRAM)
  465. dst_offset += dev_priv->vm_vram_base;
  466. else
  467. dst_offset += dev_priv->vm_gart_base;
  468. }
  469. ret = RING_SPACE(chan, 3);
  470. if (ret)
  471. return ret;
  472. BEGIN_RING(chan, NvSubM2MF, 0x0184, 2);
  473. OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, old_mem));
  474. OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, new_mem));
  475. while (length) {
  476. u32 amount, stride, height;
  477. amount = min(length, (u64)(4 * 1024 * 1024));
  478. stride = 16 * 4;
  479. height = amount / stride;
  480. if (new_mem->mem_type == TTM_PL_VRAM &&
  481. nouveau_bo_tile_layout(nvbo)) {
  482. ret = RING_SPACE(chan, 8);
  483. if (ret)
  484. return ret;
  485. BEGIN_RING(chan, NvSubM2MF, 0x0200, 7);
  486. OUT_RING (chan, 0);
  487. OUT_RING (chan, 0);
  488. OUT_RING (chan, stride);
  489. OUT_RING (chan, height);
  490. OUT_RING (chan, 1);
  491. OUT_RING (chan, 0);
  492. OUT_RING (chan, 0);
  493. } else {
  494. ret = RING_SPACE(chan, 2);
  495. if (ret)
  496. return ret;
  497. BEGIN_RING(chan, NvSubM2MF, 0x0200, 1);
  498. OUT_RING (chan, 1);
  499. }
  500. if (old_mem->mem_type == TTM_PL_VRAM &&
  501. nouveau_bo_tile_layout(nvbo)) {
  502. ret = RING_SPACE(chan, 8);
  503. if (ret)
  504. return ret;
  505. BEGIN_RING(chan, NvSubM2MF, 0x021c, 7);
  506. OUT_RING (chan, 0);
  507. OUT_RING (chan, 0);
  508. OUT_RING (chan, stride);
  509. OUT_RING (chan, height);
  510. OUT_RING (chan, 1);
  511. OUT_RING (chan, 0);
  512. OUT_RING (chan, 0);
  513. } else {
  514. ret = RING_SPACE(chan, 2);
  515. if (ret)
  516. return ret;
  517. BEGIN_RING(chan, NvSubM2MF, 0x021c, 1);
  518. OUT_RING (chan, 1);
  519. }
  520. ret = RING_SPACE(chan, 14);
  521. if (ret)
  522. return ret;
  523. BEGIN_RING(chan, NvSubM2MF, 0x0238, 2);
  524. OUT_RING (chan, upper_32_bits(src_offset));
  525. OUT_RING (chan, upper_32_bits(dst_offset));
  526. BEGIN_RING(chan, NvSubM2MF, 0x030c, 8);
  527. OUT_RING (chan, lower_32_bits(src_offset));
  528. OUT_RING (chan, lower_32_bits(dst_offset));
  529. OUT_RING (chan, stride);
  530. OUT_RING (chan, stride);
  531. OUT_RING (chan, stride);
  532. OUT_RING (chan, height);
  533. OUT_RING (chan, 0x00000101);
  534. OUT_RING (chan, 0x00000000);
  535. BEGIN_RING(chan, NvSubM2MF, NV_MEMORY_TO_MEMORY_FORMAT_NOP, 1);
  536. OUT_RING (chan, 0);
  537. length -= amount;
  538. src_offset += amount;
  539. dst_offset += amount;
  540. }
  541. return 0;
  542. }
  543. static int
  544. nv04_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
  545. struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
  546. {
  547. u32 src_offset = old_mem->start << PAGE_SHIFT;
  548. u32 dst_offset = new_mem->start << PAGE_SHIFT;
  549. u32 page_count = new_mem->num_pages;
  550. int ret;
  551. ret = RING_SPACE(chan, 3);
  552. if (ret)
  553. return ret;
  554. BEGIN_RING(chan, NvSubM2MF, NV_MEMORY_TO_MEMORY_FORMAT_DMA_SOURCE, 2);
  555. OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, old_mem));
  556. OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, new_mem));
  557. page_count = new_mem->num_pages;
  558. while (page_count) {
  559. int line_count = (page_count > 2047) ? 2047 : page_count;
  560. ret = RING_SPACE(chan, 11);
  561. if (ret)
  562. return ret;
  563. BEGIN_RING(chan, NvSubM2MF,
  564. NV_MEMORY_TO_MEMORY_FORMAT_OFFSET_IN, 8);
  565. OUT_RING (chan, src_offset);
  566. OUT_RING (chan, dst_offset);
  567. OUT_RING (chan, PAGE_SIZE); /* src_pitch */
  568. OUT_RING (chan, PAGE_SIZE); /* dst_pitch */
  569. OUT_RING (chan, PAGE_SIZE); /* line_length */
  570. OUT_RING (chan, line_count);
  571. OUT_RING (chan, 0x00000101);
  572. OUT_RING (chan, 0x00000000);
  573. BEGIN_RING(chan, NvSubM2MF, NV_MEMORY_TO_MEMORY_FORMAT_NOP, 1);
  574. OUT_RING (chan, 0);
  575. page_count -= line_count;
  576. src_offset += (PAGE_SIZE * line_count);
  577. dst_offset += (PAGE_SIZE * line_count);
  578. }
  579. return 0;
  580. }
  581. static int
  582. nouveau_bo_move_m2mf(struct ttm_buffer_object *bo, int evict, bool intr,
  583. bool no_wait_reserve, bool no_wait_gpu,
  584. struct ttm_mem_reg *new_mem)
  585. {
  586. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  587. struct nouveau_bo *nvbo = nouveau_bo(bo);
  588. struct nouveau_channel *chan;
  589. int ret;
  590. chan = nvbo->channel;
  591. if (!chan || nvbo->no_vm)
  592. chan = dev_priv->channel;
  593. if (dev_priv->card_type < NV_50)
  594. ret = nv04_bo_move_m2mf(chan, bo, &bo->mem, new_mem);
  595. else
  596. ret = nv50_bo_move_m2mf(chan, bo, &bo->mem, new_mem);
  597. if (ret)
  598. return ret;
  599. return nouveau_bo_move_accel_cleanup(chan, nvbo, evict, no_wait_reserve, no_wait_gpu, new_mem);
  600. }
  601. static int
  602. nouveau_bo_move_flipd(struct ttm_buffer_object *bo, bool evict, bool intr,
  603. bool no_wait_reserve, bool no_wait_gpu,
  604. struct ttm_mem_reg *new_mem)
  605. {
  606. u32 placement_memtype = TTM_PL_FLAG_TT | TTM_PL_MASK_CACHING;
  607. struct ttm_placement placement;
  608. struct ttm_mem_reg tmp_mem;
  609. int ret;
  610. placement.fpfn = placement.lpfn = 0;
  611. placement.num_placement = placement.num_busy_placement = 1;
  612. placement.placement = placement.busy_placement = &placement_memtype;
  613. tmp_mem = *new_mem;
  614. tmp_mem.mm_node = NULL;
  615. ret = ttm_bo_mem_space(bo, &placement, &tmp_mem, intr, no_wait_reserve, no_wait_gpu);
  616. if (ret)
  617. return ret;
  618. ret = ttm_tt_bind(bo->ttm, &tmp_mem);
  619. if (ret)
  620. goto out;
  621. ret = nouveau_bo_move_m2mf(bo, true, intr, no_wait_reserve, no_wait_gpu, &tmp_mem);
  622. if (ret)
  623. goto out;
  624. ret = ttm_bo_move_ttm(bo, evict, no_wait_reserve, no_wait_gpu, new_mem);
  625. out:
  626. ttm_bo_mem_put(bo, &tmp_mem);
  627. return ret;
  628. }
  629. static int
  630. nouveau_bo_move_flips(struct ttm_buffer_object *bo, bool evict, bool intr,
  631. bool no_wait_reserve, bool no_wait_gpu,
  632. struct ttm_mem_reg *new_mem)
  633. {
  634. u32 placement_memtype = TTM_PL_FLAG_TT | TTM_PL_MASK_CACHING;
  635. struct ttm_placement placement;
  636. struct ttm_mem_reg tmp_mem;
  637. int ret;
  638. placement.fpfn = placement.lpfn = 0;
  639. placement.num_placement = placement.num_busy_placement = 1;
  640. placement.placement = placement.busy_placement = &placement_memtype;
  641. tmp_mem = *new_mem;
  642. tmp_mem.mm_node = NULL;
  643. ret = ttm_bo_mem_space(bo, &placement, &tmp_mem, intr, no_wait_reserve, no_wait_gpu);
  644. if (ret)
  645. return ret;
  646. ret = ttm_bo_move_ttm(bo, evict, no_wait_reserve, no_wait_gpu, &tmp_mem);
  647. if (ret)
  648. goto out;
  649. ret = nouveau_bo_move_m2mf(bo, evict, intr, no_wait_reserve, no_wait_gpu, new_mem);
  650. if (ret)
  651. goto out;
  652. out:
  653. ttm_bo_mem_put(bo, &tmp_mem);
  654. return ret;
  655. }
  656. static int
  657. nouveau_bo_vm_bind(struct ttm_buffer_object *bo, struct ttm_mem_reg *new_mem,
  658. struct nouveau_tile_reg **new_tile)
  659. {
  660. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  661. struct drm_device *dev = dev_priv->dev;
  662. struct nouveau_bo *nvbo = nouveau_bo(bo);
  663. uint64_t offset;
  664. int ret;
  665. if (nvbo->no_vm || new_mem->mem_type != TTM_PL_VRAM) {
  666. /* Nothing to do. */
  667. *new_tile = NULL;
  668. return 0;
  669. }
  670. offset = new_mem->start << PAGE_SHIFT;
  671. if (dev_priv->card_type == NV_50) {
  672. ret = nv50_mem_vm_bind_linear(dev,
  673. offset + dev_priv->vm_vram_base,
  674. new_mem->size,
  675. nouveau_bo_tile_layout(nvbo),
  676. offset);
  677. if (ret)
  678. return ret;
  679. } else if (dev_priv->card_type >= NV_10) {
  680. *new_tile = nv10_mem_set_tiling(dev, offset, new_mem->size,
  681. nvbo->tile_mode);
  682. }
  683. return 0;
  684. }
  685. static void
  686. nouveau_bo_vm_cleanup(struct ttm_buffer_object *bo,
  687. struct nouveau_tile_reg *new_tile,
  688. struct nouveau_tile_reg **old_tile)
  689. {
  690. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  691. struct drm_device *dev = dev_priv->dev;
  692. if (dev_priv->card_type >= NV_10 &&
  693. dev_priv->card_type < NV_50) {
  694. if (*old_tile)
  695. nv10_mem_expire_tiling(dev, *old_tile, bo->sync_obj);
  696. *old_tile = new_tile;
  697. }
  698. }
  699. static int
  700. nouveau_bo_move(struct ttm_buffer_object *bo, bool evict, bool intr,
  701. bool no_wait_reserve, bool no_wait_gpu,
  702. struct ttm_mem_reg *new_mem)
  703. {
  704. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  705. struct nouveau_bo *nvbo = nouveau_bo(bo);
  706. struct ttm_mem_reg *old_mem = &bo->mem;
  707. struct nouveau_tile_reg *new_tile = NULL;
  708. int ret = 0;
  709. ret = nouveau_bo_vm_bind(bo, new_mem, &new_tile);
  710. if (ret)
  711. return ret;
  712. /* Fake bo copy. */
  713. if (old_mem->mem_type == TTM_PL_SYSTEM && !bo->ttm) {
  714. BUG_ON(bo->mem.mm_node != NULL);
  715. bo->mem = *new_mem;
  716. new_mem->mm_node = NULL;
  717. goto out;
  718. }
  719. /* Software copy if the card isn't up and running yet. */
  720. if (!dev_priv->channel) {
  721. ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, no_wait_gpu, new_mem);
  722. goto out;
  723. }
  724. /* Hardware assisted copy. */
  725. if (new_mem->mem_type == TTM_PL_SYSTEM)
  726. ret = nouveau_bo_move_flipd(bo, evict, intr, no_wait_reserve, no_wait_gpu, new_mem);
  727. else if (old_mem->mem_type == TTM_PL_SYSTEM)
  728. ret = nouveau_bo_move_flips(bo, evict, intr, no_wait_reserve, no_wait_gpu, new_mem);
  729. else
  730. ret = nouveau_bo_move_m2mf(bo, evict, intr, no_wait_reserve, no_wait_gpu, new_mem);
  731. if (!ret)
  732. goto out;
  733. /* Fallback to software copy. */
  734. ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, no_wait_gpu, new_mem);
  735. out:
  736. if (ret)
  737. nouveau_bo_vm_cleanup(bo, NULL, &new_tile);
  738. else
  739. nouveau_bo_vm_cleanup(bo, new_tile, &nvbo->tile);
  740. return ret;
  741. }
  742. static int
  743. nouveau_bo_verify_access(struct ttm_buffer_object *bo, struct file *filp)
  744. {
  745. return 0;
  746. }
  747. static int
  748. nouveau_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
  749. {
  750. struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
  751. struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev);
  752. struct drm_device *dev = dev_priv->dev;
  753. mem->bus.addr = NULL;
  754. mem->bus.offset = 0;
  755. mem->bus.size = mem->num_pages << PAGE_SHIFT;
  756. mem->bus.base = 0;
  757. mem->bus.is_iomem = false;
  758. if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
  759. return -EINVAL;
  760. switch (mem->mem_type) {
  761. case TTM_PL_SYSTEM:
  762. /* System memory */
  763. return 0;
  764. case TTM_PL_TT:
  765. #if __OS_HAS_AGP
  766. if (dev_priv->gart_info.type == NOUVEAU_GART_AGP) {
  767. mem->bus.offset = mem->start << PAGE_SHIFT;
  768. mem->bus.base = dev_priv->gart_info.aper_base;
  769. mem->bus.is_iomem = true;
  770. }
  771. #endif
  772. break;
  773. case TTM_PL_VRAM:
  774. mem->bus.offset = mem->start << PAGE_SHIFT;
  775. mem->bus.base = pci_resource_start(dev->pdev, 1);
  776. mem->bus.is_iomem = true;
  777. break;
  778. default:
  779. return -EINVAL;
  780. }
  781. return 0;
  782. }
  783. static void
  784. nouveau_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
  785. {
  786. }
  787. static int
  788. nouveau_ttm_fault_reserve_notify(struct ttm_buffer_object *bo)
  789. {
  790. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  791. struct nouveau_bo *nvbo = nouveau_bo(bo);
  792. /* as long as the bo isn't in vram, and isn't tiled, we've got
  793. * nothing to do here.
  794. */
  795. if (bo->mem.mem_type != TTM_PL_VRAM) {
  796. if (dev_priv->card_type < NV_50 ||
  797. !nouveau_bo_tile_layout(nvbo))
  798. return 0;
  799. }
  800. /* make sure bo is in mappable vram */
  801. if (bo->mem.start + bo->mem.num_pages < dev_priv->fb_mappable_pages)
  802. return 0;
  803. nvbo->placement.fpfn = 0;
  804. nvbo->placement.lpfn = dev_priv->fb_mappable_pages;
  805. nouveau_bo_placement_set(nvbo, TTM_PL_VRAM, 0);
  806. return ttm_bo_validate(bo, &nvbo->placement, false, true, false);
  807. }
  808. struct ttm_bo_driver nouveau_bo_driver = {
  809. .create_ttm_backend_entry = nouveau_bo_create_ttm_backend_entry,
  810. .invalidate_caches = nouveau_bo_invalidate_caches,
  811. .init_mem_type = nouveau_bo_init_mem_type,
  812. .evict_flags = nouveau_bo_evict_flags,
  813. .move = nouveau_bo_move,
  814. .verify_access = nouveau_bo_verify_access,
  815. .sync_obj_signaled = nouveau_fence_signalled,
  816. .sync_obj_wait = nouveau_fence_wait,
  817. .sync_obj_flush = nouveau_fence_flush,
  818. .sync_obj_unref = nouveau_fence_unref,
  819. .sync_obj_ref = nouveau_fence_ref,
  820. .fault_reserve_notify = &nouveau_ttm_fault_reserve_notify,
  821. .io_mem_reserve = &nouveau_ttm_io_mem_reserve,
  822. .io_mem_free = &nouveau_ttm_io_mem_free,
  823. };