nouveau_bo.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  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. if (nvbo->vma.node) {
  47. nouveau_vm_unmap(&nvbo->vma);
  48. nouveau_vm_put(&nvbo->vma);
  49. }
  50. kfree(nvbo);
  51. }
  52. static void
  53. nouveau_bo_fixup_align(struct nouveau_bo *nvbo, int *align, int *size,
  54. int *page_shift)
  55. {
  56. struct drm_nouveau_private *dev_priv = nouveau_bdev(nvbo->bo.bdev);
  57. if (dev_priv->card_type < NV_50) {
  58. if (nvbo->tile_mode) {
  59. if (dev_priv->chipset >= 0x40) {
  60. *align = 65536;
  61. *size = roundup(*size, 64 * nvbo->tile_mode);
  62. } else if (dev_priv->chipset >= 0x30) {
  63. *align = 32768;
  64. *size = roundup(*size, 64 * nvbo->tile_mode);
  65. } else if (dev_priv->chipset >= 0x20) {
  66. *align = 16384;
  67. *size = roundup(*size, 64 * nvbo->tile_mode);
  68. } else if (dev_priv->chipset >= 0x10) {
  69. *align = 16384;
  70. *size = roundup(*size, 32 * nvbo->tile_mode);
  71. }
  72. }
  73. } else {
  74. if (likely(dev_priv->chan_vm)) {
  75. if (*size > 256 * 1024)
  76. *page_shift = dev_priv->chan_vm->lpg_shift;
  77. else
  78. *page_shift = dev_priv->chan_vm->spg_shift;
  79. } else {
  80. *page_shift = 12;
  81. }
  82. *size = roundup(*size, (1 << *page_shift));
  83. *align = max((1 << *page_shift), *align);
  84. }
  85. *size = roundup(*size, PAGE_SIZE);
  86. }
  87. int
  88. nouveau_bo_new(struct drm_device *dev, struct nouveau_channel *chan,
  89. int size, int align, uint32_t flags, uint32_t tile_mode,
  90. uint32_t tile_flags, bool no_vm, bool mappable,
  91. struct nouveau_bo **pnvbo)
  92. {
  93. struct drm_nouveau_private *dev_priv = dev->dev_private;
  94. struct nouveau_bo *nvbo;
  95. int ret = 0, page_shift = 0;
  96. nvbo = kzalloc(sizeof(struct nouveau_bo), GFP_KERNEL);
  97. if (!nvbo)
  98. return -ENOMEM;
  99. INIT_LIST_HEAD(&nvbo->head);
  100. INIT_LIST_HEAD(&nvbo->entry);
  101. nvbo->mappable = mappable;
  102. nvbo->no_vm = no_vm;
  103. nvbo->tile_mode = tile_mode;
  104. nvbo->tile_flags = tile_flags;
  105. nvbo->bo.bdev = &dev_priv->ttm.bdev;
  106. nouveau_bo_fixup_align(nvbo, &align, &size, &page_shift);
  107. align >>= PAGE_SHIFT;
  108. if (!nvbo->no_vm && dev_priv->chan_vm) {
  109. ret = nouveau_vm_get(dev_priv->chan_vm, size, page_shift,
  110. NV_MEM_ACCESS_RW, &nvbo->vma);
  111. if (ret) {
  112. kfree(nvbo);
  113. return ret;
  114. }
  115. }
  116. nvbo->bo.mem.num_pages = size >> PAGE_SHIFT;
  117. nouveau_bo_placement_set(nvbo, flags, 0);
  118. nvbo->channel = chan;
  119. ret = ttm_bo_init(&dev_priv->ttm.bdev, &nvbo->bo, size,
  120. ttm_bo_type_device, &nvbo->placement, align, 0,
  121. false, NULL, size, nouveau_bo_del_ttm);
  122. if (ret) {
  123. /* ttm will call nouveau_bo_del_ttm if it fails.. */
  124. return ret;
  125. }
  126. nvbo->channel = NULL;
  127. if (nvbo->vma.node) {
  128. if (nvbo->bo.mem.mem_type == TTM_PL_VRAM)
  129. nvbo->bo.offset = nvbo->vma.offset;
  130. }
  131. *pnvbo = nvbo;
  132. return 0;
  133. }
  134. static void
  135. set_placement_list(uint32_t *pl, unsigned *n, uint32_t type, uint32_t flags)
  136. {
  137. *n = 0;
  138. if (type & TTM_PL_FLAG_VRAM)
  139. pl[(*n)++] = TTM_PL_FLAG_VRAM | flags;
  140. if (type & TTM_PL_FLAG_TT)
  141. pl[(*n)++] = TTM_PL_FLAG_TT | flags;
  142. if (type & TTM_PL_FLAG_SYSTEM)
  143. pl[(*n)++] = TTM_PL_FLAG_SYSTEM | flags;
  144. }
  145. static void
  146. set_placement_range(struct nouveau_bo *nvbo, uint32_t type)
  147. {
  148. struct drm_nouveau_private *dev_priv = nouveau_bdev(nvbo->bo.bdev);
  149. int vram_pages = dev_priv->vram_size >> PAGE_SHIFT;
  150. if (dev_priv->card_type == NV_10 &&
  151. nvbo->tile_mode && (type & TTM_PL_FLAG_VRAM) &&
  152. nvbo->bo.mem.num_pages < vram_pages / 2) {
  153. /*
  154. * Make sure that the color and depth buffers are handled
  155. * by independent memory controller units. Up to a 9x
  156. * speed up when alpha-blending and depth-test are enabled
  157. * at the same time.
  158. */
  159. if (nvbo->tile_flags & NOUVEAU_GEM_TILE_ZETA) {
  160. nvbo->placement.fpfn = vram_pages / 2;
  161. nvbo->placement.lpfn = ~0;
  162. } else {
  163. nvbo->placement.fpfn = 0;
  164. nvbo->placement.lpfn = vram_pages / 2;
  165. }
  166. }
  167. }
  168. void
  169. nouveau_bo_placement_set(struct nouveau_bo *nvbo, uint32_t type, uint32_t busy)
  170. {
  171. struct ttm_placement *pl = &nvbo->placement;
  172. uint32_t flags = TTM_PL_MASK_CACHING |
  173. (nvbo->pin_refcnt ? TTM_PL_FLAG_NO_EVICT : 0);
  174. pl->placement = nvbo->placements;
  175. set_placement_list(nvbo->placements, &pl->num_placement,
  176. type, flags);
  177. pl->busy_placement = nvbo->busy_placements;
  178. set_placement_list(nvbo->busy_placements, &pl->num_busy_placement,
  179. type | busy, flags);
  180. set_placement_range(nvbo, type);
  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 = nouveau_bo_validate(nvbo, 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 = nouveau_bo_validate(nvbo, 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. if (nvbo)
  262. ttm_bo_kunmap(&nvbo->kmap);
  263. }
  264. int
  265. nouveau_bo_validate(struct nouveau_bo *nvbo, bool interruptible,
  266. bool no_wait_reserve, bool no_wait_gpu)
  267. {
  268. int ret;
  269. ret = ttm_bo_validate(&nvbo->bo, &nvbo->placement, interruptible,
  270. no_wait_reserve, no_wait_gpu);
  271. if (ret)
  272. return ret;
  273. if (nvbo->vma.node) {
  274. if (nvbo->bo.mem.mem_type == TTM_PL_VRAM)
  275. nvbo->bo.offset = nvbo->vma.offset;
  276. }
  277. return 0;
  278. }
  279. u16
  280. nouveau_bo_rd16(struct nouveau_bo *nvbo, unsigned index)
  281. {
  282. bool is_iomem;
  283. u16 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
  284. mem = &mem[index];
  285. if (is_iomem)
  286. return ioread16_native((void __force __iomem *)mem);
  287. else
  288. return *mem;
  289. }
  290. void
  291. nouveau_bo_wr16(struct nouveau_bo *nvbo, unsigned index, u16 val)
  292. {
  293. bool is_iomem;
  294. u16 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
  295. mem = &mem[index];
  296. if (is_iomem)
  297. iowrite16_native(val, (void __force __iomem *)mem);
  298. else
  299. *mem = val;
  300. }
  301. u32
  302. nouveau_bo_rd32(struct nouveau_bo *nvbo, unsigned index)
  303. {
  304. bool is_iomem;
  305. u32 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
  306. mem = &mem[index];
  307. if (is_iomem)
  308. return ioread32_native((void __force __iomem *)mem);
  309. else
  310. return *mem;
  311. }
  312. void
  313. nouveau_bo_wr32(struct nouveau_bo *nvbo, unsigned index, u32 val)
  314. {
  315. bool is_iomem;
  316. u32 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
  317. mem = &mem[index];
  318. if (is_iomem)
  319. iowrite32_native(val, (void __force __iomem *)mem);
  320. else
  321. *mem = val;
  322. }
  323. static struct ttm_backend *
  324. nouveau_bo_create_ttm_backend_entry(struct ttm_bo_device *bdev)
  325. {
  326. struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev);
  327. struct drm_device *dev = dev_priv->dev;
  328. switch (dev_priv->gart_info.type) {
  329. #if __OS_HAS_AGP
  330. case NOUVEAU_GART_AGP:
  331. return ttm_agp_backend_init(bdev, dev->agp->bridge);
  332. #endif
  333. case NOUVEAU_GART_SGDMA:
  334. return nouveau_sgdma_init_ttm(dev);
  335. default:
  336. NV_ERROR(dev, "Unknown GART type %d\n",
  337. dev_priv->gart_info.type);
  338. break;
  339. }
  340. return NULL;
  341. }
  342. static int
  343. nouveau_bo_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags)
  344. {
  345. /* We'll do this from user space. */
  346. return 0;
  347. }
  348. static int
  349. nouveau_bo_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
  350. struct ttm_mem_type_manager *man)
  351. {
  352. struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev);
  353. struct drm_device *dev = dev_priv->dev;
  354. switch (type) {
  355. case TTM_PL_SYSTEM:
  356. man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
  357. man->available_caching = TTM_PL_MASK_CACHING;
  358. man->default_caching = TTM_PL_FLAG_CACHED;
  359. break;
  360. case TTM_PL_VRAM:
  361. if (dev_priv->card_type >= NV_50) {
  362. man->func = &nouveau_vram_manager;
  363. man->io_reserve_fastpath = false;
  364. man->use_io_reserve_lru = true;
  365. } else {
  366. man->func = &ttm_bo_manager_func;
  367. }
  368. man->flags = TTM_MEMTYPE_FLAG_FIXED |
  369. TTM_MEMTYPE_FLAG_MAPPABLE;
  370. man->available_caching = TTM_PL_FLAG_UNCACHED |
  371. TTM_PL_FLAG_WC;
  372. man->default_caching = TTM_PL_FLAG_WC;
  373. break;
  374. case TTM_PL_TT:
  375. man->func = &ttm_bo_manager_func;
  376. switch (dev_priv->gart_info.type) {
  377. case NOUVEAU_GART_AGP:
  378. man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
  379. man->available_caching = TTM_PL_FLAG_UNCACHED |
  380. TTM_PL_FLAG_WC;
  381. man->default_caching = TTM_PL_FLAG_WC;
  382. break;
  383. case NOUVEAU_GART_SGDMA:
  384. man->flags = TTM_MEMTYPE_FLAG_MAPPABLE |
  385. TTM_MEMTYPE_FLAG_CMA;
  386. man->available_caching = TTM_PL_MASK_CACHING;
  387. man->default_caching = TTM_PL_FLAG_CACHED;
  388. man->gpu_offset = dev_priv->gart_info.aper_base;
  389. break;
  390. default:
  391. NV_ERROR(dev, "Unknown GART type: %d\n",
  392. dev_priv->gart_info.type);
  393. return -EINVAL;
  394. }
  395. break;
  396. default:
  397. NV_ERROR(dev, "Unsupported memory type %u\n", (unsigned)type);
  398. return -EINVAL;
  399. }
  400. return 0;
  401. }
  402. static void
  403. nouveau_bo_evict_flags(struct ttm_buffer_object *bo, struct ttm_placement *pl)
  404. {
  405. struct nouveau_bo *nvbo = nouveau_bo(bo);
  406. switch (bo->mem.mem_type) {
  407. case TTM_PL_VRAM:
  408. nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_TT,
  409. TTM_PL_FLAG_SYSTEM);
  410. break;
  411. default:
  412. nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_SYSTEM, 0);
  413. break;
  414. }
  415. *pl = nvbo->placement;
  416. }
  417. /* GPU-assisted copy using NV_MEMORY_TO_MEMORY_FORMAT, can access
  418. * TTM_PL_{VRAM,TT} directly.
  419. */
  420. static int
  421. nouveau_bo_move_accel_cleanup(struct nouveau_channel *chan,
  422. struct nouveau_bo *nvbo, bool evict,
  423. bool no_wait_reserve, bool no_wait_gpu,
  424. struct ttm_mem_reg *new_mem)
  425. {
  426. struct nouveau_fence *fence = NULL;
  427. int ret;
  428. ret = nouveau_fence_new(chan, &fence, true);
  429. if (ret)
  430. return ret;
  431. ret = ttm_bo_move_accel_cleanup(&nvbo->bo, fence, NULL, evict,
  432. no_wait_reserve, no_wait_gpu, new_mem);
  433. nouveau_fence_unref(&fence);
  434. return ret;
  435. }
  436. static inline uint32_t
  437. nouveau_bo_mem_ctxdma(struct ttm_buffer_object *bo,
  438. struct nouveau_channel *chan, struct ttm_mem_reg *mem)
  439. {
  440. struct nouveau_bo *nvbo = nouveau_bo(bo);
  441. if (nvbo->no_vm) {
  442. if (mem->mem_type == TTM_PL_TT)
  443. return NvDmaGART;
  444. return NvDmaVRAM;
  445. }
  446. if (mem->mem_type == TTM_PL_TT)
  447. return chan->gart_handle;
  448. return chan->vram_handle;
  449. }
  450. static int
  451. nvc0_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
  452. struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
  453. {
  454. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  455. struct nouveau_bo *nvbo = nouveau_bo(bo);
  456. u64 src_offset = old_mem->start << PAGE_SHIFT;
  457. u64 dst_offset = new_mem->start << PAGE_SHIFT;
  458. u32 page_count = new_mem->num_pages;
  459. int ret;
  460. if (!nvbo->no_vm) {
  461. if (old_mem->mem_type == TTM_PL_VRAM)
  462. src_offset = nvbo->vma.offset;
  463. else
  464. src_offset += dev_priv->gart_info.aper_base;
  465. if (new_mem->mem_type == TTM_PL_VRAM)
  466. dst_offset = nvbo->vma.offset;
  467. else
  468. dst_offset += dev_priv->gart_info.aper_base;
  469. }
  470. page_count = new_mem->num_pages;
  471. while (page_count) {
  472. int line_count = (page_count > 2047) ? 2047 : page_count;
  473. ret = RING_SPACE(chan, 12);
  474. if (ret)
  475. return ret;
  476. BEGIN_NVC0(chan, 2, NvSubM2MF, 0x0238, 2);
  477. OUT_RING (chan, upper_32_bits(dst_offset));
  478. OUT_RING (chan, lower_32_bits(dst_offset));
  479. BEGIN_NVC0(chan, 2, NvSubM2MF, 0x030c, 6);
  480. OUT_RING (chan, upper_32_bits(src_offset));
  481. OUT_RING (chan, lower_32_bits(src_offset));
  482. OUT_RING (chan, PAGE_SIZE); /* src_pitch */
  483. OUT_RING (chan, PAGE_SIZE); /* dst_pitch */
  484. OUT_RING (chan, PAGE_SIZE); /* line_length */
  485. OUT_RING (chan, line_count);
  486. BEGIN_NVC0(chan, 2, NvSubM2MF, 0x0300, 1);
  487. OUT_RING (chan, 0x00100110);
  488. page_count -= line_count;
  489. src_offset += (PAGE_SIZE * line_count);
  490. dst_offset += (PAGE_SIZE * line_count);
  491. }
  492. return 0;
  493. }
  494. static int
  495. nv50_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
  496. struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
  497. {
  498. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  499. struct nouveau_bo *nvbo = nouveau_bo(bo);
  500. u64 length = (new_mem->num_pages << PAGE_SHIFT);
  501. u64 src_offset, dst_offset;
  502. int ret;
  503. src_offset = old_mem->start << PAGE_SHIFT;
  504. dst_offset = new_mem->start << PAGE_SHIFT;
  505. if (!nvbo->no_vm) {
  506. if (old_mem->mem_type == TTM_PL_VRAM)
  507. src_offset = nvbo->vma.offset;
  508. else
  509. src_offset += dev_priv->gart_info.aper_base;
  510. if (new_mem->mem_type == TTM_PL_VRAM)
  511. dst_offset = nvbo->vma.offset;
  512. else
  513. dst_offset += dev_priv->gart_info.aper_base;
  514. }
  515. ret = RING_SPACE(chan, 3);
  516. if (ret)
  517. return ret;
  518. BEGIN_RING(chan, NvSubM2MF, 0x0184, 2);
  519. OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, old_mem));
  520. OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, new_mem));
  521. while (length) {
  522. u32 amount, stride, height;
  523. amount = min(length, (u64)(4 * 1024 * 1024));
  524. stride = 16 * 4;
  525. height = amount / stride;
  526. if (new_mem->mem_type == TTM_PL_VRAM &&
  527. nouveau_bo_tile_layout(nvbo)) {
  528. ret = RING_SPACE(chan, 8);
  529. if (ret)
  530. return ret;
  531. BEGIN_RING(chan, NvSubM2MF, 0x0200, 7);
  532. OUT_RING (chan, 0);
  533. OUT_RING (chan, 0);
  534. OUT_RING (chan, stride);
  535. OUT_RING (chan, height);
  536. OUT_RING (chan, 1);
  537. OUT_RING (chan, 0);
  538. OUT_RING (chan, 0);
  539. } else {
  540. ret = RING_SPACE(chan, 2);
  541. if (ret)
  542. return ret;
  543. BEGIN_RING(chan, NvSubM2MF, 0x0200, 1);
  544. OUT_RING (chan, 1);
  545. }
  546. if (old_mem->mem_type == TTM_PL_VRAM &&
  547. nouveau_bo_tile_layout(nvbo)) {
  548. ret = RING_SPACE(chan, 8);
  549. if (ret)
  550. return ret;
  551. BEGIN_RING(chan, NvSubM2MF, 0x021c, 7);
  552. OUT_RING (chan, 0);
  553. OUT_RING (chan, 0);
  554. OUT_RING (chan, stride);
  555. OUT_RING (chan, height);
  556. OUT_RING (chan, 1);
  557. OUT_RING (chan, 0);
  558. OUT_RING (chan, 0);
  559. } else {
  560. ret = RING_SPACE(chan, 2);
  561. if (ret)
  562. return ret;
  563. BEGIN_RING(chan, NvSubM2MF, 0x021c, 1);
  564. OUT_RING (chan, 1);
  565. }
  566. ret = RING_SPACE(chan, 14);
  567. if (ret)
  568. return ret;
  569. BEGIN_RING(chan, NvSubM2MF, 0x0238, 2);
  570. OUT_RING (chan, upper_32_bits(src_offset));
  571. OUT_RING (chan, upper_32_bits(dst_offset));
  572. BEGIN_RING(chan, NvSubM2MF, 0x030c, 8);
  573. OUT_RING (chan, lower_32_bits(src_offset));
  574. OUT_RING (chan, lower_32_bits(dst_offset));
  575. OUT_RING (chan, stride);
  576. OUT_RING (chan, stride);
  577. OUT_RING (chan, stride);
  578. OUT_RING (chan, height);
  579. OUT_RING (chan, 0x00000101);
  580. OUT_RING (chan, 0x00000000);
  581. BEGIN_RING(chan, NvSubM2MF, NV_MEMORY_TO_MEMORY_FORMAT_NOP, 1);
  582. OUT_RING (chan, 0);
  583. length -= amount;
  584. src_offset += amount;
  585. dst_offset += amount;
  586. }
  587. return 0;
  588. }
  589. static int
  590. nv04_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
  591. struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
  592. {
  593. u32 src_offset = old_mem->start << PAGE_SHIFT;
  594. u32 dst_offset = new_mem->start << PAGE_SHIFT;
  595. u32 page_count = new_mem->num_pages;
  596. int ret;
  597. ret = RING_SPACE(chan, 3);
  598. if (ret)
  599. return ret;
  600. BEGIN_RING(chan, NvSubM2MF, NV_MEMORY_TO_MEMORY_FORMAT_DMA_SOURCE, 2);
  601. OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, old_mem));
  602. OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, new_mem));
  603. page_count = new_mem->num_pages;
  604. while (page_count) {
  605. int line_count = (page_count > 2047) ? 2047 : page_count;
  606. ret = RING_SPACE(chan, 11);
  607. if (ret)
  608. return ret;
  609. BEGIN_RING(chan, NvSubM2MF,
  610. NV_MEMORY_TO_MEMORY_FORMAT_OFFSET_IN, 8);
  611. OUT_RING (chan, src_offset);
  612. OUT_RING (chan, dst_offset);
  613. OUT_RING (chan, PAGE_SIZE); /* src_pitch */
  614. OUT_RING (chan, PAGE_SIZE); /* dst_pitch */
  615. OUT_RING (chan, PAGE_SIZE); /* line_length */
  616. OUT_RING (chan, line_count);
  617. OUT_RING (chan, 0x00000101);
  618. OUT_RING (chan, 0x00000000);
  619. BEGIN_RING(chan, NvSubM2MF, NV_MEMORY_TO_MEMORY_FORMAT_NOP, 1);
  620. OUT_RING (chan, 0);
  621. page_count -= line_count;
  622. src_offset += (PAGE_SIZE * line_count);
  623. dst_offset += (PAGE_SIZE * line_count);
  624. }
  625. return 0;
  626. }
  627. static int
  628. nouveau_bo_move_m2mf(struct ttm_buffer_object *bo, int evict, bool intr,
  629. bool no_wait_reserve, bool no_wait_gpu,
  630. struct ttm_mem_reg *new_mem)
  631. {
  632. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  633. struct nouveau_bo *nvbo = nouveau_bo(bo);
  634. struct nouveau_channel *chan;
  635. int ret;
  636. chan = nvbo->channel;
  637. if (!chan || nvbo->no_vm) {
  638. chan = dev_priv->channel;
  639. mutex_lock_nested(&chan->mutex, NOUVEAU_KCHANNEL_MUTEX);
  640. }
  641. if (dev_priv->card_type < NV_50)
  642. ret = nv04_bo_move_m2mf(chan, bo, &bo->mem, new_mem);
  643. else
  644. if (dev_priv->card_type < NV_C0)
  645. ret = nv50_bo_move_m2mf(chan, bo, &bo->mem, new_mem);
  646. else
  647. ret = nvc0_bo_move_m2mf(chan, bo, &bo->mem, new_mem);
  648. if (ret == 0) {
  649. ret = nouveau_bo_move_accel_cleanup(chan, nvbo, evict,
  650. no_wait_reserve,
  651. no_wait_gpu, new_mem);
  652. }
  653. if (chan == dev_priv->channel)
  654. mutex_unlock(&chan->mutex);
  655. return ret;
  656. }
  657. static int
  658. nouveau_bo_move_flipd(struct ttm_buffer_object *bo, bool evict, bool intr,
  659. bool no_wait_reserve, bool no_wait_gpu,
  660. struct ttm_mem_reg *new_mem)
  661. {
  662. u32 placement_memtype = TTM_PL_FLAG_TT | TTM_PL_MASK_CACHING;
  663. struct ttm_placement placement;
  664. struct ttm_mem_reg tmp_mem;
  665. int ret;
  666. placement.fpfn = placement.lpfn = 0;
  667. placement.num_placement = placement.num_busy_placement = 1;
  668. placement.placement = placement.busy_placement = &placement_memtype;
  669. tmp_mem = *new_mem;
  670. tmp_mem.mm_node = NULL;
  671. ret = ttm_bo_mem_space(bo, &placement, &tmp_mem, intr, no_wait_reserve, no_wait_gpu);
  672. if (ret)
  673. return ret;
  674. ret = ttm_tt_bind(bo->ttm, &tmp_mem);
  675. if (ret)
  676. goto out;
  677. ret = nouveau_bo_move_m2mf(bo, true, intr, no_wait_reserve, no_wait_gpu, &tmp_mem);
  678. if (ret)
  679. goto out;
  680. ret = ttm_bo_move_ttm(bo, true, no_wait_reserve, no_wait_gpu, new_mem);
  681. out:
  682. ttm_bo_mem_put(bo, &tmp_mem);
  683. return ret;
  684. }
  685. static int
  686. nouveau_bo_move_flips(struct ttm_buffer_object *bo, bool evict, bool intr,
  687. bool no_wait_reserve, bool no_wait_gpu,
  688. struct ttm_mem_reg *new_mem)
  689. {
  690. u32 placement_memtype = TTM_PL_FLAG_TT | TTM_PL_MASK_CACHING;
  691. struct ttm_placement placement;
  692. struct ttm_mem_reg tmp_mem;
  693. int ret;
  694. placement.fpfn = placement.lpfn = 0;
  695. placement.num_placement = placement.num_busy_placement = 1;
  696. placement.placement = placement.busy_placement = &placement_memtype;
  697. tmp_mem = *new_mem;
  698. tmp_mem.mm_node = NULL;
  699. ret = ttm_bo_mem_space(bo, &placement, &tmp_mem, intr, no_wait_reserve, no_wait_gpu);
  700. if (ret)
  701. return ret;
  702. ret = ttm_bo_move_ttm(bo, true, no_wait_reserve, no_wait_gpu, &tmp_mem);
  703. if (ret)
  704. goto out;
  705. ret = nouveau_bo_move_m2mf(bo, true, intr, no_wait_reserve, no_wait_gpu, new_mem);
  706. if (ret)
  707. goto out;
  708. out:
  709. ttm_bo_mem_put(bo, &tmp_mem);
  710. return ret;
  711. }
  712. static int
  713. nouveau_bo_vm_bind(struct ttm_buffer_object *bo, struct ttm_mem_reg *new_mem,
  714. struct nouveau_tile_reg **new_tile)
  715. {
  716. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  717. struct drm_device *dev = dev_priv->dev;
  718. struct nouveau_bo *nvbo = nouveau_bo(bo);
  719. uint64_t offset;
  720. if (nvbo->no_vm || new_mem->mem_type != TTM_PL_VRAM) {
  721. /* Nothing to do. */
  722. *new_tile = NULL;
  723. return 0;
  724. }
  725. offset = new_mem->start << PAGE_SHIFT;
  726. if (dev_priv->chan_vm) {
  727. nouveau_vm_map(&nvbo->vma, new_mem->mm_node);
  728. } else if (dev_priv->card_type >= NV_10) {
  729. *new_tile = nv10_mem_set_tiling(dev, offset, new_mem->size,
  730. nvbo->tile_mode,
  731. nvbo->tile_flags);
  732. }
  733. return 0;
  734. }
  735. static void
  736. nouveau_bo_vm_cleanup(struct ttm_buffer_object *bo,
  737. struct nouveau_tile_reg *new_tile,
  738. struct nouveau_tile_reg **old_tile)
  739. {
  740. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  741. struct drm_device *dev = dev_priv->dev;
  742. if (dev_priv->card_type >= NV_10 &&
  743. dev_priv->card_type < NV_50) {
  744. nv10_mem_put_tile_region(dev, *old_tile, bo->sync_obj);
  745. *old_tile = new_tile;
  746. }
  747. }
  748. static int
  749. nouveau_bo_move(struct ttm_buffer_object *bo, bool evict, bool intr,
  750. bool no_wait_reserve, bool no_wait_gpu,
  751. struct ttm_mem_reg *new_mem)
  752. {
  753. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  754. struct nouveau_bo *nvbo = nouveau_bo(bo);
  755. struct ttm_mem_reg *old_mem = &bo->mem;
  756. struct nouveau_tile_reg *new_tile = NULL;
  757. int ret = 0;
  758. ret = nouveau_bo_vm_bind(bo, new_mem, &new_tile);
  759. if (ret)
  760. return ret;
  761. /* Fake bo copy. */
  762. if (old_mem->mem_type == TTM_PL_SYSTEM && !bo->ttm) {
  763. BUG_ON(bo->mem.mm_node != NULL);
  764. bo->mem = *new_mem;
  765. new_mem->mm_node = NULL;
  766. goto out;
  767. }
  768. /* Software copy if the card isn't up and running yet. */
  769. if (!dev_priv->channel) {
  770. ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, no_wait_gpu, new_mem);
  771. goto out;
  772. }
  773. /* Hardware assisted copy. */
  774. if (new_mem->mem_type == TTM_PL_SYSTEM)
  775. ret = nouveau_bo_move_flipd(bo, evict, intr, no_wait_reserve, no_wait_gpu, new_mem);
  776. else if (old_mem->mem_type == TTM_PL_SYSTEM)
  777. ret = nouveau_bo_move_flips(bo, evict, intr, no_wait_reserve, no_wait_gpu, new_mem);
  778. else
  779. ret = nouveau_bo_move_m2mf(bo, evict, intr, no_wait_reserve, no_wait_gpu, new_mem);
  780. if (!ret)
  781. goto out;
  782. /* Fallback to software copy. */
  783. ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, no_wait_gpu, new_mem);
  784. out:
  785. if (ret)
  786. nouveau_bo_vm_cleanup(bo, NULL, &new_tile);
  787. else
  788. nouveau_bo_vm_cleanup(bo, new_tile, &nvbo->tile);
  789. return ret;
  790. }
  791. static int
  792. nouveau_bo_verify_access(struct ttm_buffer_object *bo, struct file *filp)
  793. {
  794. return 0;
  795. }
  796. static int
  797. nouveau_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
  798. {
  799. struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
  800. struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev);
  801. struct drm_device *dev = dev_priv->dev;
  802. int ret;
  803. mem->bus.addr = NULL;
  804. mem->bus.offset = 0;
  805. mem->bus.size = mem->num_pages << PAGE_SHIFT;
  806. mem->bus.base = 0;
  807. mem->bus.is_iomem = false;
  808. if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
  809. return -EINVAL;
  810. switch (mem->mem_type) {
  811. case TTM_PL_SYSTEM:
  812. /* System memory */
  813. return 0;
  814. case TTM_PL_TT:
  815. #if __OS_HAS_AGP
  816. if (dev_priv->gart_info.type == NOUVEAU_GART_AGP) {
  817. mem->bus.offset = mem->start << PAGE_SHIFT;
  818. mem->bus.base = dev_priv->gart_info.aper_base;
  819. mem->bus.is_iomem = true;
  820. }
  821. #endif
  822. break;
  823. case TTM_PL_VRAM:
  824. {
  825. struct nouveau_vram *vram = mem->mm_node;
  826. u8 page_shift;
  827. if (!dev_priv->bar1_vm) {
  828. mem->bus.offset = mem->start << PAGE_SHIFT;
  829. mem->bus.base = pci_resource_start(dev->pdev, 1);
  830. mem->bus.is_iomem = true;
  831. break;
  832. }
  833. if (dev_priv->card_type == NV_C0)
  834. page_shift = vram->page_shift;
  835. else
  836. page_shift = 12;
  837. ret = nouveau_vm_get(dev_priv->bar1_vm, mem->bus.size,
  838. page_shift, NV_MEM_ACCESS_RW,
  839. &vram->bar_vma);
  840. if (ret)
  841. return ret;
  842. nouveau_vm_map(&vram->bar_vma, vram);
  843. if (ret) {
  844. nouveau_vm_put(&vram->bar_vma);
  845. return ret;
  846. }
  847. mem->bus.offset = vram->bar_vma.offset;
  848. if (dev_priv->card_type == NV_50) /*XXX*/
  849. mem->bus.offset -= 0x0020000000ULL;
  850. mem->bus.base = pci_resource_start(dev->pdev, 1);
  851. mem->bus.is_iomem = true;
  852. }
  853. break;
  854. default:
  855. return -EINVAL;
  856. }
  857. return 0;
  858. }
  859. static void
  860. nouveau_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
  861. {
  862. struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev);
  863. struct nouveau_vram *vram = mem->mm_node;
  864. if (!dev_priv->bar1_vm || mem->mem_type != TTM_PL_VRAM)
  865. return;
  866. if (!vram->bar_vma.node)
  867. return;
  868. nouveau_vm_unmap(&vram->bar_vma);
  869. nouveau_vm_put(&vram->bar_vma);
  870. }
  871. static int
  872. nouveau_ttm_fault_reserve_notify(struct ttm_buffer_object *bo)
  873. {
  874. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  875. struct nouveau_bo *nvbo = nouveau_bo(bo);
  876. /* as long as the bo isn't in vram, and isn't tiled, we've got
  877. * nothing to do here.
  878. */
  879. if (bo->mem.mem_type != TTM_PL_VRAM) {
  880. if (dev_priv->card_type < NV_50 ||
  881. !nouveau_bo_tile_layout(nvbo))
  882. return 0;
  883. }
  884. /* make sure bo is in mappable vram */
  885. if (bo->mem.start + bo->mem.num_pages < dev_priv->fb_mappable_pages)
  886. return 0;
  887. nvbo->placement.fpfn = 0;
  888. nvbo->placement.lpfn = dev_priv->fb_mappable_pages;
  889. nouveau_bo_placement_set(nvbo, TTM_PL_VRAM, 0);
  890. return nouveau_bo_validate(nvbo, false, true, false);
  891. }
  892. void
  893. nouveau_bo_fence(struct nouveau_bo *nvbo, struct nouveau_fence *fence)
  894. {
  895. struct nouveau_fence *old_fence;
  896. if (likely(fence))
  897. nouveau_fence_ref(fence);
  898. spin_lock(&nvbo->bo.bdev->fence_lock);
  899. old_fence = nvbo->bo.sync_obj;
  900. nvbo->bo.sync_obj = fence;
  901. spin_unlock(&nvbo->bo.bdev->fence_lock);
  902. nouveau_fence_unref(&old_fence);
  903. }
  904. struct ttm_bo_driver nouveau_bo_driver = {
  905. .create_ttm_backend_entry = nouveau_bo_create_ttm_backend_entry,
  906. .invalidate_caches = nouveau_bo_invalidate_caches,
  907. .init_mem_type = nouveau_bo_init_mem_type,
  908. .evict_flags = nouveau_bo_evict_flags,
  909. .move = nouveau_bo_move,
  910. .verify_access = nouveau_bo_verify_access,
  911. .sync_obj_signaled = __nouveau_fence_signalled,
  912. .sync_obj_wait = __nouveau_fence_wait,
  913. .sync_obj_flush = __nouveau_fence_flush,
  914. .sync_obj_unref = __nouveau_fence_unref,
  915. .sync_obj_ref = __nouveau_fence_ref,
  916. .fault_reserve_notify = &nouveau_ttm_fault_reserve_notify,
  917. .io_mem_reserve = &nouveau_ttm_io_mem_reserve,
  918. .io_mem_free = &nouveau_ttm_io_mem_free,
  919. };