ati_pcigart.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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, 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 (void *)address;
  67. }
  68. static void drm_ati_free_pcigart_table(void *address)
  69. {
  70. struct page *page;
  71. int i;
  72. DRM_DEBUG("%s\n", __FUNCTION__);
  73. page = virt_to_page((unsigned long)address);
  74. for (i = 0; i < ATI_PCIGART_TABLE_PAGES; i++, page++) {
  75. __put_page(page);
  76. ClearPageReserved(page);
  77. }
  78. free_pages((unsigned long)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])
  100. break;
  101. pci_unmap_single(dev->pdev, entry->busaddr[i],
  102. PAGE_SIZE, PCI_DMA_TODEVICE);
  103. }
  104. if (gart_info->gart_table_location == DRM_ATI_GART_MAIN)
  105. gart_info->bus_addr = 0;
  106. }
  107. if (gart_info->gart_table_location == DRM_ATI_GART_MAIN
  108. && gart_info->addr) {
  109. drm_ati_free_pcigart_table(gart_info->addr);
  110. gart_info->addr = 0;
  111. }
  112. return 1;
  113. }
  114. EXPORT_SYMBOL(drm_ati_pcigart_cleanup);
  115. int drm_ati_pcigart_init(drm_device_t *dev, drm_ati_pcigart_info *gart_info)
  116. {
  117. drm_sg_mem_t *entry = dev->sg;
  118. void *address = NULL;
  119. unsigned long pages;
  120. u32 *pci_gart, page_base, bus_address = 0;
  121. int i, j, ret = 0;
  122. if (!entry) {
  123. DRM_ERROR("no scatter/gather memory!\n");
  124. goto done;
  125. }
  126. if (gart_info->gart_table_location == DRM_ATI_GART_MAIN) {
  127. DRM_DEBUG("PCI: no table in VRAM: using normal RAM\n");
  128. address = drm_ati_alloc_pcigart_table();
  129. if (!address) {
  130. DRM_ERROR("cannot allocate PCI GART page!\n");
  131. goto done;
  132. }
  133. if (!dev->pdev) {
  134. DRM_ERROR("PCI device unknown!\n");
  135. goto done;
  136. }
  137. bus_address = pci_map_single(dev->pdev, address,
  138. ATI_PCIGART_TABLE_PAGES *
  139. PAGE_SIZE, PCI_DMA_TODEVICE);
  140. if (bus_address == 0) {
  141. DRM_ERROR("unable to map PCIGART pages!\n");
  142. drm_ati_free_pcigart_table(address);
  143. address = 0;
  144. goto done;
  145. }
  146. } else {
  147. address = gart_info->addr;
  148. bus_address = gart_info->bus_addr;
  149. DRM_DEBUG("PCI: Gart Table: VRAM %08X mapped at %08lX\n",
  150. bus_address, (unsigned long)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->
  160. pagelist[i]),
  161. PAGE_SIZE, 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 = NULL;
  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. pci_gart++;
  176. page_base += ATI_PCIGART_PAGE_SIZE;
  177. }
  178. }
  179. ret = 1;
  180. #if defined(__i386__) || defined(__x86_64__)
  181. wbinvd();
  182. #else
  183. mb();
  184. #endif
  185. done:
  186. gart_info->addr = address;
  187. gart_info->bus_addr = bus_address;
  188. return ret;
  189. }
  190. EXPORT_SYMBOL(drm_ati_pcigart_init);