ati_pcigart.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /**
  2. * \file ati_pcigart.h
  3. * ATI PCI GART support
  4. *
  5. * \author Gareth Hughes <gareth@valinux.com>
  6. */
  7. /*
  8. * Created: Wed Dec 13 21:52:19 2000 by gareth@valinux.com
  9. *
  10. * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
  11. * All Rights Reserved.
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining a
  14. * copy of this software and associated documentation files (the "Software"),
  15. * to deal in the Software without restriction, including without limitation
  16. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  17. * and/or sell copies of the Software, and to permit persons to whom the
  18. * Software is furnished to do so, subject to the following conditions:
  19. *
  20. * The above copyright notice and this permission notice (including the next
  21. * paragraph) shall be included in all copies or substantial portions of the
  22. * Software.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  25. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  26. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  27. * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  28. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  29. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  30. * DEALINGS IN THE SOFTWARE.
  31. */
  32. #include "drmP.h"
  33. #if PAGE_SIZE == 65536
  34. # define ATI_PCIGART_TABLE_ORDER 0
  35. # define ATI_PCIGART_TABLE_PAGES (1 << 0)
  36. #elif PAGE_SIZE == 16384
  37. # define ATI_PCIGART_TABLE_ORDER 1
  38. # define ATI_PCIGART_TABLE_PAGES (1 << 1)
  39. #elif PAGE_SIZE == 8192
  40. # define ATI_PCIGART_TABLE_ORDER 2
  41. # define ATI_PCIGART_TABLE_PAGES (1 << 2)
  42. #elif PAGE_SIZE == 4096
  43. # define ATI_PCIGART_TABLE_ORDER 3
  44. # define ATI_PCIGART_TABLE_PAGES (1 << 3)
  45. #else
  46. # error - PAGE_SIZE not 64K, 16K, 8K or 4K
  47. #endif
  48. # define ATI_MAX_PCIGART_PAGES 8192 /**< 32 MB aperture, 4K pages */
  49. # define ATI_PCIGART_PAGE_SIZE 4096 /**< PCI GART page size */
  50. static unsigned long drm_ati_alloc_pcigart_table( void )
  51. {
  52. unsigned long address;
  53. struct page *page;
  54. int i;
  55. DRM_DEBUG( "%s\n", __FUNCTION__ );
  56. address = __get_free_pages( GFP_KERNEL, ATI_PCIGART_TABLE_ORDER );
  57. if ( address == 0UL ) {
  58. return 0;
  59. }
  60. page = virt_to_page( address );
  61. for ( i = 0 ; i < ATI_PCIGART_TABLE_PAGES ; i++, page++ ) {
  62. get_page(page);
  63. SetPageReserved( page );
  64. }
  65. DRM_DEBUG( "%s: returning 0x%08lx\n", __FUNCTION__, address );
  66. return address;
  67. }
  68. static void drm_ati_free_pcigart_table( unsigned long address )
  69. {
  70. struct page *page;
  71. int i;
  72. DRM_DEBUG( "%s\n", __FUNCTION__ );
  73. page = virt_to_page( address );
  74. for ( i = 0 ; i < ATI_PCIGART_TABLE_PAGES ; i++, page++ ) {
  75. __put_page(page);
  76. ClearPageReserved( page );
  77. }
  78. free_pages( address, ATI_PCIGART_TABLE_ORDER );
  79. }
  80. int drm_ati_pcigart_cleanup(drm_device_t *dev, drm_ati_pcigart_info *gart_info)
  81. {
  82. drm_sg_mem_t *entry = dev->sg;
  83. unsigned long pages;
  84. int i;
  85. /* we need to support large memory configurations */
  86. if ( !entry ) {
  87. DRM_ERROR( "no scatter/gather memory!\n" );
  88. return 0;
  89. }
  90. if (gart_info->bus_addr) {
  91. if (gart_info->gart_table_location==DRM_ATI_GART_MAIN) {
  92. pci_unmap_single(dev->pdev, gart_info->bus_addr,
  93. ATI_PCIGART_TABLE_PAGES * PAGE_SIZE,
  94. PCI_DMA_TODEVICE);
  95. }
  96. pages = ( entry->pages <= ATI_MAX_PCIGART_PAGES )
  97. ? entry->pages : ATI_MAX_PCIGART_PAGES;
  98. for ( i = 0 ; i < pages ; i++ ) {
  99. if ( !entry->busaddr[i] ) break;
  100. pci_unmap_single(dev->pdev, entry->busaddr[i],
  101. PAGE_SIZE, PCI_DMA_TODEVICE);
  102. }
  103. if (gart_info->gart_table_location==DRM_ATI_GART_MAIN)
  104. gart_info->bus_addr=0;
  105. }
  106. if (gart_info->gart_table_location==DRM_ATI_GART_MAIN && gart_info->addr) {
  107. drm_ati_free_pcigart_table(gart_info->addr);
  108. gart_info->addr=0;
  109. }
  110. return 1;
  111. }
  112. EXPORT_SYMBOL(drm_ati_pcigart_cleanup);
  113. int drm_ati_pcigart_init(drm_device_t *dev, drm_ati_pcigart_info *gart_info)
  114. {
  115. drm_sg_mem_t *entry = dev->sg;
  116. unsigned long address = 0;
  117. unsigned long pages;
  118. u32 *pci_gart, page_base, bus_address = 0;
  119. int i, j, ret = 0;
  120. if ( !entry ) {
  121. DRM_ERROR( "no scatter/gather memory!\n" );
  122. goto done;
  123. }
  124. if (gart_info->gart_table_location == DRM_ATI_GART_MAIN)
  125. {
  126. DRM_DEBUG("PCI: no table in VRAM: using normal RAM\n");
  127. address = drm_ati_alloc_pcigart_table();
  128. if ( !address ) {
  129. DRM_ERROR( "cannot allocate PCI GART page!\n" );
  130. goto done;
  131. }
  132. if ( !dev->pdev ) {
  133. DRM_ERROR( "PCI device unknown!\n" );
  134. goto done;
  135. }
  136. bus_address = pci_map_single(dev->pdev, (void *)address,
  137. ATI_PCIGART_TABLE_PAGES * PAGE_SIZE,
  138. PCI_DMA_TODEVICE);
  139. if (bus_address == 0) {
  140. DRM_ERROR( "unable to map PCIGART pages!\n" );
  141. drm_ati_free_pcigart_table( address );
  142. address = 0;
  143. goto done;
  144. }
  145. }
  146. else
  147. {
  148. address = gart_info->addr;
  149. bus_address = gart_info->bus_addr;
  150. DRM_DEBUG("PCI: Gart Table: VRAM %08X mapped at %08lX\n", bus_address, address);
  151. }
  152. pci_gart = (u32 *)address;
  153. pages = ( entry->pages <= ATI_MAX_PCIGART_PAGES )
  154. ? entry->pages : ATI_MAX_PCIGART_PAGES;
  155. memset( pci_gart, 0, ATI_MAX_PCIGART_PAGES * sizeof(u32) );
  156. for ( i = 0 ; i < pages ; i++ ) {
  157. /* we need to support large memory configurations */
  158. entry->busaddr[i] = pci_map_single(dev->pdev,
  159. page_address( entry->pagelist[i] ),
  160. PAGE_SIZE,
  161. PCI_DMA_TODEVICE);
  162. if (entry->busaddr[i] == 0) {
  163. DRM_ERROR( "unable to map PCIGART pages!\n" );
  164. drm_ati_pcigart_cleanup(dev, gart_info);
  165. address = 0;
  166. bus_address = 0;
  167. goto done;
  168. }
  169. page_base = (u32) entry->busaddr[i];
  170. for (j = 0; j < (PAGE_SIZE / ATI_PCIGART_PAGE_SIZE); j++) {
  171. if (gart_info->is_pcie)
  172. *pci_gart = (cpu_to_le32(page_base)>>8) | 0xc;
  173. else
  174. *pci_gart++ = cpu_to_le32( page_base );
  175. page_base += ATI_PCIGART_PAGE_SIZE;
  176. }
  177. }
  178. ret = 1;
  179. #if defined(__i386__) || defined(__x86_64__)
  180. wbinvd();
  181. #else
  182. mb();
  183. #endif
  184. done:
  185. gart_info->addr = address;
  186. gart_info->bus_addr = bus_address;
  187. return ret;
  188. }
  189. EXPORT_SYMBOL(drm_ati_pcigart_init);