nouveau_bo.c 27 KB

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