nouveau_mem.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  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. 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. pfifo->reassign(dev, false);
  58. pfifo->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. pfifo->cache_pull(dev, true);
  66. pfifo->reassign(dev, true);
  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_signalled(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. 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. dma_bits = 32;
  327. if (dev_priv->card_type >= NV_50) {
  328. if (pci_dma_supported(dev->pdev, DMA_BIT_MASK(40)))
  329. dma_bits = 40;
  330. } else
  331. if (0 && pci_is_pcie(dev->pdev) &&
  332. dev_priv->chipset > 0x40 &&
  333. dev_priv->chipset != 0x45) {
  334. if (pci_dma_supported(dev->pdev, DMA_BIT_MASK(39)))
  335. dma_bits = 39;
  336. }
  337. ret = pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(dma_bits));
  338. if (ret)
  339. return ret;
  340. ret = nouveau_ttm_global_init(dev_priv);
  341. if (ret)
  342. return ret;
  343. ret = ttm_bo_device_init(&dev_priv->ttm.bdev,
  344. dev_priv->ttm.bo_global_ref.ref.object,
  345. &nouveau_bo_driver, DRM_FILE_PAGE_OFFSET,
  346. dma_bits <= 32 ? true : false);
  347. if (ret) {
  348. NV_ERROR(dev, "Error initialising bo driver: %d\n", ret);
  349. return ret;
  350. }
  351. NV_INFO(dev, "Detected %dMiB VRAM\n", (int)(dev_priv->vram_size >> 20));
  352. if (dev_priv->vram_sys_base) {
  353. NV_INFO(dev, "Stolen system memory at: 0x%010llx\n",
  354. dev_priv->vram_sys_base);
  355. }
  356. dev_priv->fb_available_size = dev_priv->vram_size;
  357. dev_priv->fb_mappable_pages = dev_priv->fb_available_size;
  358. if (dev_priv->fb_mappable_pages > pci_resource_len(dev->pdev, 1))
  359. dev_priv->fb_mappable_pages = pci_resource_len(dev->pdev, 1);
  360. dev_priv->fb_mappable_pages >>= PAGE_SHIFT;
  361. dev_priv->fb_available_size -= dev_priv->ramin_rsvd_vram;
  362. dev_priv->fb_aper_free = dev_priv->fb_available_size;
  363. /* mappable vram */
  364. ret = ttm_bo_init_mm(bdev, TTM_PL_VRAM,
  365. dev_priv->fb_available_size >> PAGE_SHIFT);
  366. if (ret) {
  367. NV_ERROR(dev, "Failed VRAM mm init: %d\n", ret);
  368. return ret;
  369. }
  370. if (dev_priv->card_type < NV_50) {
  371. ret = nouveau_bo_new(dev, 256*1024, 0, TTM_PL_FLAG_VRAM,
  372. 0, 0, &dev_priv->vga_ram);
  373. if (ret == 0)
  374. ret = nouveau_bo_pin(dev_priv->vga_ram,
  375. TTM_PL_FLAG_VRAM);
  376. if (ret) {
  377. NV_WARN(dev, "failed to reserve VGA memory\n");
  378. nouveau_bo_ref(NULL, &dev_priv->vga_ram);
  379. }
  380. }
  381. dev_priv->fb_mtrr = drm_mtrr_add(pci_resource_start(dev->pdev, 1),
  382. pci_resource_len(dev->pdev, 1),
  383. DRM_MTRR_WC);
  384. return 0;
  385. }
  386. int
  387. nouveau_mem_gart_init(struct drm_device *dev)
  388. {
  389. struct drm_nouveau_private *dev_priv = dev->dev_private;
  390. struct ttm_bo_device *bdev = &dev_priv->ttm.bdev;
  391. int ret;
  392. dev_priv->gart_info.type = NOUVEAU_GART_NONE;
  393. #if !defined(__powerpc__) && !defined(__ia64__)
  394. if (drm_pci_device_is_agp(dev) && dev->agp && nouveau_agpmode) {
  395. ret = nouveau_mem_init_agp(dev);
  396. if (ret)
  397. NV_ERROR(dev, "Error initialising AGP: %d\n", ret);
  398. }
  399. #endif
  400. if (dev_priv->gart_info.type == NOUVEAU_GART_NONE) {
  401. ret = nouveau_sgdma_init(dev);
  402. if (ret) {
  403. NV_ERROR(dev, "Error initialising PCI(E): %d\n", ret);
  404. return ret;
  405. }
  406. }
  407. NV_INFO(dev, "%d MiB GART (aperture)\n",
  408. (int)(dev_priv->gart_info.aper_size >> 20));
  409. dev_priv->gart_info.aper_free = dev_priv->gart_info.aper_size;
  410. ret = ttm_bo_init_mm(bdev, TTM_PL_TT,
  411. dev_priv->gart_info.aper_size >> PAGE_SHIFT);
  412. if (ret) {
  413. NV_ERROR(dev, "Failed TT mm init: %d\n", ret);
  414. return ret;
  415. }
  416. return 0;
  417. }
  418. /* XXX: For now a dummy. More samples required, possibly even a card
  419. * Called from nouveau_perf.c */
  420. void nv30_mem_timing_entry(struct drm_device *dev, struct nouveau_pm_tbl_header *hdr,
  421. struct nouveau_pm_tbl_entry *e, uint8_t magic_number,
  422. struct nouveau_pm_memtiming *timing) {
  423. NV_DEBUG(dev,"Timing entry format unknown, please contact nouveau developers");
  424. }
  425. void nv40_mem_timing_entry(struct drm_device *dev, struct nouveau_pm_tbl_header *hdr,
  426. struct nouveau_pm_tbl_entry *e, uint8_t magic_number,
  427. struct nouveau_pm_memtiming *timing) {
  428. timing->reg_0 = (e->tRC << 24 | e->tRFC << 16 | e->tRAS << 8 | e->tRP);
  429. /* XXX: I don't trust the -1's and +1's... they must come
  430. * from somewhere! */
  431. timing->reg_1 = (e->tWR + 2 + magic_number) << 24 |
  432. 1 << 16 |
  433. (e->tUNK_1 + 2 + magic_number) << 8 |
  434. (e->tCL + 2 - magic_number);
  435. timing->reg_2 = (magic_number << 24 | e->tUNK_12 << 16 | e->tUNK_11 << 8 | e->tUNK_10);
  436. timing->reg_2 |= 0x20200000;
  437. NV_DEBUG(dev, "Entry %d: 220: %08x %08x %08x\n", timing->id,
  438. timing->reg_0, timing->reg_1,timing->reg_2);
  439. }
  440. void nv50_mem_timing_entry(struct drm_device *dev, struct bit_entry *P, struct nouveau_pm_tbl_header *hdr,
  441. struct nouveau_pm_tbl_entry *e, uint8_t magic_number,struct nouveau_pm_memtiming *timing) {
  442. struct drm_nouveau_private *dev_priv = dev->dev_private;
  443. uint8_t unk18 = 1,
  444. unk19 = 1,
  445. unk20 = 0,
  446. unk21 = 0;
  447. switch (min(hdr->entry_len, (u8) 22)) {
  448. case 22:
  449. unk21 = e->tUNK_21;
  450. case 21:
  451. unk20 = e->tUNK_20;
  452. case 20:
  453. unk19 = e->tUNK_19;
  454. case 19:
  455. unk18 = e->tUNK_18;
  456. break;
  457. }
  458. timing->reg_0 = (e->tRC << 24 | e->tRFC << 16 | e->tRAS << 8 | e->tRP);
  459. /* XXX: I don't trust the -1's and +1's... they must come
  460. * from somewhere! */
  461. timing->reg_1 = (e->tWR + unk19 + 1 + magic_number) << 24 |
  462. max(unk18, (u8) 1) << 16 |
  463. (e->tUNK_1 + unk19 + 1 + magic_number) << 8;
  464. if (dev_priv->chipset == 0xa8) {
  465. timing->reg_1 |= (e->tCL - 1);
  466. } else {
  467. timing->reg_1 |= (e->tCL + 2 - magic_number);
  468. }
  469. timing->reg_2 = (e->tUNK_12 << 16 | e->tUNK_11 << 8 | e->tUNK_10);
  470. timing->reg_5 = (e->tRAS << 24 | e->tRC);
  471. timing->reg_5 += max(e->tUNK_10, e->tUNK_11) << 16;
  472. if (P->version == 1) {
  473. timing->reg_2 |= magic_number << 24;
  474. timing->reg_3 = (0x14 + e->tCL) << 24 |
  475. 0x16 << 16 |
  476. (e->tCL - 1) << 8 |
  477. (e->tCL - 1);
  478. timing->reg_4 = (nv_rd32(dev,0x10022c) & 0xffff0000) | e->tUNK_13 << 8 | e->tUNK_13;
  479. timing->reg_5 |= (e->tCL + 2) << 8;
  480. timing->reg_7 = 0x4000202 | (e->tCL - 1) << 16;
  481. } else {
  482. timing->reg_2 |= (unk19 - 1) << 24;
  483. /* XXX: reg_10022c for recentish cards pretty much unknown*/
  484. timing->reg_3 = e->tCL - 1;
  485. timing->reg_4 = (unk20 << 24 | unk21 << 16 |
  486. e->tUNK_13 << 8 | e->tUNK_13);
  487. /* XXX: +6? */
  488. timing->reg_5 |= (unk19 + 6) << 8;
  489. /* XXX: reg_10023c currently unknown
  490. * 10023c seen as 06xxxxxx, 0bxxxxxx or 0fxxxxxx */
  491. timing->reg_7 = 0x202;
  492. }
  493. NV_DEBUG(dev, "Entry %d: 220: %08x %08x %08x %08x\n", timing->id,
  494. timing->reg_0, timing->reg_1,
  495. timing->reg_2, timing->reg_3);
  496. NV_DEBUG(dev, " 230: %08x %08x %08x %08x\n",
  497. timing->reg_4, timing->reg_5,
  498. timing->reg_6, timing->reg_7);
  499. NV_DEBUG(dev, " 240: %08x\n", timing->reg_8);
  500. }
  501. void nvc0_mem_timing_entry(struct drm_device *dev, struct nouveau_pm_tbl_header *hdr,
  502. struct nouveau_pm_tbl_entry *e, struct nouveau_pm_memtiming *timing) {
  503. timing->reg_0 = (e->tRC << 24 | (e->tRFC & 0x7f) << 17 | e->tRAS << 8 | e->tRP);
  504. timing->reg_1 = (nv_rd32(dev,0x10f294) & 0xff000000) | (e->tUNK_11&0x0f) << 20 | (e->tUNK_19 << 7) | (e->tCL & 0x0f);
  505. timing->reg_2 = (nv_rd32(dev,0x10f298) & 0xff0000ff) | e->tWR << 16 | e->tUNK_1 << 8;
  506. timing->reg_3 = e->tUNK_20 << 9 | e->tUNK_13;
  507. timing->reg_4 = (nv_rd32(dev,0x10f2a0) & 0xfff000ff) | e->tUNK_12 << 15;
  508. NV_DEBUG(dev, "Entry %d: 290: %08x %08x %08x %08x\n", timing->id,
  509. timing->reg_0, timing->reg_1,
  510. timing->reg_2, timing->reg_3);
  511. NV_DEBUG(dev, " 2a0: %08x %08x %08x %08x\n",
  512. timing->reg_4, timing->reg_5,
  513. timing->reg_6, timing->reg_7);
  514. }
  515. /**
  516. * Processes the Memory Timing BIOS table, stores generated
  517. * register values
  518. * @pre init scripts were run, memtiming regs are initialized
  519. */
  520. void
  521. nouveau_mem_timing_init(struct drm_device *dev)
  522. {
  523. struct drm_nouveau_private *dev_priv = dev->dev_private;
  524. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  525. struct nouveau_pm_memtimings *memtimings = &pm->memtimings;
  526. struct nvbios *bios = &dev_priv->vbios;
  527. struct bit_entry P;
  528. struct nouveau_pm_tbl_header *hdr = NULL;
  529. uint8_t magic_number;
  530. u8 *entry;
  531. int i;
  532. if (bios->type == NVBIOS_BIT) {
  533. if (bit_table(dev, 'P', &P))
  534. return;
  535. if (P.version == 1)
  536. hdr = (struct nouveau_pm_tbl_header *) ROMPTR(bios, P.data[4]);
  537. else
  538. if (P.version == 2)
  539. hdr = (struct nouveau_pm_tbl_header *) ROMPTR(bios, P.data[8]);
  540. else {
  541. NV_WARN(dev, "unknown mem for BIT P %d\n", P.version);
  542. }
  543. } else {
  544. NV_DEBUG(dev, "BMP version too old for memory\n");
  545. return;
  546. }
  547. if (!hdr) {
  548. NV_DEBUG(dev, "memory timing table pointer invalid\n");
  549. return;
  550. }
  551. if (hdr->version != 0x10) {
  552. NV_WARN(dev, "memory timing table 0x%02x unknown\n", hdr->version);
  553. return;
  554. }
  555. /* validate record length */
  556. if (hdr->entry_len < 15) {
  557. NV_ERROR(dev, "mem timing table length unknown: %d\n", hdr->entry_len);
  558. return;
  559. }
  560. /* parse vbios entries into common format */
  561. memtimings->timing =
  562. kcalloc(hdr->entry_cnt, sizeof(*memtimings->timing), GFP_KERNEL);
  563. if (!memtimings->timing)
  564. return;
  565. /* Get "some number" from the timing reg for NV_40 and NV_50
  566. * Used in calculations later... source unknown */
  567. magic_number = 0;
  568. if (P.version == 1) {
  569. magic_number = (nv_rd32(dev, 0x100228) & 0x0f000000) >> 24;
  570. }
  571. entry = (u8*) hdr + hdr->header_len;
  572. for (i = 0; i < hdr->entry_cnt; i++, entry += hdr->entry_len) {
  573. struct nouveau_pm_memtiming *timing = &pm->memtimings.timing[i];
  574. if (entry[0] == 0)
  575. continue;
  576. timing->id = i;
  577. timing->WR = entry[0];
  578. timing->CL = entry[2];
  579. if(dev_priv->card_type <= NV_40) {
  580. nv40_mem_timing_entry(dev,hdr,(struct nouveau_pm_tbl_entry*) entry,magic_number,&pm->memtimings.timing[i]);
  581. } else if(dev_priv->card_type == NV_50){
  582. nv50_mem_timing_entry(dev,&P,hdr,(struct nouveau_pm_tbl_entry*) entry,magic_number,&pm->memtimings.timing[i]);
  583. } else if(dev_priv->card_type == NV_C0) {
  584. nvc0_mem_timing_entry(dev,hdr,(struct nouveau_pm_tbl_entry*) entry,&pm->memtimings.timing[i]);
  585. }
  586. }
  587. memtimings->nr_timing = hdr->entry_cnt;
  588. memtimings->supported = P.version == 1;
  589. }
  590. void
  591. nouveau_mem_timing_fini(struct drm_device *dev)
  592. {
  593. struct drm_nouveau_private *dev_priv = dev->dev_private;
  594. struct nouveau_pm_memtimings *mem = &dev_priv->engine.pm.memtimings;
  595. if(mem->timing) {
  596. kfree(mem->timing);
  597. mem->timing = NULL;
  598. }
  599. }
  600. static int
  601. nouveau_vram_manager_init(struct ttm_mem_type_manager *man, unsigned long psize)
  602. {
  603. /* nothing to do */
  604. return 0;
  605. }
  606. static int
  607. nouveau_vram_manager_fini(struct ttm_mem_type_manager *man)
  608. {
  609. /* nothing to do */
  610. return 0;
  611. }
  612. static inline void
  613. nouveau_mem_node_cleanup(struct nouveau_mem *node)
  614. {
  615. if (node->vma[0].node) {
  616. nouveau_vm_unmap(&node->vma[0]);
  617. nouveau_vm_put(&node->vma[0]);
  618. }
  619. if (node->vma[1].node) {
  620. nouveau_vm_unmap(&node->vma[1]);
  621. nouveau_vm_put(&node->vma[1]);
  622. }
  623. }
  624. static void
  625. nouveau_vram_manager_del(struct ttm_mem_type_manager *man,
  626. struct ttm_mem_reg *mem)
  627. {
  628. struct drm_nouveau_private *dev_priv = nouveau_bdev(man->bdev);
  629. struct nouveau_vram_engine *vram = &dev_priv->engine.vram;
  630. struct drm_device *dev = dev_priv->dev;
  631. nouveau_mem_node_cleanup(mem->mm_node);
  632. vram->put(dev, (struct nouveau_mem **)&mem->mm_node);
  633. }
  634. static int
  635. nouveau_vram_manager_new(struct ttm_mem_type_manager *man,
  636. struct ttm_buffer_object *bo,
  637. struct ttm_placement *placement,
  638. struct ttm_mem_reg *mem)
  639. {
  640. struct drm_nouveau_private *dev_priv = nouveau_bdev(man->bdev);
  641. struct nouveau_vram_engine *vram = &dev_priv->engine.vram;
  642. struct drm_device *dev = dev_priv->dev;
  643. struct nouveau_bo *nvbo = nouveau_bo(bo);
  644. struct nouveau_mem *node;
  645. u32 size_nc = 0;
  646. int ret;
  647. if (nvbo->tile_flags & NOUVEAU_GEM_TILE_NONCONTIG)
  648. size_nc = 1 << nvbo->page_shift;
  649. ret = vram->get(dev, mem->num_pages << PAGE_SHIFT,
  650. mem->page_alignment << PAGE_SHIFT, size_nc,
  651. (nvbo->tile_flags >> 8) & 0x3ff, &node);
  652. if (ret) {
  653. mem->mm_node = NULL;
  654. return (ret == -ENOSPC) ? 0 : ret;
  655. }
  656. node->page_shift = nvbo->page_shift;
  657. mem->mm_node = node;
  658. mem->start = node->offset >> PAGE_SHIFT;
  659. return 0;
  660. }
  661. void
  662. nouveau_vram_manager_debug(struct ttm_mem_type_manager *man, const char *prefix)
  663. {
  664. struct nouveau_mm *mm = man->priv;
  665. struct nouveau_mm_node *r;
  666. u32 total = 0, free = 0;
  667. mutex_lock(&mm->mutex);
  668. list_for_each_entry(r, &mm->nodes, nl_entry) {
  669. printk(KERN_DEBUG "%s %d: 0x%010llx 0x%010llx\n",
  670. prefix, r->type, ((u64)r->offset << 12),
  671. (((u64)r->offset + r->length) << 12));
  672. total += r->length;
  673. if (!r->type)
  674. free += r->length;
  675. }
  676. mutex_unlock(&mm->mutex);
  677. printk(KERN_DEBUG "%s total: 0x%010llx free: 0x%010llx\n",
  678. prefix, (u64)total << 12, (u64)free << 12);
  679. printk(KERN_DEBUG "%s block: 0x%08x\n",
  680. prefix, mm->block_size << 12);
  681. }
  682. const struct ttm_mem_type_manager_func nouveau_vram_manager = {
  683. nouveau_vram_manager_init,
  684. nouveau_vram_manager_fini,
  685. nouveau_vram_manager_new,
  686. nouveau_vram_manager_del,
  687. nouveau_vram_manager_debug
  688. };
  689. static int
  690. nouveau_gart_manager_init(struct ttm_mem_type_manager *man, unsigned long psize)
  691. {
  692. return 0;
  693. }
  694. static int
  695. nouveau_gart_manager_fini(struct ttm_mem_type_manager *man)
  696. {
  697. return 0;
  698. }
  699. static void
  700. nouveau_gart_manager_del(struct ttm_mem_type_manager *man,
  701. struct ttm_mem_reg *mem)
  702. {
  703. nouveau_mem_node_cleanup(mem->mm_node);
  704. kfree(mem->mm_node);
  705. mem->mm_node = NULL;
  706. }
  707. static int
  708. nouveau_gart_manager_new(struct ttm_mem_type_manager *man,
  709. struct ttm_buffer_object *bo,
  710. struct ttm_placement *placement,
  711. struct ttm_mem_reg *mem)
  712. {
  713. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  714. struct nouveau_mem *node;
  715. if (unlikely((mem->num_pages << PAGE_SHIFT) >=
  716. dev_priv->gart_info.aper_size))
  717. return -ENOMEM;
  718. node = kzalloc(sizeof(*node), GFP_KERNEL);
  719. if (!node)
  720. return -ENOMEM;
  721. node->page_shift = 12;
  722. mem->mm_node = node;
  723. mem->start = 0;
  724. return 0;
  725. }
  726. void
  727. nouveau_gart_manager_debug(struct ttm_mem_type_manager *man, const char *prefix)
  728. {
  729. }
  730. const struct ttm_mem_type_manager_func nouveau_gart_manager = {
  731. nouveau_gart_manager_init,
  732. nouveau_gart_manager_fini,
  733. nouveau_gart_manager_new,
  734. nouveau_gart_manager_del,
  735. nouveau_gart_manager_debug
  736. };