nouveau_bo.c 23 KB

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