ati_pcigart.c 5.6 KB

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