ati_pcigart.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 <linux/export.h>
  33. #include "drmP.h"
  34. # define ATI_PCIGART_PAGE_SIZE 4096 /**< PCI GART page size */
  35. static int drm_ati_alloc_pcigart_table(struct drm_device *dev,
  36. struct drm_ati_pcigart_info *gart_info)
  37. {
  38. gart_info->table_handle = drm_pci_alloc(dev, gart_info->table_size,
  39. PAGE_SIZE);
  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. if (pci_set_dma_mask(dev->pdev, gart_info->table_mask)) {
  98. DRM_ERROR("fail to set dma mask to 0x%Lx\n",
  99. (unsigned long long)gart_info->table_mask);
  100. ret = 1;
  101. goto done;
  102. }
  103. ret = drm_ati_alloc_pcigart_table(dev, gart_info);
  104. if (ret) {
  105. DRM_ERROR("cannot allocate PCI GART page!\n");
  106. goto done;
  107. }
  108. pci_gart = gart_info->table_handle->vaddr;
  109. address = gart_info->table_handle->vaddr;
  110. bus_address = gart_info->table_handle->busaddr;
  111. } else {
  112. address = gart_info->addr;
  113. bus_address = gart_info->bus_addr;
  114. DRM_DEBUG("PCI: Gart Table: VRAM %08LX mapped at %08lX\n",
  115. (unsigned long long)bus_address,
  116. (unsigned long)address);
  117. }
  118. max_ati_pages = (gart_info->table_size / sizeof(u32));
  119. max_real_pages = max_ati_pages / (PAGE_SIZE / ATI_PCIGART_PAGE_SIZE);
  120. pages = (entry->pages <= max_real_pages)
  121. ? entry->pages : max_real_pages;
  122. if (gart_info->gart_table_location == DRM_ATI_GART_MAIN) {
  123. memset(pci_gart, 0, max_ati_pages * sizeof(u32));
  124. } else {
  125. memset_io((void __iomem *)map->handle, 0, max_ati_pages * sizeof(u32));
  126. }
  127. gart_idx = 0;
  128. for (i = 0; i < pages; i++) {
  129. /* we need to support large memory configurations */
  130. entry->busaddr[i] = pci_map_page(dev->pdev, entry->pagelist[i],
  131. 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  132. if (pci_dma_mapping_error(dev->pdev, entry->busaddr[i])) {
  133. DRM_ERROR("unable to map PCIGART pages!\n");
  134. drm_ati_pcigart_cleanup(dev, gart_info);
  135. address = NULL;
  136. bus_address = 0;
  137. goto done;
  138. }
  139. page_base = (u32) entry->busaddr[i];
  140. for (j = 0; j < (PAGE_SIZE / ATI_PCIGART_PAGE_SIZE); j++) {
  141. u32 val;
  142. switch(gart_info->gart_reg_if) {
  143. case DRM_ATI_GART_IGP:
  144. val = page_base | 0xc;
  145. break;
  146. case DRM_ATI_GART_PCIE:
  147. val = (page_base >> 8) | 0xc;
  148. break;
  149. default:
  150. case DRM_ATI_GART_PCI:
  151. val = page_base;
  152. break;
  153. }
  154. if (gart_info->gart_table_location ==
  155. DRM_ATI_GART_MAIN)
  156. pci_gart[gart_idx] = cpu_to_le32(val);
  157. else
  158. DRM_WRITE32(map, gart_idx * sizeof(u32), val);
  159. gart_idx++;
  160. page_base += ATI_PCIGART_PAGE_SIZE;
  161. }
  162. }
  163. ret = 1;
  164. #if defined(__i386__) || defined(__x86_64__)
  165. wbinvd();
  166. #else
  167. mb();
  168. #endif
  169. done:
  170. gart_info->addr = address;
  171. gart_info->bus_addr = bus_address;
  172. return ret;
  173. }
  174. EXPORT_SYMBOL(drm_ati_pcigart_init);