nouveau_bo.c 21 KB

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