nouveau_mem.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  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. * Keith Whitwell <keith@tungstengraphics.com>
  30. */
  31. #include "drmP.h"
  32. #include "drm.h"
  33. #include "drm_sarea.h"
  34. #include "nouveau_drv.h"
  35. #include "nouveau_pm.h"
  36. #include "nouveau_mm.h"
  37. #include "nouveau_vm.h"
  38. /*
  39. * NV10-NV40 tiling helpers
  40. */
  41. static void
  42. nv10_mem_update_tile_region(struct drm_device *dev,
  43. struct nouveau_tile_reg *tile, uint32_t addr,
  44. uint32_t size, uint32_t pitch, uint32_t flags)
  45. {
  46. struct drm_nouveau_private *dev_priv = dev->dev_private;
  47. struct nouveau_fifo_engine *pfifo = &dev_priv->engine.fifo;
  48. struct nouveau_fb_engine *pfb = &dev_priv->engine.fb;
  49. struct nouveau_pgraph_engine *pgraph = &dev_priv->engine.graph;
  50. int i = tile - dev_priv->tile.reg;
  51. unsigned long save;
  52. nouveau_fence_unref(&tile->fence);
  53. if (tile->pitch)
  54. pfb->free_tile_region(dev, i);
  55. if (pitch)
  56. pfb->init_tile_region(dev, i, addr, size, pitch, flags);
  57. spin_lock_irqsave(&dev_priv->context_switch_lock, save);
  58. pfifo->reassign(dev, false);
  59. pfifo->cache_pull(dev, false);
  60. nouveau_wait_for_idle(dev);
  61. pfb->set_tile_region(dev, i);
  62. pgraph->set_tile_region(dev, i);
  63. pfifo->cache_pull(dev, true);
  64. pfifo->reassign(dev, true);
  65. spin_unlock_irqrestore(&dev_priv->context_switch_lock, save);
  66. }
  67. static struct nouveau_tile_reg *
  68. nv10_mem_get_tile_region(struct drm_device *dev, int i)
  69. {
  70. struct drm_nouveau_private *dev_priv = dev->dev_private;
  71. struct nouveau_tile_reg *tile = &dev_priv->tile.reg[i];
  72. spin_lock(&dev_priv->tile.lock);
  73. if (!tile->used &&
  74. (!tile->fence || nouveau_fence_signalled(tile->fence)))
  75. tile->used = true;
  76. else
  77. tile = NULL;
  78. spin_unlock(&dev_priv->tile.lock);
  79. return tile;
  80. }
  81. void
  82. nv10_mem_put_tile_region(struct drm_device *dev, struct nouveau_tile_reg *tile,
  83. struct nouveau_fence *fence)
  84. {
  85. struct drm_nouveau_private *dev_priv = dev->dev_private;
  86. if (tile) {
  87. spin_lock(&dev_priv->tile.lock);
  88. if (fence) {
  89. /* Mark it as pending. */
  90. tile->fence = fence;
  91. nouveau_fence_ref(fence);
  92. }
  93. tile->used = false;
  94. spin_unlock(&dev_priv->tile.lock);
  95. }
  96. }
  97. struct nouveau_tile_reg *
  98. nv10_mem_set_tiling(struct drm_device *dev, uint32_t addr, uint32_t size,
  99. uint32_t pitch, uint32_t flags)
  100. {
  101. struct drm_nouveau_private *dev_priv = dev->dev_private;
  102. struct nouveau_fb_engine *pfb = &dev_priv->engine.fb;
  103. struct nouveau_tile_reg *tile, *found = NULL;
  104. int i;
  105. for (i = 0; i < pfb->num_tiles; i++) {
  106. tile = nv10_mem_get_tile_region(dev, i);
  107. if (pitch && !found) {
  108. found = tile;
  109. continue;
  110. } else if (tile && tile->pitch) {
  111. /* Kill an unused tile region. */
  112. nv10_mem_update_tile_region(dev, tile, 0, 0, 0, 0);
  113. }
  114. nv10_mem_put_tile_region(dev, tile, NULL);
  115. }
  116. if (found)
  117. nv10_mem_update_tile_region(dev, found, addr, size,
  118. pitch, flags);
  119. return found;
  120. }
  121. /*
  122. * Cleanup everything
  123. */
  124. void
  125. nouveau_mem_vram_fini(struct drm_device *dev)
  126. {
  127. struct drm_nouveau_private *dev_priv = dev->dev_private;
  128. nouveau_bo_unpin(dev_priv->vga_ram);
  129. nouveau_bo_ref(NULL, &dev_priv->vga_ram);
  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. static uint32_t
  161. nouveau_mem_detect_nv04(struct drm_device *dev)
  162. {
  163. uint32_t boot0 = nv_rd32(dev, NV04_PFB_BOOT_0);
  164. if (boot0 & 0x00000100)
  165. return (((boot0 >> 12) & 0xf) * 2 + 2) * 1024 * 1024;
  166. switch (boot0 & NV04_PFB_BOOT_0_RAM_AMOUNT) {
  167. case NV04_PFB_BOOT_0_RAM_AMOUNT_32MB:
  168. return 32 * 1024 * 1024;
  169. case NV04_PFB_BOOT_0_RAM_AMOUNT_16MB:
  170. return 16 * 1024 * 1024;
  171. case NV04_PFB_BOOT_0_RAM_AMOUNT_8MB:
  172. return 8 * 1024 * 1024;
  173. case NV04_PFB_BOOT_0_RAM_AMOUNT_4MB:
  174. return 4 * 1024 * 1024;
  175. }
  176. return 0;
  177. }
  178. static uint32_t
  179. nouveau_mem_detect_nforce(struct drm_device *dev)
  180. {
  181. struct drm_nouveau_private *dev_priv = dev->dev_private;
  182. struct pci_dev *bridge;
  183. uint32_t mem;
  184. bridge = pci_get_bus_and_slot(0, PCI_DEVFN(0, 1));
  185. if (!bridge) {
  186. NV_ERROR(dev, "no bridge device\n");
  187. return 0;
  188. }
  189. if (dev_priv->flags & NV_NFORCE) {
  190. pci_read_config_dword(bridge, 0x7C, &mem);
  191. return (uint64_t)(((mem >> 6) & 31) + 1)*1024*1024;
  192. } else
  193. if (dev_priv->flags & NV_NFORCE2) {
  194. pci_read_config_dword(bridge, 0x84, &mem);
  195. return (uint64_t)(((mem >> 4) & 127) + 1)*1024*1024;
  196. }
  197. NV_ERROR(dev, "impossible!\n");
  198. return 0;
  199. }
  200. int
  201. nouveau_mem_detect(struct drm_device *dev)
  202. {
  203. struct drm_nouveau_private *dev_priv = dev->dev_private;
  204. if (dev_priv->card_type == NV_04) {
  205. dev_priv->vram_size = nouveau_mem_detect_nv04(dev);
  206. } else
  207. if (dev_priv->flags & (NV_NFORCE | NV_NFORCE2)) {
  208. dev_priv->vram_size = nouveau_mem_detect_nforce(dev);
  209. } else
  210. if (dev_priv->card_type < NV_50) {
  211. dev_priv->vram_size = nv_rd32(dev, NV04_PFB_FIFO_DATA);
  212. dev_priv->vram_size &= NV10_PFB_FIFO_DATA_RAM_AMOUNT_MB_MASK;
  213. }
  214. if (dev_priv->vram_size)
  215. return 0;
  216. return -ENOMEM;
  217. }
  218. bool
  219. nouveau_mem_flags_valid(struct drm_device *dev, u32 tile_flags)
  220. {
  221. if (!(tile_flags & NOUVEAU_GEM_TILE_LAYOUT_MASK))
  222. return true;
  223. return false;
  224. }
  225. #if __OS_HAS_AGP
  226. static unsigned long
  227. get_agp_mode(struct drm_device *dev, unsigned long mode)
  228. {
  229. struct drm_nouveau_private *dev_priv = dev->dev_private;
  230. /*
  231. * FW seems to be broken on nv18, it makes the card lock up
  232. * randomly.
  233. */
  234. if (dev_priv->chipset == 0x18)
  235. mode &= ~PCI_AGP_COMMAND_FW;
  236. /*
  237. * AGP mode set in the command line.
  238. */
  239. if (nouveau_agpmode > 0) {
  240. bool agpv3 = mode & 0x8;
  241. int rate = agpv3 ? nouveau_agpmode / 4 : nouveau_agpmode;
  242. mode = (mode & ~0x7) | (rate & 0x7);
  243. }
  244. return mode;
  245. }
  246. #endif
  247. int
  248. nouveau_mem_reset_agp(struct drm_device *dev)
  249. {
  250. #if __OS_HAS_AGP
  251. uint32_t saved_pci_nv_1, pmc_enable;
  252. int ret;
  253. /* First of all, disable fast writes, otherwise if it's
  254. * already enabled in the AGP bridge and we disable the card's
  255. * AGP controller we might be locking ourselves out of it. */
  256. if ((nv_rd32(dev, NV04_PBUS_PCI_NV_19) |
  257. dev->agp->mode) & PCI_AGP_COMMAND_FW) {
  258. struct drm_agp_info info;
  259. struct drm_agp_mode mode;
  260. ret = drm_agp_info(dev, &info);
  261. if (ret)
  262. return ret;
  263. mode.mode = get_agp_mode(dev, info.mode) & ~PCI_AGP_COMMAND_FW;
  264. ret = drm_agp_enable(dev, mode);
  265. if (ret)
  266. return ret;
  267. }
  268. saved_pci_nv_1 = nv_rd32(dev, NV04_PBUS_PCI_NV_1);
  269. /* clear busmaster bit */
  270. nv_wr32(dev, NV04_PBUS_PCI_NV_1, saved_pci_nv_1 & ~0x4);
  271. /* disable AGP */
  272. nv_wr32(dev, NV04_PBUS_PCI_NV_19, 0);
  273. /* power cycle pgraph, if enabled */
  274. pmc_enable = nv_rd32(dev, NV03_PMC_ENABLE);
  275. if (pmc_enable & NV_PMC_ENABLE_PGRAPH) {
  276. nv_wr32(dev, NV03_PMC_ENABLE,
  277. pmc_enable & ~NV_PMC_ENABLE_PGRAPH);
  278. nv_wr32(dev, NV03_PMC_ENABLE, nv_rd32(dev, NV03_PMC_ENABLE) |
  279. NV_PMC_ENABLE_PGRAPH);
  280. }
  281. /* and restore (gives effect of resetting AGP) */
  282. nv_wr32(dev, NV04_PBUS_PCI_NV_1, saved_pci_nv_1);
  283. #endif
  284. return 0;
  285. }
  286. int
  287. nouveau_mem_init_agp(struct drm_device *dev)
  288. {
  289. #if __OS_HAS_AGP
  290. struct drm_nouveau_private *dev_priv = dev->dev_private;
  291. struct drm_agp_info info;
  292. struct drm_agp_mode mode;
  293. int ret;
  294. if (!dev->agp->acquired) {
  295. ret = drm_agp_acquire(dev);
  296. if (ret) {
  297. NV_ERROR(dev, "Unable to acquire AGP: %d\n", ret);
  298. return ret;
  299. }
  300. }
  301. nouveau_mem_reset_agp(dev);
  302. ret = drm_agp_info(dev, &info);
  303. if (ret) {
  304. NV_ERROR(dev, "Unable to get AGP info: %d\n", ret);
  305. return ret;
  306. }
  307. /* see agp.h for the AGPSTAT_* modes available */
  308. mode.mode = get_agp_mode(dev, info.mode);
  309. ret = drm_agp_enable(dev, mode);
  310. if (ret) {
  311. NV_ERROR(dev, "Unable to enable AGP: %d\n", ret);
  312. return ret;
  313. }
  314. dev_priv->gart_info.type = NOUVEAU_GART_AGP;
  315. dev_priv->gart_info.aper_base = info.aperture_base;
  316. dev_priv->gart_info.aper_size = info.aperture_size;
  317. #endif
  318. return 0;
  319. }
  320. int
  321. nouveau_mem_vram_init(struct drm_device *dev)
  322. {
  323. struct drm_nouveau_private *dev_priv = dev->dev_private;
  324. struct ttm_bo_device *bdev = &dev_priv->ttm.bdev;
  325. int ret, dma_bits;
  326. if (dev_priv->card_type >= NV_50 &&
  327. pci_dma_supported(dev->pdev, DMA_BIT_MASK(40)))
  328. dma_bits = 40;
  329. else
  330. dma_bits = 32;
  331. ret = pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(dma_bits));
  332. if (ret)
  333. return ret;
  334. dev_priv->fb_phys = pci_resource_start(dev->pdev, 1);
  335. ret = nouveau_ttm_global_init(dev_priv);
  336. if (ret)
  337. return ret;
  338. ret = ttm_bo_device_init(&dev_priv->ttm.bdev,
  339. dev_priv->ttm.bo_global_ref.ref.object,
  340. &nouveau_bo_driver, DRM_FILE_PAGE_OFFSET,
  341. dma_bits <= 32 ? true : false);
  342. if (ret) {
  343. NV_ERROR(dev, "Error initialising bo driver: %d\n", ret);
  344. return ret;
  345. }
  346. /* reserve space at end of VRAM for PRAMIN */
  347. if (dev_priv->chipset == 0x40 || dev_priv->chipset == 0x47 ||
  348. dev_priv->chipset == 0x49 || dev_priv->chipset == 0x4b)
  349. dev_priv->ramin_rsvd_vram = (2 * 1024 * 1024);
  350. else
  351. if (dev_priv->card_type >= NV_40)
  352. dev_priv->ramin_rsvd_vram = (1 * 1024 * 1024);
  353. else
  354. dev_priv->ramin_rsvd_vram = (512 * 1024);
  355. ret = dev_priv->engine.vram.init(dev);
  356. if (ret)
  357. return ret;
  358. NV_INFO(dev, "Detected %dMiB VRAM\n", (int)(dev_priv->vram_size >> 20));
  359. if (dev_priv->vram_sys_base) {
  360. NV_INFO(dev, "Stolen system memory at: 0x%010llx\n",
  361. dev_priv->vram_sys_base);
  362. }
  363. dev_priv->fb_available_size = dev_priv->vram_size;
  364. dev_priv->fb_mappable_pages = dev_priv->fb_available_size;
  365. if (dev_priv->fb_mappable_pages > pci_resource_len(dev->pdev, 1))
  366. dev_priv->fb_mappable_pages = pci_resource_len(dev->pdev, 1);
  367. dev_priv->fb_mappable_pages >>= PAGE_SHIFT;
  368. dev_priv->fb_available_size -= dev_priv->ramin_rsvd_vram;
  369. dev_priv->fb_aper_free = dev_priv->fb_available_size;
  370. /* mappable vram */
  371. ret = ttm_bo_init_mm(bdev, TTM_PL_VRAM,
  372. dev_priv->fb_available_size >> PAGE_SHIFT);
  373. if (ret) {
  374. NV_ERROR(dev, "Failed VRAM mm init: %d\n", ret);
  375. return ret;
  376. }
  377. ret = nouveau_bo_new(dev, NULL, 256*1024, 0, TTM_PL_FLAG_VRAM,
  378. 0, 0, true, true, &dev_priv->vga_ram);
  379. if (ret == 0)
  380. ret = nouveau_bo_pin(dev_priv->vga_ram, TTM_PL_FLAG_VRAM);
  381. if (ret) {
  382. NV_WARN(dev, "failed to reserve VGA memory\n");
  383. nouveau_bo_ref(NULL, &dev_priv->vga_ram);
  384. }
  385. dev_priv->fb_mtrr = drm_mtrr_add(pci_resource_start(dev->pdev, 1),
  386. pci_resource_len(dev->pdev, 1),
  387. DRM_MTRR_WC);
  388. return 0;
  389. }
  390. int
  391. nouveau_mem_gart_init(struct drm_device *dev)
  392. {
  393. struct drm_nouveau_private *dev_priv = dev->dev_private;
  394. struct ttm_bo_device *bdev = &dev_priv->ttm.bdev;
  395. int ret;
  396. dev_priv->gart_info.type = NOUVEAU_GART_NONE;
  397. #if !defined(__powerpc__) && !defined(__ia64__)
  398. if (drm_device_is_agp(dev) && dev->agp && nouveau_agpmode) {
  399. ret = nouveau_mem_init_agp(dev);
  400. if (ret)
  401. NV_ERROR(dev, "Error initialising AGP: %d\n", ret);
  402. }
  403. #endif
  404. if (dev_priv->gart_info.type == NOUVEAU_GART_NONE) {
  405. ret = nouveau_sgdma_init(dev);
  406. if (ret) {
  407. NV_ERROR(dev, "Error initialising PCI(E): %d\n", ret);
  408. return ret;
  409. }
  410. }
  411. NV_INFO(dev, "%d MiB GART (aperture)\n",
  412. (int)(dev_priv->gart_info.aper_size >> 20));
  413. dev_priv->gart_info.aper_free = dev_priv->gart_info.aper_size;
  414. ret = ttm_bo_init_mm(bdev, TTM_PL_TT,
  415. dev_priv->gart_info.aper_size >> PAGE_SHIFT);
  416. if (ret) {
  417. NV_ERROR(dev, "Failed TT mm init: %d\n", ret);
  418. return ret;
  419. }
  420. return 0;
  421. }
  422. void
  423. nouveau_mem_timing_init(struct drm_device *dev)
  424. {
  425. /* cards < NVC0 only */
  426. struct drm_nouveau_private *dev_priv = dev->dev_private;
  427. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  428. struct nouveau_pm_memtimings *memtimings = &pm->memtimings;
  429. struct nvbios *bios = &dev_priv->vbios;
  430. struct bit_entry P;
  431. u8 tUNK_0, tUNK_1, tUNK_2;
  432. u8 tRP; /* Byte 3 */
  433. u8 tRAS; /* Byte 5 */
  434. u8 tRFC; /* Byte 7 */
  435. u8 tRC; /* Byte 9 */
  436. u8 tUNK_10, tUNK_11, tUNK_12, tUNK_13, tUNK_14;
  437. u8 tUNK_18, tUNK_19, tUNK_20, tUNK_21;
  438. u8 *mem = NULL, *entry;
  439. int i, recordlen, entries;
  440. if (bios->type == NVBIOS_BIT) {
  441. if (bit_table(dev, 'P', &P))
  442. return;
  443. if (P.version == 1)
  444. mem = ROMPTR(bios, P.data[4]);
  445. else
  446. if (P.version == 2)
  447. mem = ROMPTR(bios, P.data[8]);
  448. else {
  449. NV_WARN(dev, "unknown mem for BIT P %d\n", P.version);
  450. }
  451. } else {
  452. NV_DEBUG(dev, "BMP version too old for memory\n");
  453. return;
  454. }
  455. if (!mem) {
  456. NV_DEBUG(dev, "memory timing table pointer invalid\n");
  457. return;
  458. }
  459. if (mem[0] != 0x10) {
  460. NV_WARN(dev, "memory timing table 0x%02x unknown\n", mem[0]);
  461. return;
  462. }
  463. /* validate record length */
  464. entries = mem[2];
  465. recordlen = mem[3];
  466. if (recordlen < 15) {
  467. NV_ERROR(dev, "mem timing table length unknown: %d\n", mem[3]);
  468. return;
  469. }
  470. /* parse vbios entries into common format */
  471. memtimings->timing =
  472. kcalloc(entries, sizeof(*memtimings->timing), GFP_KERNEL);
  473. if (!memtimings->timing)
  474. return;
  475. entry = mem + mem[1];
  476. for (i = 0; i < entries; i++, entry += recordlen) {
  477. struct nouveau_pm_memtiming *timing = &pm->memtimings.timing[i];
  478. if (entry[0] == 0)
  479. continue;
  480. tUNK_18 = 1;
  481. tUNK_19 = 1;
  482. tUNK_20 = 0;
  483. tUNK_21 = 0;
  484. switch (min(recordlen, 22)) {
  485. case 22:
  486. tUNK_21 = entry[21];
  487. case 21:
  488. tUNK_20 = entry[20];
  489. case 20:
  490. tUNK_19 = entry[19];
  491. case 19:
  492. tUNK_18 = entry[18];
  493. default:
  494. tUNK_0 = entry[0];
  495. tUNK_1 = entry[1];
  496. tUNK_2 = entry[2];
  497. tRP = entry[3];
  498. tRAS = entry[5];
  499. tRFC = entry[7];
  500. tRC = entry[9];
  501. tUNK_10 = entry[10];
  502. tUNK_11 = entry[11];
  503. tUNK_12 = entry[12];
  504. tUNK_13 = entry[13];
  505. tUNK_14 = entry[14];
  506. break;
  507. }
  508. timing->reg_100220 = (tRC << 24 | tRFC << 16 | tRAS << 8 | tRP);
  509. /* XXX: I don't trust the -1's and +1's... they must come
  510. * from somewhere! */
  511. timing->reg_100224 = ((tUNK_0 + tUNK_19 + 1) << 24 |
  512. tUNK_18 << 16 |
  513. (tUNK_1 + tUNK_19 + 1) << 8 |
  514. (tUNK_2 - 1));
  515. timing->reg_100228 = (tUNK_12 << 16 | tUNK_11 << 8 | tUNK_10);
  516. if(recordlen > 19) {
  517. timing->reg_100228 += (tUNK_19 - 1) << 24;
  518. }/* I cannot back-up this else-statement right now
  519. else {
  520. timing->reg_100228 += tUNK_12 << 24;
  521. }*/
  522. /* XXX: reg_10022c */
  523. timing->reg_10022c = tUNK_2 - 1;
  524. timing->reg_100230 = (tUNK_20 << 24 | tUNK_21 << 16 |
  525. tUNK_13 << 8 | tUNK_13);
  526. /* XXX: +6? */
  527. timing->reg_100234 = (tRAS << 24 | (tUNK_19 + 6) << 8 | tRC);
  528. timing->reg_100234 += max(tUNK_10,tUNK_11) << 16;
  529. /* XXX; reg_100238, reg_10023c
  530. * reg: 0x00??????
  531. * reg_10023c:
  532. * 0 for pre-NV50 cards
  533. * 0x????0202 for NV50+ cards (empirical evidence) */
  534. if(dev_priv->card_type >= NV_50) {
  535. timing->reg_10023c = 0x202;
  536. }
  537. NV_DEBUG(dev, "Entry %d: 220: %08x %08x %08x %08x\n", i,
  538. timing->reg_100220, timing->reg_100224,
  539. timing->reg_100228, timing->reg_10022c);
  540. NV_DEBUG(dev, " 230: %08x %08x %08x %08x\n",
  541. timing->reg_100230, timing->reg_100234,
  542. timing->reg_100238, timing->reg_10023c);
  543. }
  544. memtimings->nr_timing = entries;
  545. memtimings->supported = true;
  546. }
  547. void
  548. nouveau_mem_timing_fini(struct drm_device *dev)
  549. {
  550. struct drm_nouveau_private *dev_priv = dev->dev_private;
  551. struct nouveau_pm_memtimings *mem = &dev_priv->engine.pm.memtimings;
  552. kfree(mem->timing);
  553. }
  554. static int
  555. nouveau_vram_manager_init(struct ttm_mem_type_manager *man, unsigned long p_size)
  556. {
  557. struct drm_nouveau_private *dev_priv = nouveau_bdev(man->bdev);
  558. struct nouveau_mm *mm;
  559. u32 b_size;
  560. int ret;
  561. p_size = (p_size << PAGE_SHIFT) >> 12;
  562. b_size = dev_priv->vram_rblock_size >> 12;
  563. ret = nouveau_mm_init(&mm, 0, p_size, b_size);
  564. if (ret)
  565. return ret;
  566. man->priv = mm;
  567. return 0;
  568. }
  569. static int
  570. nouveau_vram_manager_fini(struct ttm_mem_type_manager *man)
  571. {
  572. struct nouveau_mm *mm = man->priv;
  573. int ret;
  574. ret = nouveau_mm_fini(&mm);
  575. if (ret)
  576. return ret;
  577. man->priv = NULL;
  578. return 0;
  579. }
  580. static void
  581. nouveau_vram_manager_del(struct ttm_mem_type_manager *man,
  582. struct ttm_mem_reg *mem)
  583. {
  584. struct drm_nouveau_private *dev_priv = nouveau_bdev(man->bdev);
  585. struct nouveau_vram_engine *vram = &dev_priv->engine.vram;
  586. struct drm_device *dev = dev_priv->dev;
  587. vram->put(dev, (struct nouveau_vram **)&mem->mm_node);
  588. }
  589. static int
  590. nouveau_vram_manager_new(struct ttm_mem_type_manager *man,
  591. struct ttm_buffer_object *bo,
  592. struct ttm_placement *placement,
  593. struct ttm_mem_reg *mem)
  594. {
  595. struct drm_nouveau_private *dev_priv = nouveau_bdev(man->bdev);
  596. struct nouveau_vram_engine *vram = &dev_priv->engine.vram;
  597. struct drm_device *dev = dev_priv->dev;
  598. struct nouveau_bo *nvbo = nouveau_bo(bo);
  599. struct nouveau_vram *node;
  600. u32 size_nc = 0;
  601. int ret;
  602. if (nvbo->tile_flags & NOUVEAU_GEM_TILE_NONCONTIG)
  603. size_nc = 1 << nvbo->vma.node->type;
  604. ret = vram->get(dev, mem->num_pages << PAGE_SHIFT,
  605. mem->page_alignment << PAGE_SHIFT, size_nc,
  606. (nvbo->tile_flags >> 8) & 0xff, &node);
  607. if (ret)
  608. return ret;
  609. node->page_shift = 12;
  610. if (nvbo->vma.node)
  611. node->page_shift = nvbo->vma.node->type;
  612. mem->mm_node = node;
  613. mem->start = node->offset >> PAGE_SHIFT;
  614. return 0;
  615. }
  616. void
  617. nouveau_vram_manager_debug(struct ttm_mem_type_manager *man, const char *prefix)
  618. {
  619. struct nouveau_mm *mm = man->priv;
  620. struct nouveau_mm_node *r;
  621. u64 total = 0, ttotal[3] = {}, tused[3] = {}, tfree[3] = {};
  622. int i;
  623. mutex_lock(&mm->mutex);
  624. list_for_each_entry(r, &mm->nodes, nl_entry) {
  625. printk(KERN_DEBUG "%s %s-%d: 0x%010llx 0x%010llx\n",
  626. prefix, r->free ? "free" : "used", r->type,
  627. ((u64)r->offset << 12),
  628. (((u64)r->offset + r->length) << 12));
  629. total += r->length;
  630. ttotal[r->type] += r->length;
  631. if (r->free)
  632. tfree[r->type] += r->length;
  633. else
  634. tused[r->type] += r->length;
  635. }
  636. mutex_unlock(&mm->mutex);
  637. printk(KERN_DEBUG "%s total: 0x%010llx\n", prefix, total << 12);
  638. for (i = 0; i < 3; i++) {
  639. printk(KERN_DEBUG "%s type %d: 0x%010llx, "
  640. "used 0x%010llx, free 0x%010llx\n", prefix,
  641. i, ttotal[i] << 12, tused[i] << 12, tfree[i] << 12);
  642. }
  643. }
  644. const struct ttm_mem_type_manager_func nouveau_vram_manager = {
  645. nouveau_vram_manager_init,
  646. nouveau_vram_manager_fini,
  647. nouveau_vram_manager_new,
  648. nouveau_vram_manager_del,
  649. nouveau_vram_manager_debug
  650. };