sis_mm.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /* sis_mm.c -- Private header for Direct Rendering Manager -*- linux-c -*-
  2. * Created: Mon Jan 4 10:05:05 1999 by sclin@sis.com.tw
  3. *
  4. * Copyright 2000 Silicon Integrated Systems Corp, Inc., HsinChu, Taiwan.
  5. * All rights reserved.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation
  10. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11. * and/or sell copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the next
  15. * paragraph) shall be included in all copies or substantial portions of the
  16. * 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 NONINFRINGEMENT. IN NO EVENT SHALL
  21. * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  22. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  23. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  24. * DEALINGS IN THE SOFTWARE.
  25. *
  26. * Authors:
  27. * Sung-Ching Lin <sclin@sis.com.tw>
  28. *
  29. */
  30. #include "drmP.h"
  31. #include "sis_drm.h"
  32. #include "sis_drv.h"
  33. #include "sis_ds.h"
  34. #if defined(__linux__) && defined(CONFIG_FB_SIS)
  35. #include <video/sisfb.h>
  36. #endif
  37. #define MAX_CONTEXT 100
  38. #define VIDEO_TYPE 0
  39. #define AGP_TYPE 1
  40. typedef struct {
  41. int used;
  42. int context;
  43. set_t *sets[2]; /* 0 for video, 1 for AGP */
  44. } sis_context_t;
  45. static sis_context_t global_ppriv[MAX_CONTEXT];
  46. static int add_alloc_set(int context, int type, unsigned int val)
  47. {
  48. int i, retval = 0;
  49. for (i = 0; i < MAX_CONTEXT; i++) {
  50. if (global_ppriv[i].used && global_ppriv[i].context == context)
  51. {
  52. retval = setAdd(global_ppriv[i].sets[type], val);
  53. break;
  54. }
  55. }
  56. return retval;
  57. }
  58. static int del_alloc_set(int context, int type, unsigned int val)
  59. {
  60. int i, retval = 0;
  61. for (i = 0; i < MAX_CONTEXT; i++) {
  62. if (global_ppriv[i].used && global_ppriv[i].context == context)
  63. {
  64. retval = setDel(global_ppriv[i].sets[type], val);
  65. break;
  66. }
  67. }
  68. return retval;
  69. }
  70. /* fb management via fb device */
  71. #if defined(__linux__) && defined(CONFIG_FB_SIS)
  72. static int sis_fb_init( DRM_IOCTL_ARGS )
  73. {
  74. return 0;
  75. }
  76. static int sis_fb_alloc( DRM_IOCTL_ARGS )
  77. {
  78. drm_sis_mem_t fb;
  79. struct sis_memreq req;
  80. drm_sis_mem_t __user *argp = (void __user *)data;
  81. int retval = 0;
  82. DRM_COPY_FROM_USER_IOCTL(fb, argp, sizeof(fb));
  83. req.size = fb.size;
  84. sis_malloc(&req);
  85. if (req.offset) {
  86. /* TODO */
  87. fb.offset = req.offset;
  88. fb.free = req.offset;
  89. if (!add_alloc_set(fb.context, VIDEO_TYPE, fb.free)) {
  90. DRM_DEBUG("adding to allocation set fails\n");
  91. sis_free(req.offset);
  92. retval = DRM_ERR(EINVAL);
  93. }
  94. } else {
  95. fb.offset = 0;
  96. fb.size = 0;
  97. fb.free = 0;
  98. }
  99. DRM_COPY_TO_USER_IOCTL(argp, fb, sizeof(fb));
  100. DRM_DEBUG("alloc fb, size = %d, offset = %d\n", fb.size, req.offset);
  101. return retval;
  102. }
  103. static int sis_fb_free( DRM_IOCTL_ARGS )
  104. {
  105. drm_sis_mem_t fb;
  106. int retval = 0;
  107. DRM_COPY_FROM_USER_IOCTL(fb, (drm_sis_mem_t __user *)data, sizeof(fb));
  108. if (!fb.free)
  109. return DRM_ERR(EINVAL);
  110. if (!del_alloc_set(fb.context, VIDEO_TYPE, fb.free))
  111. retval = DRM_ERR(EINVAL);
  112. sis_free((u32)fb.free);
  113. DRM_DEBUG("free fb, offset = %lu\n", fb.free);
  114. return retval;
  115. }
  116. #else
  117. /* Called by the X Server to initialize the FB heap. Allocations will fail
  118. * unless this is called. Offset is the beginning of the heap from the
  119. * framebuffer offset (MaxXFBMem in XFree86).
  120. *
  121. * Memory layout according to Thomas Winischofer:
  122. * |------------------|DDDDDDDDDDDDDDDDDDDDDDDDDDDDD|HHHH|CCCCCCCCCCC|
  123. *
  124. * X driver/sisfb HW- Command-
  125. * framebuffer memory DRI heap Cursor queue
  126. */
  127. static int sis_fb_init( DRM_IOCTL_ARGS )
  128. {
  129. DRM_DEVICE;
  130. drm_sis_private_t *dev_priv = dev->dev_private;
  131. drm_sis_fb_t fb;
  132. DRM_COPY_FROM_USER_IOCTL(fb, (drm_sis_fb_t __user *)data, sizeof(fb));
  133. if (dev_priv == NULL) {
  134. dev->dev_private = drm_calloc(1, sizeof(drm_sis_private_t),
  135. DRM_MEM_DRIVER);
  136. dev_priv = dev->dev_private;
  137. if (dev_priv == NULL)
  138. return ENOMEM;
  139. }
  140. if (dev_priv->FBHeap != NULL)
  141. return DRM_ERR(EINVAL);
  142. dev_priv->FBHeap = mmInit(fb.offset, fb.size);
  143. DRM_DEBUG("offset = %u, size = %u", fb.offset, fb.size);
  144. return 0;
  145. }
  146. static int sis_fb_alloc( DRM_IOCTL_ARGS )
  147. {
  148. DRM_DEVICE;
  149. drm_sis_private_t *dev_priv = dev->dev_private;
  150. drm_sis_mem_t __user *argp = (void __user *)data;
  151. drm_sis_mem_t fb;
  152. PMemBlock block;
  153. int retval = 0;
  154. if (dev_priv == NULL || dev_priv->FBHeap == NULL)
  155. return DRM_ERR(EINVAL);
  156. DRM_COPY_FROM_USER_IOCTL(fb, argp, sizeof(fb));
  157. block = mmAllocMem(dev_priv->FBHeap, fb.size, 0, 0);
  158. if (block) {
  159. /* TODO */
  160. fb.offset = block->ofs;
  161. fb.free = (unsigned long)block;
  162. if (!add_alloc_set(fb.context, VIDEO_TYPE, fb.free)) {
  163. DRM_DEBUG("adding to allocation set fails\n");
  164. mmFreeMem((PMemBlock)fb.free);
  165. retval = DRM_ERR(EINVAL);
  166. }
  167. } else {
  168. fb.offset = 0;
  169. fb.size = 0;
  170. fb.free = 0;
  171. }
  172. DRM_COPY_TO_USER_IOCTL(argp, fb, sizeof(fb));
  173. DRM_DEBUG("alloc fb, size = %d, offset = %d\n", fb.size, fb.offset);
  174. return retval;
  175. }
  176. static int sis_fb_free( DRM_IOCTL_ARGS )
  177. {
  178. DRM_DEVICE;
  179. drm_sis_private_t *dev_priv = dev->dev_private;
  180. drm_sis_mem_t fb;
  181. if (dev_priv == NULL || dev_priv->FBHeap == NULL)
  182. return DRM_ERR(EINVAL);
  183. DRM_COPY_FROM_USER_IOCTL(fb, (drm_sis_mem_t __user *)data, sizeof(fb));
  184. if (!mmBlockInHeap(dev_priv->FBHeap, (PMemBlock)fb.free))
  185. return DRM_ERR(EINVAL);
  186. if (!del_alloc_set(fb.context, VIDEO_TYPE, fb.free))
  187. return DRM_ERR(EINVAL);
  188. mmFreeMem((PMemBlock)fb.free);
  189. DRM_DEBUG("free fb, free = 0x%lx\n", fb.free);
  190. return 0;
  191. }
  192. #endif
  193. /* agp memory management */
  194. static int sis_ioctl_agp_init( DRM_IOCTL_ARGS )
  195. {
  196. DRM_DEVICE;
  197. drm_sis_private_t *dev_priv = dev->dev_private;
  198. drm_sis_agp_t agp;
  199. if (dev_priv == NULL) {
  200. dev->dev_private = drm_calloc(1, sizeof(drm_sis_private_t),
  201. DRM_MEM_DRIVER);
  202. dev_priv = dev->dev_private;
  203. if (dev_priv == NULL)
  204. return ENOMEM;
  205. }
  206. if (dev_priv->AGPHeap != NULL)
  207. return DRM_ERR(EINVAL);
  208. DRM_COPY_FROM_USER_IOCTL(agp, (drm_sis_agp_t __user *)data, sizeof(agp));
  209. dev_priv->AGPHeap = mmInit(agp.offset, agp.size);
  210. DRM_DEBUG("offset = %u, size = %u", agp.offset, agp.size);
  211. return 0;
  212. }
  213. static int sis_ioctl_agp_alloc( DRM_IOCTL_ARGS )
  214. {
  215. DRM_DEVICE;
  216. drm_sis_private_t *dev_priv = dev->dev_private;
  217. drm_sis_mem_t __user *argp = (void __user *)data;
  218. drm_sis_mem_t agp;
  219. PMemBlock block;
  220. int retval = 0;
  221. if (dev_priv == NULL || dev_priv->AGPHeap == NULL)
  222. return DRM_ERR(EINVAL);
  223. DRM_COPY_FROM_USER_IOCTL(agp, argp, sizeof(agp));
  224. block = mmAllocMem(dev_priv->AGPHeap, agp.size, 0, 0);
  225. if (block) {
  226. /* TODO */
  227. agp.offset = block->ofs;
  228. agp.free = (unsigned long)block;
  229. if (!add_alloc_set(agp.context, AGP_TYPE, agp.free)) {
  230. DRM_DEBUG("adding to allocation set fails\n");
  231. mmFreeMem((PMemBlock)agp.free);
  232. retval = -1;
  233. }
  234. } else {
  235. agp.offset = 0;
  236. agp.size = 0;
  237. agp.free = 0;
  238. }
  239. DRM_COPY_TO_USER_IOCTL(argp, agp, sizeof(agp));
  240. DRM_DEBUG("alloc agp, size = %d, offset = %d\n", agp.size, agp.offset);
  241. return retval;
  242. }
  243. static int sis_ioctl_agp_free( DRM_IOCTL_ARGS )
  244. {
  245. DRM_DEVICE;
  246. drm_sis_private_t *dev_priv = dev->dev_private;
  247. drm_sis_mem_t agp;
  248. if (dev_priv == NULL || dev_priv->AGPHeap == NULL)
  249. return DRM_ERR(EINVAL);
  250. DRM_COPY_FROM_USER_IOCTL(agp, (drm_sis_mem_t __user *)data, sizeof(agp));
  251. if (!mmBlockInHeap(dev_priv->AGPHeap, (PMemBlock)agp.free))
  252. return DRM_ERR(EINVAL);
  253. mmFreeMem((PMemBlock)agp.free);
  254. if (!del_alloc_set(agp.context, AGP_TYPE, agp.free))
  255. return DRM_ERR(EINVAL);
  256. DRM_DEBUG("free agp, free = 0x%lx\n", agp.free);
  257. return 0;
  258. }
  259. int sis_init_context(struct drm_device *dev, int context)
  260. {
  261. int i;
  262. for (i = 0; i < MAX_CONTEXT ; i++) {
  263. if (global_ppriv[i].used &&
  264. (global_ppriv[i].context == context))
  265. break;
  266. }
  267. if (i >= MAX_CONTEXT) {
  268. for (i = 0; i < MAX_CONTEXT ; i++) {
  269. if (!global_ppriv[i].used) {
  270. global_ppriv[i].context = context;
  271. global_ppriv[i].used = 1;
  272. global_ppriv[i].sets[0] = setInit();
  273. global_ppriv[i].sets[1] = setInit();
  274. DRM_DEBUG("init allocation set, socket=%d, "
  275. "context = %d\n", i, context);
  276. break;
  277. }
  278. }
  279. if ((i >= MAX_CONTEXT) || (global_ppriv[i].sets[0] == NULL) ||
  280. (global_ppriv[i].sets[1] == NULL))
  281. {
  282. return 0;
  283. }
  284. }
  285. return 1;
  286. }
  287. int sis_final_context(struct drm_device *dev, int context)
  288. {
  289. int i;
  290. for (i=0; i<MAX_CONTEXT; i++) {
  291. if (global_ppriv[i].used &&
  292. (global_ppriv[i].context == context))
  293. break;
  294. }
  295. if (i < MAX_CONTEXT) {
  296. set_t *set;
  297. unsigned int item;
  298. int retval;
  299. DRM_DEBUG("find socket %d, context = %d\n", i, context);
  300. /* Video Memory */
  301. set = global_ppriv[i].sets[0];
  302. retval = setFirst(set, &item);
  303. while (retval) {
  304. DRM_DEBUG("free video memory 0x%x\n", item);
  305. #if defined(__linux__) && defined(CONFIG_FB_SIS)
  306. sis_free(item);
  307. #else
  308. mmFreeMem((PMemBlock)item);
  309. #endif
  310. retval = setNext(set, &item);
  311. }
  312. setDestroy(set);
  313. /* AGP Memory */
  314. set = global_ppriv[i].sets[1];
  315. retval = setFirst(set, &item);
  316. while (retval) {
  317. DRM_DEBUG("free agp memory 0x%x\n", item);
  318. mmFreeMem((PMemBlock)item);
  319. retval = setNext(set, &item);
  320. }
  321. setDestroy(set);
  322. global_ppriv[i].used = 0;
  323. }
  324. return 1;
  325. }
  326. drm_ioctl_desc_t sis_ioctls[] = {
  327. [DRM_IOCTL_NR(DRM_SIS_FB_ALLOC)] = { sis_fb_alloc, 1, 0 },
  328. [DRM_IOCTL_NR(DRM_SIS_FB_FREE)] = { sis_fb_free, 1, 0 },
  329. [DRM_IOCTL_NR(DRM_SIS_AGP_INIT)] = { sis_ioctl_agp_init, 1, 1 },
  330. [DRM_IOCTL_NR(DRM_SIS_AGP_ALLOC)] = { sis_ioctl_agp_alloc, 1, 0 },
  331. [DRM_IOCTL_NR(DRM_SIS_AGP_FREE)] = { sis_ioctl_agp_free, 1, 0 },
  332. [DRM_IOCTL_NR(DRM_SIS_FB_INIT)] = { sis_fb_init, 1, 1 }
  333. };
  334. int sis_max_ioctl = DRM_ARRAY_SIZE(sis_ioctls);