nouveau_mem.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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. dev_priv->ttm.bdev.dev = dev->dev;
  339. ret = ttm_bo_device_init(&dev_priv->ttm.bdev,
  340. dev_priv->ttm.bo_global_ref.ref.object,
  341. &nouveau_bo_driver, DRM_FILE_PAGE_OFFSET,
  342. dma_bits <= 32 ? true : false);
  343. if (ret) {
  344. NV_ERROR(dev, "Error initialising bo driver: %d\n", ret);
  345. return ret;
  346. }
  347. /* reserve space at end of VRAM for PRAMIN */
  348. if (dev_priv->chipset == 0x40 || dev_priv->chipset == 0x47 ||
  349. dev_priv->chipset == 0x49 || dev_priv->chipset == 0x4b)
  350. dev_priv->ramin_rsvd_vram = (2 * 1024 * 1024);
  351. else
  352. if (dev_priv->card_type >= NV_40)
  353. dev_priv->ramin_rsvd_vram = (1 * 1024 * 1024);
  354. else
  355. dev_priv->ramin_rsvd_vram = (512 * 1024);
  356. ret = dev_priv->engine.vram.init(dev);
  357. if (ret)
  358. return ret;
  359. NV_INFO(dev, "Detected %dMiB VRAM\n", (int)(dev_priv->vram_size >> 20));
  360. if (dev_priv->vram_sys_base) {
  361. NV_INFO(dev, "Stolen system memory at: 0x%010llx\n",
  362. dev_priv->vram_sys_base);
  363. }
  364. dev_priv->fb_available_size = dev_priv->vram_size;
  365. dev_priv->fb_mappable_pages = dev_priv->fb_available_size;
  366. if (dev_priv->fb_mappable_pages > pci_resource_len(dev->pdev, 1))
  367. dev_priv->fb_mappable_pages = pci_resource_len(dev->pdev, 1);
  368. dev_priv->fb_mappable_pages >>= PAGE_SHIFT;
  369. dev_priv->fb_available_size -= dev_priv->ramin_rsvd_vram;
  370. dev_priv->fb_aper_free = dev_priv->fb_available_size;
  371. /* mappable vram */
  372. ret = ttm_bo_init_mm(bdev, TTM_PL_VRAM,
  373. dev_priv->fb_available_size >> PAGE_SHIFT);
  374. if (ret) {
  375. NV_ERROR(dev, "Failed VRAM mm init: %d\n", ret);
  376. return ret;
  377. }
  378. ret = nouveau_bo_new(dev, NULL, 256*1024, 0, TTM_PL_FLAG_VRAM,
  379. 0, 0, true, true, &dev_priv->vga_ram);
  380. if (ret == 0)
  381. ret = nouveau_bo_pin(dev_priv->vga_ram, TTM_PL_FLAG_VRAM);
  382. if (ret) {
  383. NV_WARN(dev, "failed to reserve VGA memory\n");
  384. nouveau_bo_ref(NULL, &dev_priv->vga_ram);
  385. }
  386. dev_priv->fb_mtrr = drm_mtrr_add(pci_resource_start(dev->pdev, 1),
  387. pci_resource_len(dev->pdev, 1),
  388. DRM_MTRR_WC);
  389. return 0;
  390. }
  391. int
  392. nouveau_mem_gart_init(struct drm_device *dev)
  393. {
  394. struct drm_nouveau_private *dev_priv = dev->dev_private;
  395. struct ttm_bo_device *bdev = &dev_priv->ttm.bdev;
  396. int ret;
  397. dev_priv->gart_info.type = NOUVEAU_GART_NONE;
  398. #if !defined(__powerpc__) && !defined(__ia64__)
  399. if (drm_pci_device_is_agp(dev) && dev->agp && nouveau_agpmode) {
  400. ret = nouveau_mem_init_agp(dev);
  401. if (ret)
  402. NV_ERROR(dev, "Error initialising AGP: %d\n", ret);
  403. }
  404. #endif
  405. if (dev_priv->gart_info.type == NOUVEAU_GART_NONE) {
  406. ret = nouveau_sgdma_init(dev);
  407. if (ret) {
  408. NV_ERROR(dev, "Error initialising PCI(E): %d\n", ret);
  409. return ret;
  410. }
  411. }
  412. NV_INFO(dev, "%d MiB GART (aperture)\n",
  413. (int)(dev_priv->gart_info.aper_size >> 20));
  414. dev_priv->gart_info.aper_free = dev_priv->gart_info.aper_size;
  415. ret = ttm_bo_init_mm(bdev, TTM_PL_TT,
  416. dev_priv->gart_info.aper_size >> PAGE_SHIFT);
  417. if (ret) {
  418. NV_ERROR(dev, "Failed TT mm init: %d\n", ret);
  419. return ret;
  420. }
  421. return 0;
  422. }
  423. void
  424. nouveau_mem_timing_init(struct drm_device *dev)
  425. {
  426. /* cards < NVC0 only */
  427. struct drm_nouveau_private *dev_priv = dev->dev_private;
  428. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  429. struct nouveau_pm_memtimings *memtimings = &pm->memtimings;
  430. struct nvbios *bios = &dev_priv->vbios;
  431. struct bit_entry P;
  432. u8 tUNK_0, tUNK_1, tUNK_2;
  433. u8 tRP; /* Byte 3 */
  434. u8 tRAS; /* Byte 5 */
  435. u8 tRFC; /* Byte 7 */
  436. u8 tRC; /* Byte 9 */
  437. u8 tUNK_10, tUNK_11, tUNK_12, tUNK_13, tUNK_14;
  438. u8 tUNK_18, tUNK_19, tUNK_20, tUNK_21;
  439. u8 *mem = NULL, *entry;
  440. int i, recordlen, entries;
  441. if (bios->type == NVBIOS_BIT) {
  442. if (bit_table(dev, 'P', &P))
  443. return;
  444. if (P.version == 1)
  445. mem = ROMPTR(bios, P.data[4]);
  446. else
  447. if (P.version == 2)
  448. mem = ROMPTR(bios, P.data[8]);
  449. else {
  450. NV_WARN(dev, "unknown mem for BIT P %d\n", P.version);
  451. }
  452. } else {
  453. NV_DEBUG(dev, "BMP version too old for memory\n");
  454. return;
  455. }
  456. if (!mem) {
  457. NV_DEBUG(dev, "memory timing table pointer invalid\n");
  458. return;
  459. }
  460. if (mem[0] != 0x10) {
  461. NV_WARN(dev, "memory timing table 0x%02x unknown\n", mem[0]);
  462. return;
  463. }
  464. /* validate record length */
  465. entries = mem[2];
  466. recordlen = mem[3];
  467. if (recordlen < 15) {
  468. NV_ERROR(dev, "mem timing table length unknown: %d\n", mem[3]);
  469. return;
  470. }
  471. /* parse vbios entries into common format */
  472. memtimings->timing =
  473. kcalloc(entries, sizeof(*memtimings->timing), GFP_KERNEL);
  474. if (!memtimings->timing)
  475. return;
  476. entry = mem + mem[1];
  477. for (i = 0; i < entries; i++, entry += recordlen) {
  478. struct nouveau_pm_memtiming *timing = &pm->memtimings.timing[i];
  479. if (entry[0] == 0)
  480. continue;
  481. tUNK_18 = 1;
  482. tUNK_19 = 1;
  483. tUNK_20 = 0;
  484. tUNK_21 = 0;
  485. switch (min(recordlen, 22)) {
  486. case 22:
  487. tUNK_21 = entry[21];
  488. case 21:
  489. tUNK_20 = entry[20];
  490. case 20:
  491. tUNK_19 = entry[19];
  492. case 19:
  493. tUNK_18 = entry[18];
  494. default:
  495. tUNK_0 = entry[0];
  496. tUNK_1 = entry[1];
  497. tUNK_2 = entry[2];
  498. tRP = entry[3];
  499. tRAS = entry[5];
  500. tRFC = entry[7];
  501. tRC = entry[9];
  502. tUNK_10 = entry[10];
  503. tUNK_11 = entry[11];
  504. tUNK_12 = entry[12];
  505. tUNK_13 = entry[13];
  506. tUNK_14 = entry[14];
  507. break;
  508. }
  509. timing->reg_100220 = (tRC << 24 | tRFC << 16 | tRAS << 8 | tRP);
  510. /* XXX: I don't trust the -1's and +1's... they must come
  511. * from somewhere! */
  512. timing->reg_100224 = ((tUNK_0 + tUNK_19 + 1) << 24 |
  513. tUNK_18 << 16 |
  514. (tUNK_1 + tUNK_19 + 1) << 8 |
  515. (tUNK_2 - 1));
  516. timing->reg_100228 = (tUNK_12 << 16 | tUNK_11 << 8 | tUNK_10);
  517. if(recordlen > 19) {
  518. timing->reg_100228 += (tUNK_19 - 1) << 24;
  519. }/* I cannot back-up this else-statement right now
  520. else {
  521. timing->reg_100228 += tUNK_12 << 24;
  522. }*/
  523. /* XXX: reg_10022c */
  524. timing->reg_10022c = tUNK_2 - 1;
  525. timing->reg_100230 = (tUNK_20 << 24 | tUNK_21 << 16 |
  526. tUNK_13 << 8 | tUNK_13);
  527. /* XXX: +6? */
  528. timing->reg_100234 = (tRAS << 24 | (tUNK_19 + 6) << 8 | tRC);
  529. timing->reg_100234 += max(tUNK_10,tUNK_11) << 16;
  530. /* XXX; reg_100238, reg_10023c
  531. * reg: 0x00??????
  532. * reg_10023c:
  533. * 0 for pre-NV50 cards
  534. * 0x????0202 for NV50+ cards (empirical evidence) */
  535. if(dev_priv->card_type >= NV_50) {
  536. timing->reg_10023c = 0x202;
  537. }
  538. NV_DEBUG(dev, "Entry %d: 220: %08x %08x %08x %08x\n", i,
  539. timing->reg_100220, timing->reg_100224,
  540. timing->reg_100228, timing->reg_10022c);
  541. NV_DEBUG(dev, " 230: %08x %08x %08x %08x\n",
  542. timing->reg_100230, timing->reg_100234,
  543. timing->reg_100238, timing->reg_10023c);
  544. }
  545. memtimings->nr_timing = entries;
  546. memtimings->supported = true;
  547. }
  548. void
  549. nouveau_mem_timing_fini(struct drm_device *dev)
  550. {
  551. struct drm_nouveau_private *dev_priv = dev->dev_private;
  552. struct nouveau_pm_memtimings *mem = &dev_priv->engine.pm.memtimings;
  553. kfree(mem->timing);
  554. }
  555. static int
  556. nouveau_vram_manager_init(struct ttm_mem_type_manager *man, unsigned long p_size)
  557. {
  558. struct drm_nouveau_private *dev_priv = nouveau_bdev(man->bdev);
  559. struct nouveau_mm *mm;
  560. u32 b_size;
  561. int ret;
  562. p_size = (p_size << PAGE_SHIFT) >> 12;
  563. b_size = dev_priv->vram_rblock_size >> 12;
  564. ret = nouveau_mm_init(&mm, 0, p_size, b_size);
  565. if (ret)
  566. return ret;
  567. man->priv = mm;
  568. return 0;
  569. }
  570. static int
  571. nouveau_vram_manager_fini(struct ttm_mem_type_manager *man)
  572. {
  573. struct nouveau_mm *mm = man->priv;
  574. int ret;
  575. ret = nouveau_mm_fini(&mm);
  576. if (ret)
  577. return ret;
  578. man->priv = NULL;
  579. return 0;
  580. }
  581. static void
  582. nouveau_vram_manager_del(struct ttm_mem_type_manager *man,
  583. struct ttm_mem_reg *mem)
  584. {
  585. struct drm_nouveau_private *dev_priv = nouveau_bdev(man->bdev);
  586. struct nouveau_vram_engine *vram = &dev_priv->engine.vram;
  587. struct drm_device *dev = dev_priv->dev;
  588. vram->put(dev, (struct nouveau_vram **)&mem->mm_node);
  589. }
  590. static int
  591. nouveau_vram_manager_new(struct ttm_mem_type_manager *man,
  592. struct ttm_buffer_object *bo,
  593. struct ttm_placement *placement,
  594. struct ttm_mem_reg *mem)
  595. {
  596. struct drm_nouveau_private *dev_priv = nouveau_bdev(man->bdev);
  597. struct nouveau_vram_engine *vram = &dev_priv->engine.vram;
  598. struct drm_device *dev = dev_priv->dev;
  599. struct nouveau_bo *nvbo = nouveau_bo(bo);
  600. struct nouveau_vram *node;
  601. u32 size_nc = 0;
  602. int ret;
  603. if (nvbo->tile_flags & NOUVEAU_GEM_TILE_NONCONTIG)
  604. size_nc = 1 << nvbo->vma.node->type;
  605. ret = vram->get(dev, mem->num_pages << PAGE_SHIFT,
  606. mem->page_alignment << PAGE_SHIFT, size_nc,
  607. (nvbo->tile_flags >> 8) & 0xff, &node);
  608. if (ret)
  609. return ret;
  610. node->page_shift = 12;
  611. if (nvbo->vma.node)
  612. node->page_shift = nvbo->vma.node->type;
  613. mem->mm_node = node;
  614. mem->start = node->offset >> PAGE_SHIFT;
  615. return 0;
  616. }
  617. void
  618. nouveau_vram_manager_debug(struct ttm_mem_type_manager *man, const char *prefix)
  619. {
  620. struct nouveau_mm *mm = man->priv;
  621. struct nouveau_mm_node *r;
  622. u32 total = 0, free = 0;
  623. mutex_lock(&mm->mutex);
  624. list_for_each_entry(r, &mm->nodes, nl_entry) {
  625. printk(KERN_DEBUG "%s %d: 0x%010llx 0x%010llx\n",
  626. prefix, r->type, ((u64)r->offset << 12),
  627. (((u64)r->offset + r->length) << 12));
  628. total += r->length;
  629. if (!r->type)
  630. free += r->length;
  631. }
  632. mutex_unlock(&mm->mutex);
  633. printk(KERN_DEBUG "%s total: 0x%010llx free: 0x%010llx\n",
  634. prefix, (u64)total << 12, (u64)free << 12);
  635. printk(KERN_DEBUG "%s block: 0x%08x\n",
  636. prefix, mm->block_size << 12);
  637. }
  638. const struct ttm_mem_type_manager_func nouveau_vram_manager = {
  639. nouveau_vram_manager_init,
  640. nouveau_vram_manager_fini,
  641. nouveau_vram_manager_new,
  642. nouveau_vram_manager_del,
  643. nouveau_vram_manager_debug
  644. };