ati_pcigart.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. # define ATI_PCIGART_PAGE_SIZE 4096 /**< PCI GART page size */
  34. static int drm_ati_alloc_pcigart_table(struct drm_device *dev,
  35. struct drm_ati_pcigart_info *gart_info)
  36. {
  37. gart_info->table_handle = drm_pci_alloc(dev, gart_info->table_size,
  38. PAGE_SIZE,
  39. gart_info->table_mask);
  40. if (gart_info->table_handle == NULL)
  41. return -ENOMEM;
  42. return 0;
  43. }
  44. static void drm_ati_free_pcigart_table(struct drm_device *dev,
  45. struct drm_ati_pcigart_info *gart_info)
  46. {
  47. drm_pci_free(dev, gart_info->table_handle);
  48. gart_info->table_handle = NULL;
  49. }
  50. int drm_ati_pcigart_cleanup(struct drm_device *dev, struct drm_ati_pcigart_info *gart_info)
  51. {
  52. struct drm_sg_mem *entry = dev->sg;
  53. unsigned long pages;
  54. int i;
  55. int max_pages;
  56. /* we need to support large memory configurations */
  57. if (!entry) {
  58. DRM_ERROR("no scatter/gather memory!\n");
  59. return 0;
  60. }
  61. if (gart_info->bus_addr) {
  62. max_pages = (gart_info->table_size / sizeof(u32));
  63. pages = (entry->pages <= max_pages)
  64. ? entry->pages : max_pages;
  65. for (i = 0; i < pages; i++) {
  66. if (!entry->busaddr[i])
  67. break;
  68. pci_unmap_page(dev->pdev, entry->busaddr[i],
  69. PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  70. }
  71. if (gart_info->gart_table_location == DRM_ATI_GART_MAIN)
  72. gart_info->bus_addr = 0;
  73. }
  74. if (gart_info->gart_table_location == DRM_ATI_GART_MAIN &&
  75. gart_info->table_handle) {
  76. drm_ati_free_pcigart_table(dev, gart_info);
  77. }
  78. return 1;
  79. }
  80. EXPORT_SYMBOL(drm_ati_pcigart_cleanup);
  81. int drm_ati_pcigart_init(struct drm_device *dev, struct drm_ati_pcigart_info *gart_info)
  82. {
  83. struct drm_local_map *map = &gart_info->mapping;
  84. struct drm_sg_mem *entry = dev->sg;
  85. void *address = NULL;
  86. unsigned long pages;
  87. u32 *pci_gart = NULL, page_base, gart_idx;
  88. dma_addr_t bus_address = 0;
  89. int i, j, ret = 0;
  90. int max_ati_pages, max_real_pages;
  91. if (!entry) {
  92. DRM_ERROR("no scatter/gather memory!\n");
  93. goto done;
  94. }
  95. if (gart_info->gart_table_location == DRM_ATI_GART_MAIN) {
  96. DRM_DEBUG("PCI: no table in VRAM: using normal RAM\n");
  97. ret = drm_ati_alloc_pcigart_table(dev, gart_info);
  98. if (ret) {
  99. DRM_ERROR("cannot allocate PCI GART page!\n");
  100. goto done;
  101. }
  102. pci_gart = gart_info->table_handle->vaddr;
  103. address = gart_info->table_handle->vaddr;
  104. bus_address = gart_info->table_handle->busaddr;
  105. } else {
  106. address = gart_info->addr;
  107. bus_address = gart_info->bus_addr;
  108. DRM_DEBUG("PCI: Gart Table: VRAM %08LX mapped at %08lX\n",
  109. (unsigned long long)bus_address,
  110. (unsigned long)address);
  111. }
  112. max_ati_pages = (gart_info->table_size / sizeof(u32));
  113. max_real_pages = max_ati_pages / (PAGE_SIZE / ATI_PCIGART_PAGE_SIZE);
  114. pages = (entry->pages <= max_real_pages)
  115. ? entry->pages : max_real_pages;
  116. if (gart_info->gart_table_location == DRM_ATI_GART_MAIN) {
  117. memset(pci_gart, 0, max_ati_pages * sizeof(u32));
  118. } else {
  119. memset_io((void __iomem *)map->handle, 0, max_ati_pages * sizeof(u32));
  120. }
  121. gart_idx = 0;
  122. for (i = 0; i < pages; i++) {
  123. /* we need to support large memory configurations */
  124. entry->busaddr[i] = pci_map_page(dev->pdev, entry->pagelist[i],
  125. 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  126. if (entry->busaddr[i] == 0) {
  127. DRM_ERROR("unable to map PCIGART pages!\n");
  128. drm_ati_pcigart_cleanup(dev, gart_info);
  129. address = NULL;
  130. bus_address = 0;
  131. goto done;
  132. }
  133. page_base = (u32) entry->busaddr[i];
  134. for (j = 0; j < (PAGE_SIZE / ATI_PCIGART_PAGE_SIZE); j++) {
  135. u32 val;
  136. switch(gart_info->gart_reg_if) {
  137. case DRM_ATI_GART_IGP:
  138. val = page_base | 0xc;
  139. break;
  140. case DRM_ATI_GART_PCIE:
  141. val = (page_base >> 8) | 0xc;
  142. break;
  143. default:
  144. case DRM_ATI_GART_PCI:
  145. val = page_base;
  146. break;
  147. }
  148. if (gart_info->gart_table_location ==
  149. DRM_ATI_GART_MAIN)
  150. pci_gart[gart_idx] = cpu_to_le32(val);
  151. else
  152. DRM_WRITE32(map, gart_idx * sizeof(u32), val);
  153. gart_idx++;
  154. page_base += ATI_PCIGART_PAGE_SIZE;
  155. }
  156. }
  157. ret = 1;
  158. #if defined(__i386__) || defined(__x86_64__)
  159. wbinvd();
  160. #else
  161. mb();
  162. #endif
  163. done:
  164. gart_info->addr = address;
  165. gart_info->bus_addr = bus_address;
  166. return ret;
  167. }
  168. EXPORT_SYMBOL(drm_ati_pcigart_init);