nouveau_bo.c 25 KB

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