nouveau_mem.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251
  1. /*
  2. * Copyright (C) The Weather Channel, Inc. 2002. All Rights Reserved.
  3. * Copyright 2005 Stephane Marchesin
  4. *
  5. * The Weather Channel (TM) funded Tungsten Graphics to develop the
  6. * initial release of the Radeon 8500 driver under the XFree86 license.
  7. * This notice must be preserved.
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a
  10. * copy of this software and associated documentation files (the "Software"),
  11. * to deal in the Software without restriction, including without limitation
  12. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  13. * and/or sell copies of the Software, and to permit persons to whom the
  14. * Software is furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice (including the next
  17. * paragraph) shall be included in all copies or substantial portions of the
  18. * Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  23. * THE AUTHORS AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  24. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  25. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  26. * DEALINGS IN THE SOFTWARE.
  27. *
  28. * Authors:
  29. * Ben Skeggs <bskeggs@redhat.com>
  30. * Roy Spliet <r.spliet@student.tudelft.nl>
  31. */
  32. #include "drmP.h"
  33. #include "nouveau_drv.h"
  34. #include "nouveau_pm.h"
  35. #include "nouveau_mm.h"
  36. #include "nouveau_vm.h"
  37. #include "nouveau_fifo.h"
  38. #include "nouveau_fence.h"
  39. /*
  40. * NV10-NV40 tiling helpers
  41. */
  42. static void
  43. nv10_mem_update_tile_region(struct drm_device *dev,
  44. struct nouveau_tile_reg *tile, uint32_t addr,
  45. uint32_t size, uint32_t pitch, uint32_t flags)
  46. {
  47. struct drm_nouveau_private *dev_priv = dev->dev_private;
  48. struct nouveau_fb_engine *pfb = &dev_priv->engine.fb;
  49. int i = tile - dev_priv->tile.reg, j;
  50. unsigned long save;
  51. nouveau_fence_unref(&tile->fence);
  52. if (tile->pitch)
  53. pfb->free_tile_region(dev, i);
  54. if (pitch)
  55. pfb->init_tile_region(dev, i, addr, size, pitch, flags);
  56. spin_lock_irqsave(&dev_priv->context_switch_lock, save);
  57. nv_wr32(dev, NV03_PFIFO_CACHES, 0);
  58. nv04_fifo_cache_pull(dev, false);
  59. nouveau_wait_for_idle(dev);
  60. pfb->set_tile_region(dev, i);
  61. for (j = 0; j < NVOBJ_ENGINE_NR; j++) {
  62. if (dev_priv->eng[j] && dev_priv->eng[j]->set_tile_region)
  63. dev_priv->eng[j]->set_tile_region(dev, i);
  64. }
  65. nv04_fifo_cache_pull(dev, true);
  66. nv_wr32(dev, NV03_PFIFO_CACHES, 1);
  67. spin_unlock_irqrestore(&dev_priv->context_switch_lock, save);
  68. }
  69. static struct nouveau_tile_reg *
  70. nv10_mem_get_tile_region(struct drm_device *dev, int i)
  71. {
  72. struct drm_nouveau_private *dev_priv = dev->dev_private;
  73. struct nouveau_tile_reg *tile = &dev_priv->tile.reg[i];
  74. spin_lock(&dev_priv->tile.lock);
  75. if (!tile->used &&
  76. (!tile->fence || nouveau_fence_done(tile->fence)))
  77. tile->used = true;
  78. else
  79. tile = NULL;
  80. spin_unlock(&dev_priv->tile.lock);
  81. return tile;
  82. }
  83. void
  84. nv10_mem_put_tile_region(struct drm_device *dev, struct nouveau_tile_reg *tile,
  85. struct nouveau_fence *fence)
  86. {
  87. struct drm_nouveau_private *dev_priv = dev->dev_private;
  88. if (tile) {
  89. spin_lock(&dev_priv->tile.lock);
  90. if (fence) {
  91. /* Mark it as pending. */
  92. tile->fence = fence;
  93. nouveau_fence_ref(fence);
  94. }
  95. tile->used = false;
  96. spin_unlock(&dev_priv->tile.lock);
  97. }
  98. }
  99. struct nouveau_tile_reg *
  100. nv10_mem_set_tiling(struct drm_device *dev, uint32_t addr, uint32_t size,
  101. uint32_t pitch, uint32_t flags)
  102. {
  103. struct drm_nouveau_private *dev_priv = dev->dev_private;
  104. struct nouveau_fb_engine *pfb = &dev_priv->engine.fb;
  105. struct nouveau_tile_reg *tile, *found = NULL;
  106. int i;
  107. for (i = 0; i < pfb->num_tiles; i++) {
  108. tile = nv10_mem_get_tile_region(dev, i);
  109. if (pitch && !found) {
  110. found = tile;
  111. continue;
  112. } else if (tile && tile->pitch) {
  113. /* Kill an unused tile region. */
  114. nv10_mem_update_tile_region(dev, tile, 0, 0, 0, 0);
  115. }
  116. nv10_mem_put_tile_region(dev, tile, NULL);
  117. }
  118. if (found)
  119. nv10_mem_update_tile_region(dev, found, addr, size,
  120. pitch, flags);
  121. return found;
  122. }
  123. /*
  124. * Cleanup everything
  125. */
  126. void
  127. nouveau_mem_vram_fini(struct drm_device *dev)
  128. {
  129. struct drm_nouveau_private *dev_priv = dev->dev_private;
  130. ttm_bo_device_release(&dev_priv->ttm.bdev);
  131. nouveau_ttm_global_release(dev_priv);
  132. if (dev_priv->fb_mtrr >= 0) {
  133. drm_mtrr_del(dev_priv->fb_mtrr,
  134. pci_resource_start(dev->pdev, 1),
  135. pci_resource_len(dev->pdev, 1), DRM_MTRR_WC);
  136. dev_priv->fb_mtrr = -1;
  137. }
  138. }
  139. void
  140. nouveau_mem_gart_fini(struct drm_device *dev)
  141. {
  142. nouveau_sgdma_takedown(dev);
  143. if (drm_core_has_AGP(dev) && dev->agp) {
  144. struct drm_agp_mem *entry, *tempe;
  145. /* Remove AGP resources, but leave dev->agp
  146. intact until drv_cleanup is called. */
  147. list_for_each_entry_safe(entry, tempe, &dev->agp->memory, head) {
  148. if (entry->bound)
  149. drm_unbind_agp(entry->memory);
  150. drm_free_agp(entry->memory, entry->pages);
  151. kfree(entry);
  152. }
  153. INIT_LIST_HEAD(&dev->agp->memory);
  154. if (dev->agp->acquired)
  155. drm_agp_release(dev);
  156. dev->agp->acquired = 0;
  157. dev->agp->enabled = 0;
  158. }
  159. }
  160. bool
  161. nouveau_mem_flags_valid(struct drm_device *dev, u32 tile_flags)
  162. {
  163. if (!(tile_flags & NOUVEAU_GEM_TILE_LAYOUT_MASK))
  164. return true;
  165. return false;
  166. }
  167. #if __OS_HAS_AGP
  168. static unsigned long
  169. get_agp_mode(struct drm_device *dev, unsigned long mode)
  170. {
  171. struct drm_nouveau_private *dev_priv = dev->dev_private;
  172. /*
  173. * FW seems to be broken on nv18, it makes the card lock up
  174. * randomly.
  175. */
  176. if (dev_priv->chipset == 0x18)
  177. mode &= ~PCI_AGP_COMMAND_FW;
  178. /*
  179. * AGP mode set in the command line.
  180. */
  181. if (nouveau_agpmode > 0) {
  182. bool agpv3 = mode & 0x8;
  183. int rate = agpv3 ? nouveau_agpmode / 4 : nouveau_agpmode;
  184. mode = (mode & ~0x7) | (rate & 0x7);
  185. }
  186. return mode;
  187. }
  188. #endif
  189. int
  190. nouveau_mem_reset_agp(struct drm_device *dev)
  191. {
  192. #if __OS_HAS_AGP
  193. uint32_t saved_pci_nv_1, pmc_enable;
  194. int ret;
  195. /* First of all, disable fast writes, otherwise if it's
  196. * already enabled in the AGP bridge and we disable the card's
  197. * AGP controller we might be locking ourselves out of it. */
  198. if ((nv_rd32(dev, NV04_PBUS_PCI_NV_19) |
  199. dev->agp->mode) & PCI_AGP_COMMAND_FW) {
  200. struct drm_agp_info info;
  201. struct drm_agp_mode mode;
  202. ret = drm_agp_info(dev, &info);
  203. if (ret)
  204. return ret;
  205. mode.mode = get_agp_mode(dev, info.mode) & ~PCI_AGP_COMMAND_FW;
  206. ret = drm_agp_enable(dev, mode);
  207. if (ret)
  208. return ret;
  209. }
  210. saved_pci_nv_1 = nv_rd32(dev, NV04_PBUS_PCI_NV_1);
  211. /* clear busmaster bit */
  212. nv_wr32(dev, NV04_PBUS_PCI_NV_1, saved_pci_nv_1 & ~0x4);
  213. /* disable AGP */
  214. nv_wr32(dev, NV04_PBUS_PCI_NV_19, 0);
  215. /* power cycle pgraph, if enabled */
  216. pmc_enable = nv_rd32(dev, NV03_PMC_ENABLE);
  217. if (pmc_enable & NV_PMC_ENABLE_PGRAPH) {
  218. nv_wr32(dev, NV03_PMC_ENABLE,
  219. pmc_enable & ~NV_PMC_ENABLE_PGRAPH);
  220. nv_wr32(dev, NV03_PMC_ENABLE, nv_rd32(dev, NV03_PMC_ENABLE) |
  221. NV_PMC_ENABLE_PGRAPH);
  222. }
  223. /* and restore (gives effect of resetting AGP) */
  224. nv_wr32(dev, NV04_PBUS_PCI_NV_1, saved_pci_nv_1);
  225. #endif
  226. return 0;
  227. }
  228. int
  229. nouveau_mem_init_agp(struct drm_device *dev)
  230. {
  231. #if __OS_HAS_AGP
  232. struct drm_nouveau_private *dev_priv = dev->dev_private;
  233. struct drm_agp_info info;
  234. struct drm_agp_mode mode;
  235. int ret;
  236. if (!dev->agp->acquired) {
  237. ret = drm_agp_acquire(dev);
  238. if (ret) {
  239. NV_ERROR(dev, "Unable to acquire AGP: %d\n", ret);
  240. return ret;
  241. }
  242. }
  243. nouveau_mem_reset_agp(dev);
  244. ret = drm_agp_info(dev, &info);
  245. if (ret) {
  246. NV_ERROR(dev, "Unable to get AGP info: %d\n", ret);
  247. return ret;
  248. }
  249. /* see agp.h for the AGPSTAT_* modes available */
  250. mode.mode = get_agp_mode(dev, info.mode);
  251. ret = drm_agp_enable(dev, mode);
  252. if (ret) {
  253. NV_ERROR(dev, "Unable to enable AGP: %d\n", ret);
  254. return ret;
  255. }
  256. dev_priv->gart_info.type = NOUVEAU_GART_AGP;
  257. dev_priv->gart_info.aper_base = info.aperture_base;
  258. dev_priv->gart_info.aper_size = info.aperture_size;
  259. #endif
  260. return 0;
  261. }
  262. static const struct vram_types {
  263. int value;
  264. const char *name;
  265. } vram_type_map[] = {
  266. { NV_MEM_TYPE_STOLEN , "stolen system memory" },
  267. { NV_MEM_TYPE_SGRAM , "SGRAM" },
  268. { NV_MEM_TYPE_SDRAM , "SDRAM" },
  269. { NV_MEM_TYPE_DDR1 , "DDR1" },
  270. { NV_MEM_TYPE_DDR2 , "DDR2" },
  271. { NV_MEM_TYPE_DDR3 , "DDR3" },
  272. { NV_MEM_TYPE_GDDR2 , "GDDR2" },
  273. { NV_MEM_TYPE_GDDR3 , "GDDR3" },
  274. { NV_MEM_TYPE_GDDR4 , "GDDR4" },
  275. { NV_MEM_TYPE_GDDR5 , "GDDR5" },
  276. { NV_MEM_TYPE_UNKNOWN, "unknown type" }
  277. };
  278. int
  279. nouveau_mem_vram_init(struct drm_device *dev)
  280. {
  281. struct drm_nouveau_private *dev_priv = dev->dev_private;
  282. struct ttm_bo_device *bdev = &dev_priv->ttm.bdev;
  283. const struct vram_types *vram_type;
  284. int ret, dma_bits;
  285. dma_bits = 32;
  286. if (dev_priv->card_type >= NV_50) {
  287. if (pci_dma_supported(dev->pdev, DMA_BIT_MASK(40)))
  288. dma_bits = 40;
  289. } else
  290. if (0 && pci_is_pcie(dev->pdev) &&
  291. dev_priv->chipset > 0x40 &&
  292. dev_priv->chipset != 0x45) {
  293. if (pci_dma_supported(dev->pdev, DMA_BIT_MASK(39)))
  294. dma_bits = 39;
  295. }
  296. ret = pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(dma_bits));
  297. if (ret)
  298. return ret;
  299. ret = pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(dma_bits));
  300. if (ret) {
  301. /* Reset to default value. */
  302. pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(32));
  303. }
  304. ret = nouveau_ttm_global_init(dev_priv);
  305. if (ret)
  306. return ret;
  307. ret = ttm_bo_device_init(&dev_priv->ttm.bdev,
  308. dev_priv->ttm.bo_global_ref.ref.object,
  309. &nouveau_bo_driver, DRM_FILE_PAGE_OFFSET,
  310. dma_bits <= 32 ? true : false);
  311. if (ret) {
  312. NV_ERROR(dev, "Error initialising bo driver: %d\n", ret);
  313. return ret;
  314. }
  315. vram_type = vram_type_map;
  316. while (vram_type->value != NV_MEM_TYPE_UNKNOWN) {
  317. if (nouveau_vram_type) {
  318. if (!strcasecmp(nouveau_vram_type, vram_type->name))
  319. break;
  320. dev_priv->vram_type = vram_type->value;
  321. } else {
  322. if (vram_type->value == dev_priv->vram_type)
  323. break;
  324. }
  325. vram_type++;
  326. }
  327. NV_INFO(dev, "Detected %dMiB VRAM (%s)\n",
  328. (int)(dev_priv->vram_size >> 20), vram_type->name);
  329. if (dev_priv->vram_sys_base) {
  330. NV_INFO(dev, "Stolen system memory at: 0x%010llx\n",
  331. dev_priv->vram_sys_base);
  332. }
  333. dev_priv->fb_available_size = dev_priv->vram_size;
  334. dev_priv->fb_mappable_pages = dev_priv->fb_available_size;
  335. if (dev_priv->fb_mappable_pages > pci_resource_len(dev->pdev, 1))
  336. dev_priv->fb_mappable_pages = pci_resource_len(dev->pdev, 1);
  337. dev_priv->fb_mappable_pages >>= PAGE_SHIFT;
  338. dev_priv->fb_available_size -= dev_priv->ramin_rsvd_vram;
  339. dev_priv->fb_aper_free = dev_priv->fb_available_size;
  340. /* mappable vram */
  341. ret = ttm_bo_init_mm(bdev, TTM_PL_VRAM,
  342. dev_priv->fb_available_size >> PAGE_SHIFT);
  343. if (ret) {
  344. NV_ERROR(dev, "Failed VRAM mm init: %d\n", ret);
  345. return ret;
  346. }
  347. if (dev_priv->card_type < NV_50) {
  348. ret = nouveau_bo_new(dev, 256*1024, 0, TTM_PL_FLAG_VRAM,
  349. 0, 0, NULL, &dev_priv->vga_ram);
  350. if (ret == 0)
  351. ret = nouveau_bo_pin(dev_priv->vga_ram,
  352. TTM_PL_FLAG_VRAM);
  353. if (ret) {
  354. NV_WARN(dev, "failed to reserve VGA memory\n");
  355. nouveau_bo_ref(NULL, &dev_priv->vga_ram);
  356. }
  357. }
  358. dev_priv->fb_mtrr = drm_mtrr_add(pci_resource_start(dev->pdev, 1),
  359. pci_resource_len(dev->pdev, 1),
  360. DRM_MTRR_WC);
  361. return 0;
  362. }
  363. int
  364. nouveau_mem_gart_init(struct drm_device *dev)
  365. {
  366. struct drm_nouveau_private *dev_priv = dev->dev_private;
  367. struct ttm_bo_device *bdev = &dev_priv->ttm.bdev;
  368. int ret;
  369. dev_priv->gart_info.type = NOUVEAU_GART_NONE;
  370. #if !defined(__powerpc__) && !defined(__ia64__)
  371. if (drm_pci_device_is_agp(dev) && dev->agp && nouveau_agpmode) {
  372. ret = nouveau_mem_init_agp(dev);
  373. if (ret)
  374. NV_ERROR(dev, "Error initialising AGP: %d\n", ret);
  375. }
  376. #endif
  377. if (dev_priv->gart_info.type == NOUVEAU_GART_NONE) {
  378. ret = nouveau_sgdma_init(dev);
  379. if (ret) {
  380. NV_ERROR(dev, "Error initialising PCI(E): %d\n", ret);
  381. return ret;
  382. }
  383. }
  384. NV_INFO(dev, "%d MiB GART (aperture)\n",
  385. (int)(dev_priv->gart_info.aper_size >> 20));
  386. dev_priv->gart_info.aper_free = dev_priv->gart_info.aper_size;
  387. ret = ttm_bo_init_mm(bdev, TTM_PL_TT,
  388. dev_priv->gart_info.aper_size >> PAGE_SHIFT);
  389. if (ret) {
  390. NV_ERROR(dev, "Failed TT mm init: %d\n", ret);
  391. return ret;
  392. }
  393. return 0;
  394. }
  395. static int
  396. nv40_mem_timing_calc(struct drm_device *dev, u32 freq,
  397. struct nouveau_pm_tbl_entry *e, u8 len,
  398. struct nouveau_pm_memtiming *boot,
  399. struct nouveau_pm_memtiming *t)
  400. {
  401. t->reg[0] = (e->tRP << 24 | e->tRAS << 16 | e->tRFC << 8 | e->tRC);
  402. /* XXX: I don't trust the -1's and +1's... they must come
  403. * from somewhere! */
  404. t->reg[1] = (e->tWR + 2 + (t->tCWL - 1)) << 24 |
  405. 1 << 16 |
  406. (e->tWTR + 2 + (t->tCWL - 1)) << 8 |
  407. (e->tCL + 2 - (t->tCWL - 1));
  408. t->reg[2] = 0x20200000 |
  409. ((t->tCWL - 1) << 24 |
  410. e->tRRD << 16 |
  411. e->tRCDWR << 8 |
  412. e->tRCDRD);
  413. NV_DEBUG(dev, "Entry %d: 220: %08x %08x %08x\n", t->id,
  414. t->reg[0], t->reg[1], t->reg[2]);
  415. return 0;
  416. }
  417. static int
  418. nv50_mem_timing_calc(struct drm_device *dev, u32 freq,
  419. struct nouveau_pm_tbl_entry *e, u8 len,
  420. struct nouveau_pm_memtiming *boot,
  421. struct nouveau_pm_memtiming *t)
  422. {
  423. struct drm_nouveau_private *dev_priv = dev->dev_private;
  424. struct bit_entry P;
  425. uint8_t unk18 = 1, unk20 = 0, unk21 = 0, tmp7_3;
  426. if (bit_table(dev, 'P', &P))
  427. return -EINVAL;
  428. switch (min(len, (u8) 22)) {
  429. case 22:
  430. unk21 = e->tUNK_21;
  431. case 21:
  432. unk20 = e->tUNK_20;
  433. case 20:
  434. if (e->tCWL > 0)
  435. t->tCWL = e->tCWL;
  436. case 19:
  437. unk18 = e->tUNK_18;
  438. break;
  439. }
  440. t->reg[0] = (e->tRP << 24 | e->tRAS << 16 | e->tRFC << 8 | e->tRC);
  441. t->reg[1] = (e->tWR + 2 + (t->tCWL - 1)) << 24 |
  442. max(unk18, (u8) 1) << 16 |
  443. (e->tWTR + 2 + (t->tCWL - 1)) << 8;
  444. t->reg[2] = ((t->tCWL - 1) << 24 |
  445. e->tRRD << 16 |
  446. e->tRCDWR << 8 |
  447. e->tRCDRD);
  448. t->reg[4] = e->tUNK_13 << 8 | e->tUNK_13;
  449. t->reg[5] = (e->tRFC << 24 | max(e->tRCDRD, e->tRCDWR) << 16 | e->tRP);
  450. t->reg[8] = boot->reg[8] & 0xffffff00;
  451. if (P.version == 1) {
  452. t->reg[1] |= (e->tCL + 2 - (t->tCWL - 1));
  453. t->reg[3] = (0x14 + e->tCL) << 24 |
  454. 0x16 << 16 |
  455. (e->tCL - 1) << 8 |
  456. (e->tCL - 1);
  457. t->reg[4] |= boot->reg[4] & 0xffff0000;
  458. t->reg[6] = (0x33 - t->tCWL) << 16 |
  459. t->tCWL << 8 |
  460. (0x2e + e->tCL - t->tCWL);
  461. t->reg[7] = 0x4000202 | (e->tCL - 1) << 16;
  462. /* XXX: P.version == 1 only has DDR2 and GDDR3? */
  463. if (dev_priv->vram_type == NV_MEM_TYPE_DDR2) {
  464. t->reg[5] |= (e->tCL + 3) << 8;
  465. t->reg[6] |= (t->tCWL - 2) << 8;
  466. t->reg[8] |= (e->tCL - 4);
  467. } else {
  468. t->reg[5] |= (e->tCL + 2) << 8;
  469. t->reg[6] |= t->tCWL << 8;
  470. t->reg[8] |= (e->tCL - 2);
  471. }
  472. } else {
  473. t->reg[1] |= (5 + e->tCL - (t->tCWL));
  474. /* XXX: 0xb? 0x30? */
  475. t->reg[3] = (0x30 + e->tCL) << 24 |
  476. (boot->reg[3] & 0x00ff0000)|
  477. (0xb + e->tCL) << 8 |
  478. (e->tCL - 1);
  479. t->reg[4] |= (unk20 << 24 | unk21 << 16);
  480. /* XXX: +6? */
  481. t->reg[5] |= (t->tCWL + 6) << 8;
  482. t->reg[6] = (0x5a + e->tCL) << 16 |
  483. (6 - e->tCL + t->tCWL) << 8 |
  484. (0x50 + e->tCL - t->tCWL);
  485. tmp7_3 = (boot->reg[7] & 0xff000000) >> 24;
  486. t->reg[7] = (tmp7_3 << 24) |
  487. ((tmp7_3 - 6 + e->tCL) << 16) |
  488. 0x202;
  489. }
  490. NV_DEBUG(dev, "Entry %d: 220: %08x %08x %08x %08x\n", t->id,
  491. t->reg[0], t->reg[1], t->reg[2], t->reg[3]);
  492. NV_DEBUG(dev, " 230: %08x %08x %08x %08x\n",
  493. t->reg[4], t->reg[5], t->reg[6], t->reg[7]);
  494. NV_DEBUG(dev, " 240: %08x\n", t->reg[8]);
  495. return 0;
  496. }
  497. static int
  498. nvc0_mem_timing_calc(struct drm_device *dev, u32 freq,
  499. struct nouveau_pm_tbl_entry *e, u8 len,
  500. struct nouveau_pm_memtiming *boot,
  501. struct nouveau_pm_memtiming *t)
  502. {
  503. if (e->tCWL > 0)
  504. t->tCWL = e->tCWL;
  505. t->reg[0] = (e->tRP << 24 | (e->tRAS & 0x7f) << 17 |
  506. e->tRFC << 8 | e->tRC);
  507. t->reg[1] = (boot->reg[1] & 0xff000000) |
  508. (e->tRCDWR & 0x0f) << 20 |
  509. (e->tRCDRD & 0x0f) << 14 |
  510. (t->tCWL << 7) |
  511. (e->tCL & 0x0f);
  512. t->reg[2] = (boot->reg[2] & 0xff0000ff) |
  513. e->tWR << 16 | e->tWTR << 8;
  514. t->reg[3] = (e->tUNK_20 & 0x1f) << 9 |
  515. (e->tUNK_21 & 0xf) << 5 |
  516. (e->tUNK_13 & 0x1f);
  517. t->reg[4] = (boot->reg[4] & 0xfff00fff) |
  518. (e->tRRD&0x1f) << 15;
  519. NV_DEBUG(dev, "Entry %d: 290: %08x %08x %08x %08x\n", t->id,
  520. t->reg[0], t->reg[1], t->reg[2], t->reg[3]);
  521. NV_DEBUG(dev, " 2a0: %08x\n", t->reg[4]);
  522. return 0;
  523. }
  524. /**
  525. * MR generation methods
  526. */
  527. static int
  528. nouveau_mem_ddr2_mr(struct drm_device *dev, u32 freq,
  529. struct nouveau_pm_tbl_entry *e, u8 len,
  530. struct nouveau_pm_memtiming *boot,
  531. struct nouveau_pm_memtiming *t)
  532. {
  533. t->drive_strength = 0;
  534. if (len < 15) {
  535. t->odt = boot->odt;
  536. } else {
  537. t->odt = e->RAM_FT1 & 0x07;
  538. }
  539. if (e->tCL >= NV_MEM_CL_DDR2_MAX) {
  540. NV_WARN(dev, "(%u) Invalid tCL: %u", t->id, e->tCL);
  541. return -ERANGE;
  542. }
  543. if (e->tWR >= NV_MEM_WR_DDR2_MAX) {
  544. NV_WARN(dev, "(%u) Invalid tWR: %u", t->id, e->tWR);
  545. return -ERANGE;
  546. }
  547. if (t->odt > 3) {
  548. NV_WARN(dev, "(%u) Invalid odt value, assuming disabled: %x",
  549. t->id, t->odt);
  550. t->odt = 0;
  551. }
  552. t->mr[0] = (boot->mr[0] & 0x100f) |
  553. (e->tCL) << 4 |
  554. (e->tWR - 1) << 9;
  555. t->mr[1] = (boot->mr[1] & 0x101fbb) |
  556. (t->odt & 0x1) << 2 |
  557. (t->odt & 0x2) << 5;
  558. NV_DEBUG(dev, "(%u) MR: %08x", t->id, t->mr[0]);
  559. return 0;
  560. }
  561. uint8_t nv_mem_wr_lut_ddr3[NV_MEM_WR_DDR3_MAX] = {
  562. 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 5, 6, 6, 7, 7, 0, 0};
  563. static int
  564. nouveau_mem_ddr3_mr(struct drm_device *dev, u32 freq,
  565. struct nouveau_pm_tbl_entry *e, u8 len,
  566. struct nouveau_pm_memtiming *boot,
  567. struct nouveau_pm_memtiming *t)
  568. {
  569. u8 cl = e->tCL - 4;
  570. t->drive_strength = 0;
  571. if (len < 15) {
  572. t->odt = boot->odt;
  573. } else {
  574. t->odt = e->RAM_FT1 & 0x07;
  575. }
  576. if (e->tCL >= NV_MEM_CL_DDR3_MAX || e->tCL < 4) {
  577. NV_WARN(dev, "(%u) Invalid tCL: %u", t->id, e->tCL);
  578. return -ERANGE;
  579. }
  580. if (e->tWR >= NV_MEM_WR_DDR3_MAX || e->tWR < 4) {
  581. NV_WARN(dev, "(%u) Invalid tWR: %u", t->id, e->tWR);
  582. return -ERANGE;
  583. }
  584. if (e->tCWL < 5) {
  585. NV_WARN(dev, "(%u) Invalid tCWL: %u", t->id, e->tCWL);
  586. return -ERANGE;
  587. }
  588. t->mr[0] = (boot->mr[0] & 0x180b) |
  589. /* CAS */
  590. (cl & 0x7) << 4 |
  591. (cl & 0x8) >> 1 |
  592. (nv_mem_wr_lut_ddr3[e->tWR]) << 9;
  593. t->mr[1] = (boot->mr[1] & 0x101dbb) |
  594. (t->odt & 0x1) << 2 |
  595. (t->odt & 0x2) << 5 |
  596. (t->odt & 0x4) << 7;
  597. t->mr[2] = (boot->mr[2] & 0x20ffb7) | (e->tCWL - 5) << 3;
  598. NV_DEBUG(dev, "(%u) MR: %08x %08x", t->id, t->mr[0], t->mr[2]);
  599. return 0;
  600. }
  601. uint8_t nv_mem_cl_lut_gddr3[NV_MEM_CL_GDDR3_MAX] = {
  602. 0, 0, 0, 0, 4, 5, 6, 7, 0, 1, 2, 3, 8, 9, 10, 11};
  603. uint8_t nv_mem_wr_lut_gddr3[NV_MEM_WR_GDDR3_MAX] = {
  604. 0, 0, 0, 0, 0, 2, 3, 8, 9, 10, 11, 0, 0, 1, 1, 0, 3};
  605. static int
  606. nouveau_mem_gddr3_mr(struct drm_device *dev, u32 freq,
  607. struct nouveau_pm_tbl_entry *e, u8 len,
  608. struct nouveau_pm_memtiming *boot,
  609. struct nouveau_pm_memtiming *t)
  610. {
  611. if (len < 15) {
  612. t->drive_strength = boot->drive_strength;
  613. t->odt = boot->odt;
  614. } else {
  615. t->drive_strength = (e->RAM_FT1 & 0x30) >> 4;
  616. t->odt = e->RAM_FT1 & 0x07;
  617. }
  618. if (e->tCL >= NV_MEM_CL_GDDR3_MAX) {
  619. NV_WARN(dev, "(%u) Invalid tCL: %u", t->id, e->tCL);
  620. return -ERANGE;
  621. }
  622. if (e->tWR >= NV_MEM_WR_GDDR3_MAX) {
  623. NV_WARN(dev, "(%u) Invalid tWR: %u", t->id, e->tWR);
  624. return -ERANGE;
  625. }
  626. if (t->odt > 3) {
  627. NV_WARN(dev, "(%u) Invalid odt value, assuming autocal: %x",
  628. t->id, t->odt);
  629. t->odt = 0;
  630. }
  631. t->mr[0] = (boot->mr[0] & 0xe0b) |
  632. /* CAS */
  633. ((nv_mem_cl_lut_gddr3[e->tCL] & 0x7) << 4) |
  634. ((nv_mem_cl_lut_gddr3[e->tCL] & 0x8) >> 2);
  635. t->mr[1] = (boot->mr[1] & 0x100f40) | t->drive_strength |
  636. (t->odt << 2) |
  637. (nv_mem_wr_lut_gddr3[e->tWR] & 0xf) << 4;
  638. t->mr[2] = boot->mr[2];
  639. NV_DEBUG(dev, "(%u) MR: %08x %08x %08x", t->id,
  640. t->mr[0], t->mr[1], t->mr[2]);
  641. return 0;
  642. }
  643. static int
  644. nouveau_mem_gddr5_mr(struct drm_device *dev, u32 freq,
  645. struct nouveau_pm_tbl_entry *e, u8 len,
  646. struct nouveau_pm_memtiming *boot,
  647. struct nouveau_pm_memtiming *t)
  648. {
  649. if (len < 15) {
  650. t->drive_strength = boot->drive_strength;
  651. t->odt = boot->odt;
  652. } else {
  653. t->drive_strength = (e->RAM_FT1 & 0x30) >> 4;
  654. t->odt = e->RAM_FT1 & 0x03;
  655. }
  656. if (e->tCL >= NV_MEM_CL_GDDR5_MAX) {
  657. NV_WARN(dev, "(%u) Invalid tCL: %u", t->id, e->tCL);
  658. return -ERANGE;
  659. }
  660. if (e->tWR >= NV_MEM_WR_GDDR5_MAX) {
  661. NV_WARN(dev, "(%u) Invalid tWR: %u", t->id, e->tWR);
  662. return -ERANGE;
  663. }
  664. if (t->odt > 3) {
  665. NV_WARN(dev, "(%u) Invalid odt value, assuming autocal: %x",
  666. t->id, t->odt);
  667. t->odt = 0;
  668. }
  669. t->mr[0] = (boot->mr[0] & 0x007) |
  670. ((e->tCL - 5) << 3) |
  671. ((e->tWR - 4) << 8);
  672. t->mr[1] = (boot->mr[1] & 0x1007f0) |
  673. t->drive_strength |
  674. (t->odt << 2);
  675. NV_DEBUG(dev, "(%u) MR: %08x %08x", t->id, t->mr[0], t->mr[1]);
  676. return 0;
  677. }
  678. int
  679. nouveau_mem_timing_calc(struct drm_device *dev, u32 freq,
  680. struct nouveau_pm_memtiming *t)
  681. {
  682. struct drm_nouveau_private *dev_priv = dev->dev_private;
  683. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  684. struct nouveau_pm_memtiming *boot = &pm->boot.timing;
  685. struct nouveau_pm_tbl_entry *e;
  686. u8 ver, len, *ptr, *ramcfg;
  687. int ret;
  688. ptr = nouveau_perf_timing(dev, freq, &ver, &len);
  689. if (!ptr || ptr[0] == 0x00) {
  690. *t = *boot;
  691. return 0;
  692. }
  693. e = (struct nouveau_pm_tbl_entry *)ptr;
  694. t->tCWL = boot->tCWL;
  695. switch (dev_priv->card_type) {
  696. case NV_40:
  697. ret = nv40_mem_timing_calc(dev, freq, e, len, boot, t);
  698. break;
  699. case NV_50:
  700. ret = nv50_mem_timing_calc(dev, freq, e, len, boot, t);
  701. break;
  702. case NV_C0:
  703. case NV_D0:
  704. ret = nvc0_mem_timing_calc(dev, freq, e, len, boot, t);
  705. break;
  706. default:
  707. ret = -ENODEV;
  708. break;
  709. }
  710. switch (dev_priv->vram_type * !ret) {
  711. case NV_MEM_TYPE_GDDR3:
  712. ret = nouveau_mem_gddr3_mr(dev, freq, e, len, boot, t);
  713. break;
  714. case NV_MEM_TYPE_GDDR5:
  715. ret = nouveau_mem_gddr5_mr(dev, freq, e, len, boot, t);
  716. break;
  717. case NV_MEM_TYPE_DDR2:
  718. ret = nouveau_mem_ddr2_mr(dev, freq, e, len, boot, t);
  719. break;
  720. case NV_MEM_TYPE_DDR3:
  721. ret = nouveau_mem_ddr3_mr(dev, freq, e, len, boot, t);
  722. break;
  723. default:
  724. ret = -EINVAL;
  725. break;
  726. }
  727. ramcfg = nouveau_perf_ramcfg(dev, freq, &ver, &len);
  728. if (ramcfg) {
  729. int dll_off;
  730. if (ver == 0x00)
  731. dll_off = !!(ramcfg[3] & 0x04);
  732. else
  733. dll_off = !!(ramcfg[2] & 0x40);
  734. switch (dev_priv->vram_type) {
  735. case NV_MEM_TYPE_GDDR3:
  736. t->mr[1] &= ~0x00000040;
  737. t->mr[1] |= 0x00000040 * dll_off;
  738. break;
  739. default:
  740. t->mr[1] &= ~0x00000001;
  741. t->mr[1] |= 0x00000001 * dll_off;
  742. break;
  743. }
  744. }
  745. return ret;
  746. }
  747. void
  748. nouveau_mem_timing_read(struct drm_device *dev, struct nouveau_pm_memtiming *t)
  749. {
  750. struct drm_nouveau_private *dev_priv = dev->dev_private;
  751. u32 timing_base, timing_regs, mr_base;
  752. int i;
  753. if (dev_priv->card_type >= 0xC0) {
  754. timing_base = 0x10f290;
  755. mr_base = 0x10f300;
  756. } else {
  757. timing_base = 0x100220;
  758. mr_base = 0x1002c0;
  759. }
  760. t->id = -1;
  761. switch (dev_priv->card_type) {
  762. case NV_50:
  763. timing_regs = 9;
  764. break;
  765. case NV_C0:
  766. case NV_D0:
  767. timing_regs = 5;
  768. break;
  769. case NV_30:
  770. case NV_40:
  771. timing_regs = 3;
  772. break;
  773. default:
  774. timing_regs = 0;
  775. return;
  776. }
  777. for(i = 0; i < timing_regs; i++)
  778. t->reg[i] = nv_rd32(dev, timing_base + (0x04 * i));
  779. t->tCWL = 0;
  780. if (dev_priv->card_type < NV_C0) {
  781. t->tCWL = ((nv_rd32(dev, 0x100228) & 0x0f000000) >> 24) + 1;
  782. } else if (dev_priv->card_type <= NV_D0) {
  783. t->tCWL = ((nv_rd32(dev, 0x10f294) & 0x00000f80) >> 7);
  784. }
  785. t->mr[0] = nv_rd32(dev, mr_base);
  786. t->mr[1] = nv_rd32(dev, mr_base + 0x04);
  787. t->mr[2] = nv_rd32(dev, mr_base + 0x20);
  788. t->mr[3] = nv_rd32(dev, mr_base + 0x24);
  789. t->odt = 0;
  790. t->drive_strength = 0;
  791. switch (dev_priv->vram_type) {
  792. case NV_MEM_TYPE_DDR3:
  793. t->odt |= (t->mr[1] & 0x200) >> 7;
  794. case NV_MEM_TYPE_DDR2:
  795. t->odt |= (t->mr[1] & 0x04) >> 2 |
  796. (t->mr[1] & 0x40) >> 5;
  797. break;
  798. case NV_MEM_TYPE_GDDR3:
  799. case NV_MEM_TYPE_GDDR5:
  800. t->drive_strength = t->mr[1] & 0x03;
  801. t->odt = (t->mr[1] & 0x0c) >> 2;
  802. break;
  803. default:
  804. break;
  805. }
  806. }
  807. int
  808. nouveau_mem_exec(struct nouveau_mem_exec_func *exec,
  809. struct nouveau_pm_level *perflvl)
  810. {
  811. struct drm_nouveau_private *dev_priv = exec->dev->dev_private;
  812. struct nouveau_pm_memtiming *info = &perflvl->timing;
  813. u32 tMRD = 1000, tCKSRE = 0, tCKSRX = 0, tXS = 0, tDLLK = 0;
  814. u32 mr[3] = { info->mr[0], info->mr[1], info->mr[2] };
  815. u32 mr1_dlloff;
  816. switch (dev_priv->vram_type) {
  817. case NV_MEM_TYPE_DDR2:
  818. tDLLK = 2000;
  819. mr1_dlloff = 0x00000001;
  820. break;
  821. case NV_MEM_TYPE_DDR3:
  822. tDLLK = 12000;
  823. tCKSRE = 2000;
  824. tXS = 1000;
  825. mr1_dlloff = 0x00000001;
  826. break;
  827. case NV_MEM_TYPE_GDDR3:
  828. tDLLK = 40000;
  829. mr1_dlloff = 0x00000040;
  830. break;
  831. default:
  832. NV_ERROR(exec->dev, "cannot reclock unsupported memtype\n");
  833. return -ENODEV;
  834. }
  835. /* fetch current MRs */
  836. switch (dev_priv->vram_type) {
  837. case NV_MEM_TYPE_GDDR3:
  838. case NV_MEM_TYPE_DDR3:
  839. mr[2] = exec->mrg(exec, 2);
  840. default:
  841. mr[1] = exec->mrg(exec, 1);
  842. mr[0] = exec->mrg(exec, 0);
  843. break;
  844. }
  845. /* DLL 'on' -> DLL 'off' mode, disable before entering self-refresh */
  846. if (!(mr[1] & mr1_dlloff) && (info->mr[1] & mr1_dlloff)) {
  847. exec->precharge(exec);
  848. exec->mrs (exec, 1, mr[1] | mr1_dlloff);
  849. exec->wait(exec, tMRD);
  850. }
  851. /* enter self-refresh mode */
  852. exec->precharge(exec);
  853. exec->refresh(exec);
  854. exec->refresh(exec);
  855. exec->refresh_auto(exec, false);
  856. exec->refresh_self(exec, true);
  857. exec->wait(exec, tCKSRE);
  858. /* modify input clock frequency */
  859. exec->clock_set(exec);
  860. /* exit self-refresh mode */
  861. exec->wait(exec, tCKSRX);
  862. exec->precharge(exec);
  863. exec->refresh_self(exec, false);
  864. exec->refresh_auto(exec, true);
  865. exec->wait(exec, tXS);
  866. exec->wait(exec, tXS);
  867. /* update MRs */
  868. if (mr[2] != info->mr[2]) {
  869. exec->mrs (exec, 2, info->mr[2]);
  870. exec->wait(exec, tMRD);
  871. }
  872. if (mr[1] != info->mr[1]) {
  873. /* need to keep DLL off until later, at least on GDDR3 */
  874. exec->mrs (exec, 1, info->mr[1] | (mr[1] & mr1_dlloff));
  875. exec->wait(exec, tMRD);
  876. }
  877. if (mr[0] != info->mr[0]) {
  878. exec->mrs (exec, 0, info->mr[0]);
  879. exec->wait(exec, tMRD);
  880. }
  881. /* update PFB timing registers */
  882. exec->timing_set(exec);
  883. /* DLL (enable + ) reset */
  884. if (!(info->mr[1] & mr1_dlloff)) {
  885. if (mr[1] & mr1_dlloff) {
  886. exec->mrs (exec, 1, info->mr[1]);
  887. exec->wait(exec, tMRD);
  888. }
  889. exec->mrs (exec, 0, info->mr[0] | 0x00000100);
  890. exec->wait(exec, tMRD);
  891. exec->mrs (exec, 0, info->mr[0] | 0x00000000);
  892. exec->wait(exec, tMRD);
  893. exec->wait(exec, tDLLK);
  894. if (dev_priv->vram_type == NV_MEM_TYPE_GDDR3)
  895. exec->precharge(exec);
  896. }
  897. return 0;
  898. }
  899. int
  900. nouveau_mem_vbios_type(struct drm_device *dev)
  901. {
  902. struct bit_entry M;
  903. u8 ramcfg = (nv_rd32(dev, 0x101000) & 0x0000003c) >> 2;
  904. if (!bit_table(dev, 'M', &M) || M.version != 2 || M.length < 5) {
  905. u8 *table = ROMPTR(dev, M.data[3]);
  906. if (table && table[0] == 0x10 && ramcfg < table[3]) {
  907. u8 *entry = table + table[1] + (ramcfg * table[2]);
  908. switch (entry[0] & 0x0f) {
  909. case 0: return NV_MEM_TYPE_DDR2;
  910. case 1: return NV_MEM_TYPE_DDR3;
  911. case 2: return NV_MEM_TYPE_GDDR3;
  912. case 3: return NV_MEM_TYPE_GDDR5;
  913. default:
  914. break;
  915. }
  916. }
  917. }
  918. return NV_MEM_TYPE_UNKNOWN;
  919. }
  920. static int
  921. nouveau_vram_manager_init(struct ttm_mem_type_manager *man, unsigned long psize)
  922. {
  923. /* nothing to do */
  924. return 0;
  925. }
  926. static int
  927. nouveau_vram_manager_fini(struct ttm_mem_type_manager *man)
  928. {
  929. /* nothing to do */
  930. return 0;
  931. }
  932. static inline void
  933. nouveau_mem_node_cleanup(struct nouveau_mem *node)
  934. {
  935. if (node->vma[0].node) {
  936. nouveau_vm_unmap(&node->vma[0]);
  937. nouveau_vm_put(&node->vma[0]);
  938. }
  939. if (node->vma[1].node) {
  940. nouveau_vm_unmap(&node->vma[1]);
  941. nouveau_vm_put(&node->vma[1]);
  942. }
  943. }
  944. static void
  945. nouveau_vram_manager_del(struct ttm_mem_type_manager *man,
  946. struct ttm_mem_reg *mem)
  947. {
  948. struct drm_nouveau_private *dev_priv = nouveau_bdev(man->bdev);
  949. struct nouveau_vram_engine *vram = &dev_priv->engine.vram;
  950. struct drm_device *dev = dev_priv->dev;
  951. nouveau_mem_node_cleanup(mem->mm_node);
  952. vram->put(dev, (struct nouveau_mem **)&mem->mm_node);
  953. }
  954. static int
  955. nouveau_vram_manager_new(struct ttm_mem_type_manager *man,
  956. struct ttm_buffer_object *bo,
  957. struct ttm_placement *placement,
  958. struct ttm_mem_reg *mem)
  959. {
  960. struct drm_nouveau_private *dev_priv = nouveau_bdev(man->bdev);
  961. struct nouveau_vram_engine *vram = &dev_priv->engine.vram;
  962. struct drm_device *dev = dev_priv->dev;
  963. struct nouveau_bo *nvbo = nouveau_bo(bo);
  964. struct nouveau_mem *node;
  965. u32 size_nc = 0;
  966. int ret;
  967. if (nvbo->tile_flags & NOUVEAU_GEM_TILE_NONCONTIG)
  968. size_nc = 1 << nvbo->page_shift;
  969. ret = vram->get(dev, mem->num_pages << PAGE_SHIFT,
  970. mem->page_alignment << PAGE_SHIFT, size_nc,
  971. (nvbo->tile_flags >> 8) & 0x3ff, &node);
  972. if (ret) {
  973. mem->mm_node = NULL;
  974. return (ret == -ENOSPC) ? 0 : ret;
  975. }
  976. node->page_shift = nvbo->page_shift;
  977. mem->mm_node = node;
  978. mem->start = node->offset >> PAGE_SHIFT;
  979. return 0;
  980. }
  981. void
  982. nouveau_vram_manager_debug(struct ttm_mem_type_manager *man, const char *prefix)
  983. {
  984. struct nouveau_mm *mm = man->priv;
  985. struct nouveau_mm_node *r;
  986. u32 total = 0, free = 0;
  987. mutex_lock(&mm->mutex);
  988. list_for_each_entry(r, &mm->nodes, nl_entry) {
  989. printk(KERN_DEBUG "%s %d: 0x%010llx 0x%010llx\n",
  990. prefix, r->type, ((u64)r->offset << 12),
  991. (((u64)r->offset + r->length) << 12));
  992. total += r->length;
  993. if (!r->type)
  994. free += r->length;
  995. }
  996. mutex_unlock(&mm->mutex);
  997. printk(KERN_DEBUG "%s total: 0x%010llx free: 0x%010llx\n",
  998. prefix, (u64)total << 12, (u64)free << 12);
  999. printk(KERN_DEBUG "%s block: 0x%08x\n",
  1000. prefix, mm->block_size << 12);
  1001. }
  1002. const struct ttm_mem_type_manager_func nouveau_vram_manager = {
  1003. nouveau_vram_manager_init,
  1004. nouveau_vram_manager_fini,
  1005. nouveau_vram_manager_new,
  1006. nouveau_vram_manager_del,
  1007. nouveau_vram_manager_debug
  1008. };
  1009. static int
  1010. nouveau_gart_manager_init(struct ttm_mem_type_manager *man, unsigned long psize)
  1011. {
  1012. return 0;
  1013. }
  1014. static int
  1015. nouveau_gart_manager_fini(struct ttm_mem_type_manager *man)
  1016. {
  1017. return 0;
  1018. }
  1019. static void
  1020. nouveau_gart_manager_del(struct ttm_mem_type_manager *man,
  1021. struct ttm_mem_reg *mem)
  1022. {
  1023. nouveau_mem_node_cleanup(mem->mm_node);
  1024. kfree(mem->mm_node);
  1025. mem->mm_node = NULL;
  1026. }
  1027. static int
  1028. nouveau_gart_manager_new(struct ttm_mem_type_manager *man,
  1029. struct ttm_buffer_object *bo,
  1030. struct ttm_placement *placement,
  1031. struct ttm_mem_reg *mem)
  1032. {
  1033. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  1034. struct nouveau_mem *node;
  1035. if (unlikely((mem->num_pages << PAGE_SHIFT) >=
  1036. dev_priv->gart_info.aper_size))
  1037. return -ENOMEM;
  1038. node = kzalloc(sizeof(*node), GFP_KERNEL);
  1039. if (!node)
  1040. return -ENOMEM;
  1041. node->page_shift = 12;
  1042. mem->mm_node = node;
  1043. mem->start = 0;
  1044. return 0;
  1045. }
  1046. void
  1047. nouveau_gart_manager_debug(struct ttm_mem_type_manager *man, const char *prefix)
  1048. {
  1049. }
  1050. const struct ttm_mem_type_manager_func nouveau_gart_manager = {
  1051. nouveau_gart_manager_init,
  1052. nouveau_gart_manager_fini,
  1053. nouveau_gart_manager_new,
  1054. nouveau_gart_manager_del,
  1055. nouveau_gart_manager_debug
  1056. };