nouveau_mem.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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. /*
  36. * NV10-NV40 tiling helpers
  37. */
  38. static void
  39. nv10_mem_set_region_tiling(struct drm_device *dev, int i, uint32_t addr,
  40. uint32_t size, uint32_t pitch)
  41. {
  42. struct drm_nouveau_private *dev_priv = dev->dev_private;
  43. struct nouveau_fifo_engine *pfifo = &dev_priv->engine.fifo;
  44. struct nouveau_fb_engine *pfb = &dev_priv->engine.fb;
  45. struct nouveau_pgraph_engine *pgraph = &dev_priv->engine.graph;
  46. struct nouveau_tile_reg *tile = &dev_priv->tile.reg[i];
  47. tile->addr = addr;
  48. tile->size = size;
  49. tile->used = !!pitch;
  50. nouveau_fence_unref((void **)&tile->fence);
  51. if (!pfifo->cache_flush(dev))
  52. return;
  53. pfifo->reassign(dev, false);
  54. pfifo->cache_flush(dev);
  55. pfifo->cache_pull(dev, false);
  56. nouveau_wait_for_idle(dev);
  57. pgraph->set_region_tiling(dev, i, addr, size, pitch);
  58. pfb->set_region_tiling(dev, i, addr, size, pitch);
  59. pfifo->cache_pull(dev, true);
  60. pfifo->reassign(dev, true);
  61. }
  62. struct nouveau_tile_reg *
  63. nv10_mem_set_tiling(struct drm_device *dev, uint32_t addr, uint32_t size,
  64. uint32_t pitch)
  65. {
  66. struct drm_nouveau_private *dev_priv = dev->dev_private;
  67. struct nouveau_fb_engine *pfb = &dev_priv->engine.fb;
  68. struct nouveau_tile_reg *tile = dev_priv->tile.reg, *found = NULL;
  69. int i;
  70. spin_lock(&dev_priv->tile.lock);
  71. for (i = 0; i < pfb->num_tiles; i++) {
  72. if (tile[i].used)
  73. /* Tile region in use. */
  74. continue;
  75. if (tile[i].fence &&
  76. !nouveau_fence_signalled(tile[i].fence, NULL))
  77. /* Pending tile region. */
  78. continue;
  79. if (max(tile[i].addr, addr) <
  80. min(tile[i].addr + tile[i].size, addr + size))
  81. /* Kill an intersecting tile region. */
  82. nv10_mem_set_region_tiling(dev, i, 0, 0, 0);
  83. if (pitch && !found) {
  84. /* Free tile region. */
  85. nv10_mem_set_region_tiling(dev, i, addr, size, pitch);
  86. found = &tile[i];
  87. }
  88. }
  89. spin_unlock(&dev_priv->tile.lock);
  90. return found;
  91. }
  92. void
  93. nv10_mem_expire_tiling(struct drm_device *dev, struct nouveau_tile_reg *tile,
  94. struct nouveau_fence *fence)
  95. {
  96. if (fence) {
  97. /* Mark it as pending. */
  98. tile->fence = fence;
  99. nouveau_fence_ref(fence);
  100. }
  101. tile->used = false;
  102. }
  103. /*
  104. * NV50 VM helpers
  105. */
  106. int
  107. nv50_mem_vm_bind_linear(struct drm_device *dev, uint64_t virt, uint32_t size,
  108. uint32_t flags, uint64_t phys)
  109. {
  110. struct drm_nouveau_private *dev_priv = dev->dev_private;
  111. struct nouveau_gpuobj *pgt;
  112. unsigned block;
  113. int i;
  114. virt = ((virt - dev_priv->vm_vram_base) >> 16) << 1;
  115. size = (size >> 16) << 1;
  116. phys |= ((uint64_t)flags << 32);
  117. phys |= 1;
  118. if (dev_priv->vram_sys_base) {
  119. phys += dev_priv->vram_sys_base;
  120. phys |= 0x30;
  121. }
  122. dev_priv->engine.instmem.prepare_access(dev, true);
  123. while (size) {
  124. unsigned offset_h = upper_32_bits(phys);
  125. unsigned offset_l = lower_32_bits(phys);
  126. unsigned pte, end;
  127. for (i = 7; i >= 0; i--) {
  128. block = 1 << (i + 1);
  129. if (size >= block && !(virt & (block - 1)))
  130. break;
  131. }
  132. offset_l |= (i << 7);
  133. phys += block << 15;
  134. size -= block;
  135. while (block) {
  136. pgt = dev_priv->vm_vram_pt[virt >> 14];
  137. pte = virt & 0x3ffe;
  138. end = pte + block;
  139. if (end > 16384)
  140. end = 16384;
  141. block -= (end - pte);
  142. virt += (end - pte);
  143. while (pte < end) {
  144. nv_wo32(dev, pgt, pte++, offset_l);
  145. nv_wo32(dev, pgt, pte++, offset_h);
  146. }
  147. }
  148. }
  149. dev_priv->engine.instmem.finish_access(dev);
  150. nv_wr32(dev, 0x100c80, 0x00050001);
  151. if (!nv_wait(0x100c80, 0x00000001, 0x00000000)) {
  152. NV_ERROR(dev, "timeout: (0x100c80 & 1) == 0 (2)\n");
  153. NV_ERROR(dev, "0x100c80 = 0x%08x\n", nv_rd32(dev, 0x100c80));
  154. return -EBUSY;
  155. }
  156. nv_wr32(dev, 0x100c80, 0x00000001);
  157. if (!nv_wait(0x100c80, 0x00000001, 0x00000000)) {
  158. NV_ERROR(dev, "timeout: (0x100c80 & 1) == 0 (2)\n");
  159. NV_ERROR(dev, "0x100c80 = 0x%08x\n", nv_rd32(dev, 0x100c80));
  160. return -EBUSY;
  161. }
  162. nv_wr32(dev, 0x100c80, 0x00040001);
  163. if (!nv_wait(0x100c80, 0x00000001, 0x00000000)) {
  164. NV_ERROR(dev, "timeout: (0x100c80 & 1) == 0 (2)\n");
  165. NV_ERROR(dev, "0x100c80 = 0x%08x\n", nv_rd32(dev, 0x100c80));
  166. return -EBUSY;
  167. }
  168. nv_wr32(dev, 0x100c80, 0x00060001);
  169. if (!nv_wait(0x100c80, 0x00000001, 0x00000000)) {
  170. NV_ERROR(dev, "timeout: (0x100c80 & 1) == 0 (2)\n");
  171. NV_ERROR(dev, "0x100c80 = 0x%08x\n", nv_rd32(dev, 0x100c80));
  172. return -EBUSY;
  173. }
  174. return 0;
  175. }
  176. void
  177. nv50_mem_vm_unbind(struct drm_device *dev, uint64_t virt, uint32_t size)
  178. {
  179. struct drm_nouveau_private *dev_priv = dev->dev_private;
  180. struct nouveau_gpuobj *pgt;
  181. unsigned pages, pte, end;
  182. virt -= dev_priv->vm_vram_base;
  183. pages = (size >> 16) << 1;
  184. dev_priv->engine.instmem.prepare_access(dev, true);
  185. while (pages) {
  186. pgt = dev_priv->vm_vram_pt[virt >> 29];
  187. pte = (virt & 0x1ffe0000ULL) >> 15;
  188. end = pte + pages;
  189. if (end > 16384)
  190. end = 16384;
  191. pages -= (end - pte);
  192. virt += (end - pte) << 15;
  193. while (pte < end)
  194. nv_wo32(dev, pgt, pte++, 0);
  195. }
  196. dev_priv->engine.instmem.finish_access(dev);
  197. nv_wr32(dev, 0x100c80, 0x00050001);
  198. if (!nv_wait(0x100c80, 0x00000001, 0x00000000)) {
  199. NV_ERROR(dev, "timeout: (0x100c80 & 1) == 0 (2)\n");
  200. NV_ERROR(dev, "0x100c80 = 0x%08x\n", nv_rd32(dev, 0x100c80));
  201. return;
  202. }
  203. nv_wr32(dev, 0x100c80, 0x00000001);
  204. if (!nv_wait(0x100c80, 0x00000001, 0x00000000)) {
  205. NV_ERROR(dev, "timeout: (0x100c80 & 1) == 0 (2)\n");
  206. NV_ERROR(dev, "0x100c80 = 0x%08x\n", nv_rd32(dev, 0x100c80));
  207. return;
  208. }
  209. nv_wr32(dev, 0x100c80, 0x00040001);
  210. if (!nv_wait(0x100c80, 0x00000001, 0x00000000)) {
  211. NV_ERROR(dev, "timeout: (0x100c80 & 1) == 0 (2)\n");
  212. NV_ERROR(dev, "0x100c80 = 0x%08x\n", nv_rd32(dev, 0x100c80));
  213. return;
  214. }
  215. nv_wr32(dev, 0x100c80, 0x00060001);
  216. if (!nv_wait(0x100c80, 0x00000001, 0x00000000)) {
  217. NV_ERROR(dev, "timeout: (0x100c80 & 1) == 0 (2)\n");
  218. NV_ERROR(dev, "0x100c80 = 0x%08x\n", nv_rd32(dev, 0x100c80));
  219. }
  220. }
  221. /*
  222. * Cleanup everything
  223. */
  224. void
  225. nouveau_mem_close(struct drm_device *dev)
  226. {
  227. struct drm_nouveau_private *dev_priv = dev->dev_private;
  228. nouveau_bo_unpin(dev_priv->vga_ram);
  229. nouveau_bo_ref(NULL, &dev_priv->vga_ram);
  230. ttm_bo_device_release(&dev_priv->ttm.bdev);
  231. nouveau_ttm_global_release(dev_priv);
  232. if (drm_core_has_AGP(dev) && dev->agp &&
  233. drm_core_check_feature(dev, DRIVER_MODESET)) {
  234. struct drm_agp_mem *entry, *tempe;
  235. /* Remove AGP resources, but leave dev->agp
  236. intact until drv_cleanup is called. */
  237. list_for_each_entry_safe(entry, tempe, &dev->agp->memory, head) {
  238. if (entry->bound)
  239. drm_unbind_agp(entry->memory);
  240. drm_free_agp(entry->memory, entry->pages);
  241. kfree(entry);
  242. }
  243. INIT_LIST_HEAD(&dev->agp->memory);
  244. if (dev->agp->acquired)
  245. drm_agp_release(dev);
  246. dev->agp->acquired = 0;
  247. dev->agp->enabled = 0;
  248. }
  249. if (dev_priv->fb_mtrr) {
  250. drm_mtrr_del(dev_priv->fb_mtrr,
  251. pci_resource_start(dev->pdev, 1),
  252. pci_resource_len(dev->pdev, 1), DRM_MTRR_WC);
  253. dev_priv->fb_mtrr = 0;
  254. }
  255. }
  256. static uint32_t
  257. nouveau_mem_detect_nv04(struct drm_device *dev)
  258. {
  259. uint32_t boot0 = nv_rd32(dev, NV03_BOOT_0);
  260. if (boot0 & 0x00000100)
  261. return (((boot0 >> 12) & 0xf) * 2 + 2) * 1024 * 1024;
  262. switch (boot0 & NV03_BOOT_0_RAM_AMOUNT) {
  263. case NV04_BOOT_0_RAM_AMOUNT_32MB:
  264. return 32 * 1024 * 1024;
  265. case NV04_BOOT_0_RAM_AMOUNT_16MB:
  266. return 16 * 1024 * 1024;
  267. case NV04_BOOT_0_RAM_AMOUNT_8MB:
  268. return 8 * 1024 * 1024;
  269. case NV04_BOOT_0_RAM_AMOUNT_4MB:
  270. return 4 * 1024 * 1024;
  271. }
  272. return 0;
  273. }
  274. static uint32_t
  275. nouveau_mem_detect_nforce(struct drm_device *dev)
  276. {
  277. struct drm_nouveau_private *dev_priv = dev->dev_private;
  278. struct pci_dev *bridge;
  279. uint32_t mem;
  280. bridge = pci_get_bus_and_slot(0, PCI_DEVFN(0, 1));
  281. if (!bridge) {
  282. NV_ERROR(dev, "no bridge device\n");
  283. return 0;
  284. }
  285. if (dev_priv->flags & NV_NFORCE) {
  286. pci_read_config_dword(bridge, 0x7C, &mem);
  287. return (uint64_t)(((mem >> 6) & 31) + 1)*1024*1024;
  288. } else
  289. if (dev_priv->flags & NV_NFORCE2) {
  290. pci_read_config_dword(bridge, 0x84, &mem);
  291. return (uint64_t)(((mem >> 4) & 127) + 1)*1024*1024;
  292. }
  293. NV_ERROR(dev, "impossible!\n");
  294. return 0;
  295. }
  296. /* returns the amount of FB ram in bytes */
  297. int
  298. nouveau_mem_detect(struct drm_device *dev)
  299. {
  300. struct drm_nouveau_private *dev_priv = dev->dev_private;
  301. if (dev_priv->card_type == NV_04) {
  302. dev_priv->vram_size = nouveau_mem_detect_nv04(dev);
  303. } else
  304. if (dev_priv->flags & (NV_NFORCE | NV_NFORCE2)) {
  305. dev_priv->vram_size = nouveau_mem_detect_nforce(dev);
  306. } else {
  307. dev_priv->vram_size = nv_rd32(dev, NV04_FIFO_DATA);
  308. dev_priv->vram_size &= NV10_FIFO_DATA_RAM_AMOUNT_MB_MASK;
  309. if (dev_priv->chipset == 0xaa || dev_priv->chipset == 0xac)
  310. dev_priv->vram_sys_base = nv_rd32(dev, 0x100e10);
  311. dev_priv->vram_sys_base <<= 12;
  312. }
  313. NV_INFO(dev, "Detected %dMiB VRAM\n", (int)(dev_priv->vram_size >> 20));
  314. if (dev_priv->vram_sys_base) {
  315. NV_INFO(dev, "Stolen system memory at: 0x%010llx\n",
  316. dev_priv->vram_sys_base);
  317. }
  318. if (dev_priv->vram_size)
  319. return 0;
  320. return -ENOMEM;
  321. }
  322. #if __OS_HAS_AGP
  323. static void nouveau_mem_reset_agp(struct drm_device *dev)
  324. {
  325. uint32_t saved_pci_nv_1, saved_pci_nv_19, pmc_enable;
  326. saved_pci_nv_1 = nv_rd32(dev, NV04_PBUS_PCI_NV_1);
  327. saved_pci_nv_19 = nv_rd32(dev, NV04_PBUS_PCI_NV_19);
  328. /* clear busmaster bit */
  329. nv_wr32(dev, NV04_PBUS_PCI_NV_1, saved_pci_nv_1 & ~0x4);
  330. /* clear SBA and AGP bits */
  331. nv_wr32(dev, NV04_PBUS_PCI_NV_19, saved_pci_nv_19 & 0xfffff0ff);
  332. /* power cycle pgraph, if enabled */
  333. pmc_enable = nv_rd32(dev, NV03_PMC_ENABLE);
  334. if (pmc_enable & NV_PMC_ENABLE_PGRAPH) {
  335. nv_wr32(dev, NV03_PMC_ENABLE,
  336. pmc_enable & ~NV_PMC_ENABLE_PGRAPH);
  337. nv_wr32(dev, NV03_PMC_ENABLE, nv_rd32(dev, NV03_PMC_ENABLE) |
  338. NV_PMC_ENABLE_PGRAPH);
  339. }
  340. /* and restore (gives effect of resetting AGP) */
  341. nv_wr32(dev, NV04_PBUS_PCI_NV_19, saved_pci_nv_19);
  342. nv_wr32(dev, NV04_PBUS_PCI_NV_1, saved_pci_nv_1);
  343. }
  344. #endif
  345. int
  346. nouveau_mem_init_agp(struct drm_device *dev)
  347. {
  348. #if __OS_HAS_AGP
  349. struct drm_nouveau_private *dev_priv = dev->dev_private;
  350. struct drm_agp_info info;
  351. struct drm_agp_mode mode;
  352. int ret;
  353. if (nouveau_noagp)
  354. return 0;
  355. nouveau_mem_reset_agp(dev);
  356. if (!dev->agp->acquired) {
  357. ret = drm_agp_acquire(dev);
  358. if (ret) {
  359. NV_ERROR(dev, "Unable to acquire AGP: %d\n", ret);
  360. return ret;
  361. }
  362. }
  363. ret = drm_agp_info(dev, &info);
  364. if (ret) {
  365. NV_ERROR(dev, "Unable to get AGP info: %d\n", ret);
  366. return ret;
  367. }
  368. /* see agp.h for the AGPSTAT_* modes available */
  369. mode.mode = info.mode;
  370. ret = drm_agp_enable(dev, mode);
  371. if (ret) {
  372. NV_ERROR(dev, "Unable to enable AGP: %d\n", ret);
  373. return ret;
  374. }
  375. dev_priv->gart_info.type = NOUVEAU_GART_AGP;
  376. dev_priv->gart_info.aper_base = info.aperture_base;
  377. dev_priv->gart_info.aper_size = info.aperture_size;
  378. #endif
  379. return 0;
  380. }
  381. int
  382. nouveau_mem_init(struct drm_device *dev)
  383. {
  384. struct drm_nouveau_private *dev_priv = dev->dev_private;
  385. struct ttm_bo_device *bdev = &dev_priv->ttm.bdev;
  386. int ret, dma_bits = 32;
  387. dev_priv->fb_phys = pci_resource_start(dev->pdev, 1);
  388. dev_priv->gart_info.type = NOUVEAU_GART_NONE;
  389. if (dev_priv->card_type >= NV_50 &&
  390. pci_dma_supported(dev->pdev, DMA_BIT_MASK(40)))
  391. dma_bits = 40;
  392. ret = pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(dma_bits));
  393. if (ret) {
  394. NV_ERROR(dev, "Error setting DMA mask: %d\n", ret);
  395. return ret;
  396. }
  397. ret = nouveau_ttm_global_init(dev_priv);
  398. if (ret)
  399. return ret;
  400. ret = ttm_bo_device_init(&dev_priv->ttm.bdev,
  401. dev_priv->ttm.bo_global_ref.ref.object,
  402. &nouveau_bo_driver, DRM_FILE_PAGE_OFFSET,
  403. dma_bits <= 32 ? true : false);
  404. if (ret) {
  405. NV_ERROR(dev, "Error initialising bo driver: %d\n", ret);
  406. return ret;
  407. }
  408. INIT_LIST_HEAD(&dev_priv->ttm.bo_list);
  409. spin_lock_init(&dev_priv->ttm.bo_list_lock);
  410. spin_lock_init(&dev_priv->tile.lock);
  411. dev_priv->fb_available_size = dev_priv->vram_size;
  412. dev_priv->fb_mappable_pages = dev_priv->fb_available_size;
  413. if (dev_priv->fb_mappable_pages > pci_resource_len(dev->pdev, 1))
  414. dev_priv->fb_mappable_pages =
  415. pci_resource_len(dev->pdev, 1);
  416. dev_priv->fb_mappable_pages >>= PAGE_SHIFT;
  417. /* remove reserved space at end of vram from available amount */
  418. dev_priv->fb_available_size -= dev_priv->ramin_rsvd_vram;
  419. dev_priv->fb_aper_free = dev_priv->fb_available_size;
  420. /* mappable vram */
  421. ret = ttm_bo_init_mm(bdev, TTM_PL_VRAM,
  422. dev_priv->fb_available_size >> PAGE_SHIFT);
  423. if (ret) {
  424. NV_ERROR(dev, "Failed VRAM mm init: %d\n", ret);
  425. return ret;
  426. }
  427. ret = nouveau_bo_new(dev, NULL, 256*1024, 0, TTM_PL_FLAG_VRAM,
  428. 0, 0, true, true, &dev_priv->vga_ram);
  429. if (ret == 0)
  430. ret = nouveau_bo_pin(dev_priv->vga_ram, TTM_PL_FLAG_VRAM);
  431. if (ret) {
  432. NV_WARN(dev, "failed to reserve VGA memory\n");
  433. nouveau_bo_ref(NULL, &dev_priv->vga_ram);
  434. }
  435. /* GART */
  436. #if !defined(__powerpc__) && !defined(__ia64__)
  437. if (drm_device_is_agp(dev) && dev->agp) {
  438. ret = nouveau_mem_init_agp(dev);
  439. if (ret)
  440. NV_ERROR(dev, "Error initialising AGP: %d\n", ret);
  441. }
  442. #endif
  443. if (dev_priv->gart_info.type == NOUVEAU_GART_NONE) {
  444. ret = nouveau_sgdma_init(dev);
  445. if (ret) {
  446. NV_ERROR(dev, "Error initialising PCI(E): %d\n", ret);
  447. return ret;
  448. }
  449. }
  450. NV_INFO(dev, "%d MiB GART (aperture)\n",
  451. (int)(dev_priv->gart_info.aper_size >> 20));
  452. dev_priv->gart_info.aper_free = dev_priv->gart_info.aper_size;
  453. ret = ttm_bo_init_mm(bdev, TTM_PL_TT,
  454. dev_priv->gart_info.aper_size >> PAGE_SHIFT);
  455. if (ret) {
  456. NV_ERROR(dev, "Failed TT mm init: %d\n", ret);
  457. return ret;
  458. }
  459. dev_priv->fb_mtrr = drm_mtrr_add(pci_resource_start(dev->pdev, 1),
  460. pci_resource_len(dev->pdev, 1),
  461. DRM_MTRR_WC);
  462. return 0;
  463. }