ati_pcigart.c 5.6 KB

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