ati_pcigart.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 void *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 | __GFP_COMP,
  57. ATI_PCIGART_TABLE_ORDER);
  58. if (address == 0UL) {
  59. return NULL;
  60. }
  61. page = virt_to_page(address);
  62. for (i = 0; i < ATI_PCIGART_TABLE_PAGES; i++, page++)
  63. SetPageReserved(page);
  64. DRM_DEBUG("%s: returning 0x%08lx\n", __FUNCTION__, address);
  65. return (void *)address;
  66. }
  67. static void drm_ati_free_pcigart_table(void *address)
  68. {
  69. struct page *page;
  70. int i;
  71. DRM_DEBUG("%s\n", __FUNCTION__);
  72. page = virt_to_page((unsigned long)address);
  73. for (i = 0; i < ATI_PCIGART_TABLE_PAGES; i++, page++)
  74. ClearPageReserved(page);
  75. free_pages((unsigned long)address, ATI_PCIGART_TABLE_ORDER);
  76. }
  77. int drm_ati_pcigart_cleanup(drm_device_t *dev, drm_ati_pcigart_info *gart_info)
  78. {
  79. drm_sg_mem_t *entry = dev->sg;
  80. unsigned long pages;
  81. int i;
  82. /* we need to support large memory configurations */
  83. if (!entry) {
  84. DRM_ERROR("no scatter/gather memory!\n");
  85. return 0;
  86. }
  87. if (gart_info->bus_addr) {
  88. if (gart_info->gart_table_location == DRM_ATI_GART_MAIN) {
  89. pci_unmap_single(dev->pdev, gart_info->bus_addr,
  90. ATI_PCIGART_TABLE_PAGES * PAGE_SIZE,
  91. PCI_DMA_TODEVICE);
  92. }
  93. pages = (entry->pages <= ATI_MAX_PCIGART_PAGES)
  94. ? entry->pages : ATI_MAX_PCIGART_PAGES;
  95. for (i = 0; i < pages; i++) {
  96. if (!entry->busaddr[i])
  97. break;
  98. pci_unmap_single(dev->pdev, entry->busaddr[i],
  99. PAGE_SIZE, PCI_DMA_TODEVICE);
  100. }
  101. if (gart_info->gart_table_location == DRM_ATI_GART_MAIN)
  102. gart_info->bus_addr = 0;
  103. }
  104. if (gart_info->gart_table_location == DRM_ATI_GART_MAIN
  105. && gart_info->addr) {
  106. drm_ati_free_pcigart_table(gart_info->addr);
  107. gart_info->addr = NULL;
  108. }
  109. return 1;
  110. }
  111. EXPORT_SYMBOL(drm_ati_pcigart_cleanup);
  112. int drm_ati_pcigart_init(drm_device_t *dev, drm_ati_pcigart_info *gart_info)
  113. {
  114. drm_sg_mem_t *entry = dev->sg;
  115. void *address = NULL;
  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. if (gart_info->gart_table_location == DRM_ATI_GART_MAIN) {
  124. DRM_DEBUG("PCI: no table in VRAM: using normal RAM\n");
  125. address = drm_ati_alloc_pcigart_table();
  126. if (!address) {
  127. DRM_ERROR("cannot allocate PCI GART page!\n");
  128. goto done;
  129. }
  130. if (!dev->pdev) {
  131. DRM_ERROR("PCI device unknown!\n");
  132. goto done;
  133. }
  134. bus_address = pci_map_single(dev->pdev, address,
  135. ATI_PCIGART_TABLE_PAGES *
  136. PAGE_SIZE, PCI_DMA_TODEVICE);
  137. if (bus_address == 0) {
  138. DRM_ERROR("unable to map PCIGART pages!\n");
  139. drm_ati_free_pcigart_table(address);
  140. address = NULL;
  141. goto done;
  142. }
  143. } else {
  144. address = gart_info->addr;
  145. bus_address = gart_info->bus_addr;
  146. DRM_DEBUG("PCI: Gart Table: VRAM %08X mapped at %08lX\n",
  147. bus_address, (unsigned long)address);
  148. }
  149. pci_gart = (u32 *) address;
  150. pages = (entry->pages <= ATI_MAX_PCIGART_PAGES)
  151. ? entry->pages : ATI_MAX_PCIGART_PAGES;
  152. memset(pci_gart, 0, ATI_MAX_PCIGART_PAGES * sizeof(u32));
  153. for (i = 0; i < pages; i++) {
  154. /* we need to support large memory configurations */
  155. entry->busaddr[i] = pci_map_single(dev->pdev,
  156. page_address(entry->
  157. pagelist[i]),
  158. PAGE_SIZE, PCI_DMA_TODEVICE);
  159. if (entry->busaddr[i] == 0) {
  160. DRM_ERROR("unable to map PCIGART pages!\n");
  161. drm_ati_pcigart_cleanup(dev, gart_info);
  162. address = NULL;
  163. bus_address = 0;
  164. goto done;
  165. }
  166. page_base = (u32) entry->busaddr[i];
  167. for (j = 0; j < (PAGE_SIZE / ATI_PCIGART_PAGE_SIZE); j++) {
  168. if (gart_info->is_pcie)
  169. *pci_gart = cpu_to_le32((page_base >> 8) | 0xc);
  170. else
  171. *pci_gart = cpu_to_le32(page_base);
  172. pci_gart++;
  173. page_base += ATI_PCIGART_PAGE_SIZE;
  174. }
  175. }
  176. ret = 1;
  177. #if defined(__i386__) || defined(__x86_64__)
  178. wbinvd();
  179. #else
  180. mb();
  181. #endif
  182. done:
  183. gart_info->addr = address;
  184. gart_info->bus_addr = bus_address;
  185. return ret;
  186. }
  187. EXPORT_SYMBOL(drm_ati_pcigart_init);