nouveau_mem.c 23 KB

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