nouveau_bo.c 28 KB

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