ati_pcigart.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /**
  2. * \file ati_pcigart.c
  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. drm_ati_pcigart_info * gart_info)
  82. {
  83. drm_sg_mem_t *entry = dev->sg;
  84. unsigned long pages;
  85. int i;
  86. /* we need to support large memory configurations */
  87. if (!entry) {
  88. DRM_ERROR("no scatter/gather memory!\n");
  89. return 0;
  90. }
  91. if (gart_info->bus_addr) {
  92. if (gart_info->gart_table_location == DRM_ATI_GART_MAIN) {
  93. pci_unmap_single(dev->pdev, gart_info->bus_addr,
  94. ATI_PCIGART_TABLE_PAGES * PAGE_SIZE,
  95. PCI_DMA_TODEVICE);
  96. }
  97. pages = (entry->pages <= ATI_MAX_PCIGART_PAGES)
  98. ? entry->pages : ATI_MAX_PCIGART_PAGES;
  99. for (i = 0; i < pages; i++) {
  100. if (!entry->busaddr[i])
  101. break;
  102. pci_unmap_single(dev->pdev, entry->busaddr[i],
  103. PAGE_SIZE, PCI_DMA_TODEVICE);
  104. }
  105. if (gart_info->gart_table_location == DRM_ATI_GART_MAIN)
  106. gart_info->bus_addr = 0;
  107. }
  108. if (gart_info->gart_table_location == DRM_ATI_GART_MAIN
  109. && gart_info->addr) {
  110. drm_ati_free_pcigart_table(gart_info->addr);
  111. gart_info->addr = 0;
  112. }
  113. return 1;
  114. }
  115. EXPORT_SYMBOL(drm_ati_pcigart_cleanup);
  116. int drm_ati_pcigart_init(drm_device_t * dev, drm_ati_pcigart_info * gart_info)
  117. {
  118. drm_sg_mem_t *entry = dev->sg;
  119. unsigned long address = 0;
  120. unsigned long pages;
  121. u32 *pci_gart, page_base, bus_address = 0;
  122. int i, j, ret = 0;
  123. if (!entry) {
  124. DRM_ERROR("no scatter/gather memory!\n");
  125. goto done;
  126. }
  127. if (gart_info->gart_table_location == DRM_ATI_GART_MAIN) {
  128. DRM_DEBUG("PCI: no table in VRAM: using normal RAM\n");
  129. address = drm_ati_alloc_pcigart_table();
  130. if (!address) {
  131. DRM_ERROR("cannot allocate PCI GART page!\n");
  132. goto done;
  133. }
  134. if (!dev->pdev) {
  135. DRM_ERROR("PCI device unknown!\n");
  136. goto done;
  137. }
  138. bus_address = pci_map_single(dev->pdev, (void *)address,
  139. ATI_PCIGART_TABLE_PAGES *
  140. PAGE_SIZE, PCI_DMA_TODEVICE);
  141. if (bus_address == 0) {
  142. DRM_ERROR("unable to map PCIGART pages!\n");
  143. drm_ati_free_pcigart_table(address);
  144. address = 0;
  145. goto done;
  146. }
  147. } else {
  148. address = gart_info->addr;
  149. bus_address = gart_info->bus_addr;
  150. DRM_DEBUG("PCI: Gart Table: VRAM %08X mapped at %08lX\n",
  151. bus_address, address);
  152. }
  153. pci_gart = (u32 *) address;
  154. pages = (entry->pages <= ATI_MAX_PCIGART_PAGES)
  155. ? entry->pages : ATI_MAX_PCIGART_PAGES;
  156. memset(pci_gart, 0, ATI_MAX_PCIGART_PAGES * sizeof(u32));
  157. for (i = 0; i < pages; i++) {
  158. /* we need to support large memory configurations */
  159. entry->busaddr[i] = pci_map_single(dev->pdev,
  160. page_address(entry->
  161. pagelist[i]),
  162. PAGE_SIZE, PCI_DMA_TODEVICE);
  163. if (entry->busaddr[i] == 0) {
  164. DRM_ERROR("unable to map PCIGART pages!\n");
  165. drm_ati_pcigart_cleanup(dev, gart_info);
  166. address = 0;
  167. bus_address = 0;
  168. goto done;
  169. }
  170. page_base = (u32) entry->busaddr[i];
  171. for (j = 0; j < (PAGE_SIZE / ATI_PCIGART_PAGE_SIZE); j++) {
  172. if (gart_info->is_pcie)
  173. *pci_gart = (cpu_to_le32(page_base) >> 8) | 0xc;
  174. else
  175. *pci_gart = cpu_to_le32(page_base);
  176. *pci_gart++;
  177. page_base += ATI_PCIGART_PAGE_SIZE;
  178. }
  179. }
  180. ret = 1;
  181. #if defined(__i386__) || defined(__x86_64__)
  182. wbinvd();
  183. #else
  184. mb();
  185. #endif
  186. done:
  187. gart_info->addr = address;
  188. gart_info->bus_addr = bus_address;
  189. return ret;
  190. }
  191. EXPORT_SYMBOL(drm_ati_pcigart_init);