sis_mm.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /**************************************************************************
  2. *
  3. * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND., USA.
  4. * All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sub license, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the
  15. * next paragraph) shall be included in all copies or substantial portions
  16. * of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  21. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  22. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  23. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  24. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. *
  27. **************************************************************************/
  28. /*
  29. * Authors:
  30. * Thomas Hellström <thomas-at-tungstengraphics-dot-com>
  31. */
  32. #include <drm/drmP.h>
  33. #include <drm/sis_drm.h>
  34. #include "sis_drv.h"
  35. #include <video/sisfb.h>
  36. #define VIDEO_TYPE 0
  37. #define AGP_TYPE 1
  38. struct sis_memblock {
  39. struct drm_mm_node mm_node;
  40. struct sis_memreq req;
  41. struct list_head owner_list;
  42. };
  43. #if defined(CONFIG_FB_SIS) || defined(CONFIG_FB_SIS_MODULE)
  44. /* fb management via fb device */
  45. #define SIS_MM_ALIGN_SHIFT 0
  46. #define SIS_MM_ALIGN_MASK 0
  47. #else /* CONFIG_FB_SIS[_MODULE] */
  48. #define SIS_MM_ALIGN_SHIFT 4
  49. #define SIS_MM_ALIGN_MASK ((1 << SIS_MM_ALIGN_SHIFT) - 1)
  50. #endif /* CONFIG_FB_SIS[_MODULE] */
  51. static int sis_fb_init(struct drm_device *dev, void *data, struct drm_file *file_priv)
  52. {
  53. drm_sis_private_t *dev_priv = dev->dev_private;
  54. drm_sis_fb_t *fb = data;
  55. mutex_lock(&dev->struct_mutex);
  56. /* Unconditionally init the drm_mm, even though we don't use it when the
  57. * fb sis driver is available - make cleanup easier. */
  58. drm_mm_init(&dev_priv->vram_mm, 0, fb->size >> SIS_MM_ALIGN_SHIFT);
  59. dev_priv->vram_initialized = 1;
  60. dev_priv->vram_offset = fb->offset;
  61. mutex_unlock(&dev->struct_mutex);
  62. DRM_DEBUG("offset = %lu, size = %lu\n", fb->offset, fb->size);
  63. return 0;
  64. }
  65. static int sis_drm_alloc(struct drm_device *dev, struct drm_file *file,
  66. void *data, int pool)
  67. {
  68. drm_sis_private_t *dev_priv = dev->dev_private;
  69. drm_sis_mem_t *mem = data;
  70. int retval = 0, user_key;
  71. struct sis_memblock *item;
  72. struct sis_file_private *file_priv = file->driver_priv;
  73. unsigned long offset;
  74. mutex_lock(&dev->struct_mutex);
  75. if (0 == ((pool == 0) ? dev_priv->vram_initialized :
  76. dev_priv->agp_initialized)) {
  77. DRM_ERROR
  78. ("Attempt to allocate from uninitialized memory manager.\n");
  79. mutex_unlock(&dev->struct_mutex);
  80. return -EINVAL;
  81. }
  82. item = kzalloc(sizeof(*item), GFP_KERNEL);
  83. if (!item) {
  84. retval = -ENOMEM;
  85. goto fail_alloc;
  86. }
  87. mem->size = (mem->size + SIS_MM_ALIGN_MASK) >> SIS_MM_ALIGN_SHIFT;
  88. if (pool == AGP_TYPE) {
  89. retval = drm_mm_insert_node(&dev_priv->agp_mm,
  90. &item->mm_node,
  91. mem->size, 0,
  92. DRM_MM_SEARCH_DEFAULT);
  93. offset = item->mm_node.start;
  94. } else {
  95. #if defined(CONFIG_FB_SIS) || defined(CONFIG_FB_SIS_MODULE)
  96. item->req.size = mem->size;
  97. sis_malloc(&item->req);
  98. if (item->req.size == 0)
  99. retval = -ENOMEM;
  100. offset = item->req.offset;
  101. #else
  102. retval = drm_mm_insert_node(&dev_priv->vram_mm,
  103. &item->mm_node,
  104. mem->size, 0,
  105. DRM_MM_SEARCH_DEFAULT);
  106. offset = item->mm_node.start;
  107. #endif
  108. }
  109. if (retval)
  110. goto fail_alloc;
  111. retval = idr_alloc(&dev_priv->object_idr, item, 1, 0, GFP_KERNEL);
  112. if (retval < 0)
  113. goto fail_idr;
  114. user_key = retval;
  115. list_add(&item->owner_list, &file_priv->obj_list);
  116. mutex_unlock(&dev->struct_mutex);
  117. mem->offset = ((pool == 0) ?
  118. dev_priv->vram_offset : dev_priv->agp_offset) +
  119. (offset << SIS_MM_ALIGN_SHIFT);
  120. mem->free = user_key;
  121. mem->size = mem->size << SIS_MM_ALIGN_SHIFT;
  122. return 0;
  123. fail_idr:
  124. drm_mm_remove_node(&item->mm_node);
  125. fail_alloc:
  126. kfree(item);
  127. mutex_unlock(&dev->struct_mutex);
  128. mem->offset = 0;
  129. mem->size = 0;
  130. mem->free = 0;
  131. DRM_DEBUG("alloc %d, size = %ld, offset = %ld\n", pool, mem->size,
  132. mem->offset);
  133. return retval;
  134. }
  135. static int sis_drm_free(struct drm_device *dev, void *data, struct drm_file *file_priv)
  136. {
  137. drm_sis_private_t *dev_priv = dev->dev_private;
  138. drm_sis_mem_t *mem = data;
  139. struct sis_memblock *obj;
  140. mutex_lock(&dev->struct_mutex);
  141. obj = idr_find(&dev_priv->object_idr, mem->free);
  142. if (obj == NULL) {
  143. mutex_unlock(&dev->struct_mutex);
  144. return -EINVAL;
  145. }
  146. idr_remove(&dev_priv->object_idr, mem->free);
  147. list_del(&obj->owner_list);
  148. if (drm_mm_node_allocated(&obj->mm_node))
  149. drm_mm_remove_node(&obj->mm_node);
  150. #if defined(CONFIG_FB_SIS) || defined(CONFIG_FB_SIS_MODULE)
  151. else
  152. sis_free(obj->req.offset);
  153. #endif
  154. kfree(obj);
  155. mutex_unlock(&dev->struct_mutex);
  156. DRM_DEBUG("free = 0x%lx\n", mem->free);
  157. return 0;
  158. }
  159. static int sis_fb_alloc(struct drm_device *dev, void *data,
  160. struct drm_file *file_priv)
  161. {
  162. return sis_drm_alloc(dev, file_priv, data, VIDEO_TYPE);
  163. }
  164. static int sis_ioctl_agp_init(struct drm_device *dev, void *data,
  165. struct drm_file *file_priv)
  166. {
  167. drm_sis_private_t *dev_priv = dev->dev_private;
  168. drm_sis_agp_t *agp = data;
  169. dev_priv = dev->dev_private;
  170. mutex_lock(&dev->struct_mutex);
  171. drm_mm_init(&dev_priv->agp_mm, 0, agp->size >> SIS_MM_ALIGN_SHIFT);
  172. dev_priv->agp_initialized = 1;
  173. dev_priv->agp_offset = agp->offset;
  174. mutex_unlock(&dev->struct_mutex);
  175. DRM_DEBUG("offset = %lu, size = %lu\n", agp->offset, agp->size);
  176. return 0;
  177. }
  178. static int sis_ioctl_agp_alloc(struct drm_device *dev, void *data,
  179. struct drm_file *file_priv)
  180. {
  181. return sis_drm_alloc(dev, file_priv, data, AGP_TYPE);
  182. }
  183. static drm_local_map_t *sis_reg_init(struct drm_device *dev)
  184. {
  185. struct drm_map_list *entry;
  186. drm_local_map_t *map;
  187. list_for_each_entry(entry, &dev->maplist, head) {
  188. map = entry->map;
  189. if (!map)
  190. continue;
  191. if (map->type == _DRM_REGISTERS)
  192. return map;
  193. }
  194. return NULL;
  195. }
  196. int sis_idle(struct drm_device *dev)
  197. {
  198. drm_sis_private_t *dev_priv = dev->dev_private;
  199. uint32_t idle_reg;
  200. unsigned long end;
  201. int i;
  202. if (dev_priv->idle_fault)
  203. return 0;
  204. if (dev_priv->mmio == NULL) {
  205. dev_priv->mmio = sis_reg_init(dev);
  206. if (dev_priv->mmio == NULL) {
  207. DRM_ERROR("Could not find register map.\n");
  208. return 0;
  209. }
  210. }
  211. /*
  212. * Implement a device switch here if needed
  213. */
  214. if (dev_priv->chipset != SIS_CHIP_315)
  215. return 0;
  216. /*
  217. * Timeout after 3 seconds. We cannot use DRM_WAIT_ON here
  218. * because its polling frequency is too low.
  219. */
  220. end = jiffies + (HZ * 3);
  221. for (i = 0; i < 4; ++i) {
  222. do {
  223. idle_reg = SIS_READ(0x85cc);
  224. } while (!time_after_eq(jiffies, end) &&
  225. ((idle_reg & 0x80000000) != 0x80000000));
  226. }
  227. if (time_after_eq(jiffies, end)) {
  228. DRM_ERROR("Graphics engine idle timeout. "
  229. "Disabling idle check\n");
  230. dev_priv->idle_fault = 1;
  231. }
  232. /*
  233. * The caller never sees an error code. It gets trapped
  234. * in libdrm.
  235. */
  236. return 0;
  237. }
  238. void sis_lastclose(struct drm_device *dev)
  239. {
  240. drm_sis_private_t *dev_priv = dev->dev_private;
  241. if (!dev_priv)
  242. return;
  243. mutex_lock(&dev->struct_mutex);
  244. if (dev_priv->vram_initialized) {
  245. drm_mm_takedown(&dev_priv->vram_mm);
  246. dev_priv->vram_initialized = 0;
  247. }
  248. if (dev_priv->agp_initialized) {
  249. drm_mm_takedown(&dev_priv->agp_mm);
  250. dev_priv->agp_initialized = 0;
  251. }
  252. dev_priv->mmio = NULL;
  253. mutex_unlock(&dev->struct_mutex);
  254. }
  255. void sis_reclaim_buffers_locked(struct drm_device *dev,
  256. struct drm_file *file)
  257. {
  258. struct sis_file_private *file_priv = file->driver_priv;
  259. struct sis_memblock *entry, *next;
  260. if (!(file->minor->master && file->master->lock.hw_lock))
  261. return;
  262. drm_legacy_idlelock_take(&file->master->lock);
  263. mutex_lock(&dev->struct_mutex);
  264. if (list_empty(&file_priv->obj_list)) {
  265. mutex_unlock(&dev->struct_mutex);
  266. drm_legacy_idlelock_release(&file->master->lock);
  267. return;
  268. }
  269. sis_idle(dev);
  270. list_for_each_entry_safe(entry, next, &file_priv->obj_list,
  271. owner_list) {
  272. list_del(&entry->owner_list);
  273. if (drm_mm_node_allocated(&entry->mm_node))
  274. drm_mm_remove_node(&entry->mm_node);
  275. #if defined(CONFIG_FB_SIS) || defined(CONFIG_FB_SIS_MODULE)
  276. else
  277. sis_free(entry->req.offset);
  278. #endif
  279. kfree(entry);
  280. }
  281. mutex_unlock(&dev->struct_mutex);
  282. drm_legacy_idlelock_release(&file->master->lock);
  283. return;
  284. }
  285. const struct drm_ioctl_desc sis_ioctls[] = {
  286. DRM_IOCTL_DEF_DRV(SIS_FB_ALLOC, sis_fb_alloc, DRM_AUTH),
  287. DRM_IOCTL_DEF_DRV(SIS_FB_FREE, sis_drm_free, DRM_AUTH),
  288. DRM_IOCTL_DEF_DRV(SIS_AGP_INIT, sis_ioctl_agp_init, DRM_AUTH | DRM_MASTER | DRM_ROOT_ONLY),
  289. DRM_IOCTL_DEF_DRV(SIS_AGP_ALLOC, sis_ioctl_agp_alloc, DRM_AUTH),
  290. DRM_IOCTL_DEF_DRV(SIS_AGP_FREE, sis_drm_free, DRM_AUTH),
  291. DRM_IOCTL_DEF_DRV(SIS_FB_INIT, sis_fb_init, DRM_AUTH | DRM_MASTER | DRM_ROOT_ONLY),
  292. };
  293. int sis_max_ioctl = ARRAY_SIZE(sis_ioctls);