ati_pcigart.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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,
  81. unsigned long addr,
  82. dma_addr_t bus_addr)
  83. {
  84. drm_sg_mem_t *entry = dev->sg;
  85. unsigned long pages;
  86. int i;
  87. /* we need to support large memory configurations */
  88. if ( !entry ) {
  89. DRM_ERROR( "no scatter/gather memory!\n" );
  90. return 0;
  91. }
  92. if ( bus_addr ) {
  93. pci_unmap_single(dev->pdev, bus_addr,
  94. ATI_PCIGART_TABLE_PAGES * PAGE_SIZE,
  95. PCI_DMA_TODEVICE);
  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. }
  104. if ( addr ) {
  105. drm_ati_free_pcigart_table( addr );
  106. }
  107. return 1;
  108. }
  109. EXPORT_SYMBOL(drm_ati_pcigart_cleanup);
  110. int drm_ati_pcigart_init( drm_device_t *dev,
  111. unsigned long *addr,
  112. dma_addr_t *bus_addr)
  113. {
  114. drm_sg_mem_t *entry = dev->sg;
  115. unsigned long address = 0;
  116. unsigned long pages;
  117. u32 *pci_gart, page_base, bus_address = 0;
  118. int i, j, ret = 0;
  119. if ( !entry ) {
  120. DRM_ERROR( "no scatter/gather memory!\n" );
  121. goto done;
  122. }
  123. address = drm_ati_alloc_pcigart_table();
  124. if ( !address ) {
  125. DRM_ERROR( "cannot allocate PCI GART page!\n" );
  126. goto done;
  127. }
  128. if ( !dev->pdev ) {
  129. DRM_ERROR( "PCI device unknown!\n" );
  130. goto done;
  131. }
  132. bus_address = pci_map_single(dev->pdev, (void *)address,
  133. ATI_PCIGART_TABLE_PAGES * PAGE_SIZE,
  134. PCI_DMA_TODEVICE);
  135. if (bus_address == 0) {
  136. DRM_ERROR( "unable to map PCIGART pages!\n" );
  137. drm_ati_free_pcigart_table( address );
  138. address = 0;
  139. goto done;
  140. }
  141. pci_gart = (u32 *)address;
  142. pages = ( entry->pages <= ATI_MAX_PCIGART_PAGES )
  143. ? entry->pages : ATI_MAX_PCIGART_PAGES;
  144. memset( pci_gart, 0, ATI_MAX_PCIGART_PAGES * sizeof(u32) );
  145. for ( i = 0 ; i < pages ; i++ ) {
  146. /* we need to support large memory configurations */
  147. entry->busaddr[i] = pci_map_single(dev->pdev,
  148. page_address( entry->pagelist[i] ),
  149. PAGE_SIZE,
  150. PCI_DMA_TODEVICE);
  151. if (entry->busaddr[i] == 0) {
  152. DRM_ERROR( "unable to map PCIGART pages!\n" );
  153. drm_ati_pcigart_cleanup( dev, address, bus_address );
  154. address = 0;
  155. bus_address = 0;
  156. goto done;
  157. }
  158. page_base = (u32) entry->busaddr[i];
  159. for (j = 0; j < (PAGE_SIZE / ATI_PCIGART_PAGE_SIZE); j++) {
  160. *pci_gart++ = cpu_to_le32( page_base );
  161. page_base += ATI_PCIGART_PAGE_SIZE;
  162. }
  163. }
  164. ret = 1;
  165. #if defined(__i386__) || defined(__x86_64__)
  166. wbinvd();
  167. #else
  168. mb();
  169. #endif
  170. done:
  171. *addr = address;
  172. *bus_addr = bus_address;
  173. return ret;
  174. }
  175. EXPORT_SYMBOL(drm_ati_pcigart_init);