ffb_context.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. /* $Id: ffb_context.c,v 1.5 2001/08/09 17:47:51 davem Exp $
  2. * ffb_context.c: Creator/Creator3D DRI/DRM context switching.
  3. *
  4. * Copyright (C) 2000 David S. Miller (davem@redhat.com)
  5. *
  6. * Almost entirely stolen from tdfx_context.c, see there
  7. * for authors.
  8. */
  9. #include <linux/sched.h>
  10. #include <asm/upa.h>
  11. #include "ffb.h"
  12. #include "drmP.h"
  13. #include "ffb_drv.h"
  14. static int DRM(alloc_queue) (drm_device_t * dev, int is_2d_only) {
  15. ffb_dev_priv_t *fpriv = (ffb_dev_priv_t *) dev->dev_private;
  16. int i;
  17. for (i = 0; i < FFB_MAX_CTXS; i++) {
  18. if (fpriv->hw_state[i] == NULL)
  19. break;
  20. }
  21. if (i == FFB_MAX_CTXS)
  22. return -1;
  23. fpriv->hw_state[i] = kmalloc(sizeof(struct ffb_hw_context), GFP_KERNEL);
  24. if (fpriv->hw_state[i] == NULL)
  25. return -1;
  26. fpriv->hw_state[i]->is_2d_only = is_2d_only;
  27. /* Plus one because 0 is the special DRM_KERNEL_CONTEXT. */
  28. return i + 1;
  29. }
  30. static void ffb_save_context(ffb_dev_priv_t * fpriv, int idx)
  31. {
  32. ffb_fbcPtr ffb = fpriv->regs;
  33. struct ffb_hw_context *ctx;
  34. int i;
  35. ctx = fpriv->hw_state[idx - 1];
  36. if (idx == 0 || ctx == NULL)
  37. return;
  38. if (ctx->is_2d_only) {
  39. /* 2D applications only care about certain pieces
  40. * of state.
  41. */
  42. ctx->drawop = upa_readl(&ffb->drawop);
  43. ctx->ppc = upa_readl(&ffb->ppc);
  44. ctx->wid = upa_readl(&ffb->wid);
  45. ctx->fg = upa_readl(&ffb->fg);
  46. ctx->bg = upa_readl(&ffb->bg);
  47. ctx->xclip = upa_readl(&ffb->xclip);
  48. ctx->fbc = upa_readl(&ffb->fbc);
  49. ctx->rop = upa_readl(&ffb->rop);
  50. ctx->cmp = upa_readl(&ffb->cmp);
  51. ctx->matchab = upa_readl(&ffb->matchab);
  52. ctx->magnab = upa_readl(&ffb->magnab);
  53. ctx->pmask = upa_readl(&ffb->pmask);
  54. ctx->xpmask = upa_readl(&ffb->xpmask);
  55. ctx->lpat = upa_readl(&ffb->lpat);
  56. ctx->fontxy = upa_readl(&ffb->fontxy);
  57. ctx->fontw = upa_readl(&ffb->fontw);
  58. ctx->fontinc = upa_readl(&ffb->fontinc);
  59. /* stencil/stencilctl only exists on FFB2+ and later
  60. * due to the introduction of 3DRAM-III.
  61. */
  62. if (fpriv->ffb_type == ffb2_vertical_plus ||
  63. fpriv->ffb_type == ffb2_horizontal_plus) {
  64. ctx->stencil = upa_readl(&ffb->stencil);
  65. ctx->stencilctl = upa_readl(&ffb->stencilctl);
  66. }
  67. for (i = 0; i < 32; i++)
  68. ctx->area_pattern[i] = upa_readl(&ffb->pattern[i]);
  69. ctx->ucsr = upa_readl(&ffb->ucsr);
  70. return;
  71. }
  72. /* Fetch drawop. */
  73. ctx->drawop = upa_readl(&ffb->drawop);
  74. /* If we were saving the vertex registers, this is where
  75. * we would do it. We would save 32 32-bit words starting
  76. * at ffb->suvtx.
  77. */
  78. /* Capture rendering attributes. */
  79. ctx->ppc = upa_readl(&ffb->ppc); /* Pixel Processor Control */
  80. ctx->wid = upa_readl(&ffb->wid); /* Current WID */
  81. ctx->fg = upa_readl(&ffb->fg); /* Constant FG color */
  82. ctx->bg = upa_readl(&ffb->bg); /* Constant BG color */
  83. ctx->consty = upa_readl(&ffb->consty); /* Constant Y */
  84. ctx->constz = upa_readl(&ffb->constz); /* Constant Z */
  85. ctx->xclip = upa_readl(&ffb->xclip); /* X plane clip */
  86. ctx->dcss = upa_readl(&ffb->dcss); /* Depth Cue Scale Slope */
  87. ctx->vclipmin = upa_readl(&ffb->vclipmin); /* Primary XY clip, minimum */
  88. ctx->vclipmax = upa_readl(&ffb->vclipmax); /* Primary XY clip, maximum */
  89. ctx->vclipzmin = upa_readl(&ffb->vclipzmin); /* Primary Z clip, minimum */
  90. ctx->vclipzmax = upa_readl(&ffb->vclipzmax); /* Primary Z clip, maximum */
  91. ctx->dcsf = upa_readl(&ffb->dcsf); /* Depth Cue Scale Front Bound */
  92. ctx->dcsb = upa_readl(&ffb->dcsb); /* Depth Cue Scale Back Bound */
  93. ctx->dczf = upa_readl(&ffb->dczf); /* Depth Cue Scale Z Front */
  94. ctx->dczb = upa_readl(&ffb->dczb); /* Depth Cue Scale Z Back */
  95. ctx->blendc = upa_readl(&ffb->blendc); /* Alpha Blend Control */
  96. ctx->blendc1 = upa_readl(&ffb->blendc1); /* Alpha Blend Color 1 */
  97. ctx->blendc2 = upa_readl(&ffb->blendc2); /* Alpha Blend Color 2 */
  98. ctx->fbc = upa_readl(&ffb->fbc); /* Frame Buffer Control */
  99. ctx->rop = upa_readl(&ffb->rop); /* Raster Operation */
  100. ctx->cmp = upa_readl(&ffb->cmp); /* Compare Controls */
  101. ctx->matchab = upa_readl(&ffb->matchab); /* Buffer A/B Match Ops */
  102. ctx->matchc = upa_readl(&ffb->matchc); /* Buffer C Match Ops */
  103. ctx->magnab = upa_readl(&ffb->magnab); /* Buffer A/B Magnitude Ops */
  104. ctx->magnc = upa_readl(&ffb->magnc); /* Buffer C Magnitude Ops */
  105. ctx->pmask = upa_readl(&ffb->pmask); /* RGB Plane Mask */
  106. ctx->xpmask = upa_readl(&ffb->xpmask); /* X Plane Mask */
  107. ctx->ypmask = upa_readl(&ffb->ypmask); /* Y Plane Mask */
  108. ctx->zpmask = upa_readl(&ffb->zpmask); /* Z Plane Mask */
  109. /* Auxiliary Clips. */
  110. ctx->auxclip0min = upa_readl(&ffb->auxclip[0].min);
  111. ctx->auxclip0max = upa_readl(&ffb->auxclip[0].max);
  112. ctx->auxclip1min = upa_readl(&ffb->auxclip[1].min);
  113. ctx->auxclip1max = upa_readl(&ffb->auxclip[1].max);
  114. ctx->auxclip2min = upa_readl(&ffb->auxclip[2].min);
  115. ctx->auxclip2max = upa_readl(&ffb->auxclip[2].max);
  116. ctx->auxclip3min = upa_readl(&ffb->auxclip[3].min);
  117. ctx->auxclip3max = upa_readl(&ffb->auxclip[3].max);
  118. ctx->lpat = upa_readl(&ffb->lpat); /* Line Pattern */
  119. ctx->fontxy = upa_readl(&ffb->fontxy); /* XY Font Coordinate */
  120. ctx->fontw = upa_readl(&ffb->fontw); /* Font Width */
  121. ctx->fontinc = upa_readl(&ffb->fontinc); /* Font X/Y Increment */
  122. /* These registers/features only exist on FFB2 and later chips. */
  123. if (fpriv->ffb_type >= ffb2_prototype) {
  124. ctx->dcss1 = upa_readl(&ffb->dcss1); /* Depth Cue Scale Slope 1 */
  125. ctx->dcss2 = upa_readl(&ffb->dcss2); /* Depth Cue Scale Slope 2 */
  126. ctx->dcss2 = upa_readl(&ffb->dcss3); /* Depth Cue Scale Slope 3 */
  127. ctx->dcs2 = upa_readl(&ffb->dcs2); /* Depth Cue Scale 2 */
  128. ctx->dcs3 = upa_readl(&ffb->dcs3); /* Depth Cue Scale 3 */
  129. ctx->dcs4 = upa_readl(&ffb->dcs4); /* Depth Cue Scale 4 */
  130. ctx->dcd2 = upa_readl(&ffb->dcd2); /* Depth Cue Depth 2 */
  131. ctx->dcd3 = upa_readl(&ffb->dcd3); /* Depth Cue Depth 3 */
  132. ctx->dcd4 = upa_readl(&ffb->dcd4); /* Depth Cue Depth 4 */
  133. /* And stencil/stencilctl only exists on FFB2+ and later
  134. * due to the introduction of 3DRAM-III.
  135. */
  136. if (fpriv->ffb_type == ffb2_vertical_plus ||
  137. fpriv->ffb_type == ffb2_horizontal_plus) {
  138. ctx->stencil = upa_readl(&ffb->stencil);
  139. ctx->stencilctl = upa_readl(&ffb->stencilctl);
  140. }
  141. }
  142. /* Save the 32x32 area pattern. */
  143. for (i = 0; i < 32; i++)
  144. ctx->area_pattern[i] = upa_readl(&ffb->pattern[i]);
  145. /* Finally, stash away the User Constol/Status Register. */
  146. ctx->ucsr = upa_readl(&ffb->ucsr);
  147. }
  148. static void ffb_restore_context(ffb_dev_priv_t * fpriv, int old, int idx)
  149. {
  150. ffb_fbcPtr ffb = fpriv->regs;
  151. struct ffb_hw_context *ctx;
  152. int i;
  153. ctx = fpriv->hw_state[idx - 1];
  154. if (idx == 0 || ctx == NULL)
  155. return;
  156. if (ctx->is_2d_only) {
  157. /* 2D applications only care about certain pieces
  158. * of state.
  159. */
  160. upa_writel(ctx->drawop, &ffb->drawop);
  161. /* If we were restoring the vertex registers, this is where
  162. * we would do it. We would restore 32 32-bit words starting
  163. * at ffb->suvtx.
  164. */
  165. upa_writel(ctx->ppc, &ffb->ppc);
  166. upa_writel(ctx->wid, &ffb->wid);
  167. upa_writel(ctx->fg, &ffb->fg);
  168. upa_writel(ctx->bg, &ffb->bg);
  169. upa_writel(ctx->xclip, &ffb->xclip);
  170. upa_writel(ctx->fbc, &ffb->fbc);
  171. upa_writel(ctx->rop, &ffb->rop);
  172. upa_writel(ctx->cmp, &ffb->cmp);
  173. upa_writel(ctx->matchab, &ffb->matchab);
  174. upa_writel(ctx->magnab, &ffb->magnab);
  175. upa_writel(ctx->pmask, &ffb->pmask);
  176. upa_writel(ctx->xpmask, &ffb->xpmask);
  177. upa_writel(ctx->lpat, &ffb->lpat);
  178. upa_writel(ctx->fontxy, &ffb->fontxy);
  179. upa_writel(ctx->fontw, &ffb->fontw);
  180. upa_writel(ctx->fontinc, &ffb->fontinc);
  181. /* stencil/stencilctl only exists on FFB2+ and later
  182. * due to the introduction of 3DRAM-III.
  183. */
  184. if (fpriv->ffb_type == ffb2_vertical_plus ||
  185. fpriv->ffb_type == ffb2_horizontal_plus) {
  186. upa_writel(ctx->stencil, &ffb->stencil);
  187. upa_writel(ctx->stencilctl, &ffb->stencilctl);
  188. upa_writel(0x80000000, &ffb->fbc);
  189. upa_writel((ctx->stencilctl | 0x80000),
  190. &ffb->rawstencilctl);
  191. upa_writel(ctx->fbc, &ffb->fbc);
  192. }
  193. for (i = 0; i < 32; i++)
  194. upa_writel(ctx->area_pattern[i], &ffb->pattern[i]);
  195. upa_writel((ctx->ucsr & 0xf0000), &ffb->ucsr);
  196. return;
  197. }
  198. /* Restore drawop. */
  199. upa_writel(ctx->drawop, &ffb->drawop);
  200. /* If we were restoring the vertex registers, this is where
  201. * we would do it. We would restore 32 32-bit words starting
  202. * at ffb->suvtx.
  203. */
  204. /* Restore rendering attributes. */
  205. upa_writel(ctx->ppc, &ffb->ppc); /* Pixel Processor Control */
  206. upa_writel(ctx->wid, &ffb->wid); /* Current WID */
  207. upa_writel(ctx->fg, &ffb->fg); /* Constant FG color */
  208. upa_writel(ctx->bg, &ffb->bg); /* Constant BG color */
  209. upa_writel(ctx->consty, &ffb->consty); /* Constant Y */
  210. upa_writel(ctx->constz, &ffb->constz); /* Constant Z */
  211. upa_writel(ctx->xclip, &ffb->xclip); /* X plane clip */
  212. upa_writel(ctx->dcss, &ffb->dcss); /* Depth Cue Scale Slope */
  213. upa_writel(ctx->vclipmin, &ffb->vclipmin); /* Primary XY clip, minimum */
  214. upa_writel(ctx->vclipmax, &ffb->vclipmax); /* Primary XY clip, maximum */
  215. upa_writel(ctx->vclipzmin, &ffb->vclipzmin); /* Primary Z clip, minimum */
  216. upa_writel(ctx->vclipzmax, &ffb->vclipzmax); /* Primary Z clip, maximum */
  217. upa_writel(ctx->dcsf, &ffb->dcsf); /* Depth Cue Scale Front Bound */
  218. upa_writel(ctx->dcsb, &ffb->dcsb); /* Depth Cue Scale Back Bound */
  219. upa_writel(ctx->dczf, &ffb->dczf); /* Depth Cue Scale Z Front */
  220. upa_writel(ctx->dczb, &ffb->dczb); /* Depth Cue Scale Z Back */
  221. upa_writel(ctx->blendc, &ffb->blendc); /* Alpha Blend Control */
  222. upa_writel(ctx->blendc1, &ffb->blendc1); /* Alpha Blend Color 1 */
  223. upa_writel(ctx->blendc2, &ffb->blendc2); /* Alpha Blend Color 2 */
  224. upa_writel(ctx->fbc, &ffb->fbc); /* Frame Buffer Control */
  225. upa_writel(ctx->rop, &ffb->rop); /* Raster Operation */
  226. upa_writel(ctx->cmp, &ffb->cmp); /* Compare Controls */
  227. upa_writel(ctx->matchab, &ffb->matchab); /* Buffer A/B Match Ops */
  228. upa_writel(ctx->matchc, &ffb->matchc); /* Buffer C Match Ops */
  229. upa_writel(ctx->magnab, &ffb->magnab); /* Buffer A/B Magnitude Ops */
  230. upa_writel(ctx->magnc, &ffb->magnc); /* Buffer C Magnitude Ops */
  231. upa_writel(ctx->pmask, &ffb->pmask); /* RGB Plane Mask */
  232. upa_writel(ctx->xpmask, &ffb->xpmask); /* X Plane Mask */
  233. upa_writel(ctx->ypmask, &ffb->ypmask); /* Y Plane Mask */
  234. upa_writel(ctx->zpmask, &ffb->zpmask); /* Z Plane Mask */
  235. /* Auxiliary Clips. */
  236. upa_writel(ctx->auxclip0min, &ffb->auxclip[0].min);
  237. upa_writel(ctx->auxclip0max, &ffb->auxclip[0].max);
  238. upa_writel(ctx->auxclip1min, &ffb->auxclip[1].min);
  239. upa_writel(ctx->auxclip1max, &ffb->auxclip[1].max);
  240. upa_writel(ctx->auxclip2min, &ffb->auxclip[2].min);
  241. upa_writel(ctx->auxclip2max, &ffb->auxclip[2].max);
  242. upa_writel(ctx->auxclip3min, &ffb->auxclip[3].min);
  243. upa_writel(ctx->auxclip3max, &ffb->auxclip[3].max);
  244. upa_writel(ctx->lpat, &ffb->lpat); /* Line Pattern */
  245. upa_writel(ctx->fontxy, &ffb->fontxy); /* XY Font Coordinate */
  246. upa_writel(ctx->fontw, &ffb->fontw); /* Font Width */
  247. upa_writel(ctx->fontinc, &ffb->fontinc); /* Font X/Y Increment */
  248. /* These registers/features only exist on FFB2 and later chips. */
  249. if (fpriv->ffb_type >= ffb2_prototype) {
  250. upa_writel(ctx->dcss1, &ffb->dcss1); /* Depth Cue Scale Slope 1 */
  251. upa_writel(ctx->dcss2, &ffb->dcss2); /* Depth Cue Scale Slope 2 */
  252. upa_writel(ctx->dcss3, &ffb->dcss2); /* Depth Cue Scale Slope 3 */
  253. upa_writel(ctx->dcs2, &ffb->dcs2); /* Depth Cue Scale 2 */
  254. upa_writel(ctx->dcs3, &ffb->dcs3); /* Depth Cue Scale 3 */
  255. upa_writel(ctx->dcs4, &ffb->dcs4); /* Depth Cue Scale 4 */
  256. upa_writel(ctx->dcd2, &ffb->dcd2); /* Depth Cue Depth 2 */
  257. upa_writel(ctx->dcd3, &ffb->dcd3); /* Depth Cue Depth 3 */
  258. upa_writel(ctx->dcd4, &ffb->dcd4); /* Depth Cue Depth 4 */
  259. /* And stencil/stencilctl only exists on FFB2+ and later
  260. * due to the introduction of 3DRAM-III.
  261. */
  262. if (fpriv->ffb_type == ffb2_vertical_plus ||
  263. fpriv->ffb_type == ffb2_horizontal_plus) {
  264. /* Unfortunately, there is a hardware bug on
  265. * the FFB2+ chips which prevents a normal write
  266. * to the stencil control register from working
  267. * as it should.
  268. *
  269. * The state controlled by the FFB stencilctl register
  270. * really gets transferred to the per-buffer instances
  271. * of the stencilctl register in the 3DRAM chips.
  272. *
  273. * The bug is that FFB does not update buffer C correctly,
  274. * so we have to do it by hand for them.
  275. */
  276. /* This will update buffers A and B. */
  277. upa_writel(ctx->stencil, &ffb->stencil);
  278. upa_writel(ctx->stencilctl, &ffb->stencilctl);
  279. /* Force FFB to use buffer C 3dram regs. */
  280. upa_writel(0x80000000, &ffb->fbc);
  281. upa_writel((ctx->stencilctl | 0x80000),
  282. &ffb->rawstencilctl);
  283. /* Now restore the correct FBC controls. */
  284. upa_writel(ctx->fbc, &ffb->fbc);
  285. }
  286. }
  287. /* Restore the 32x32 area pattern. */
  288. for (i = 0; i < 32; i++)
  289. upa_writel(ctx->area_pattern[i], &ffb->pattern[i]);
  290. /* Finally, stash away the User Constol/Status Register.
  291. * The only state we really preserve here is the picking
  292. * control.
  293. */
  294. upa_writel((ctx->ucsr & 0xf0000), &ffb->ucsr);
  295. }
  296. #define FFB_UCSR_FB_BUSY 0x01000000
  297. #define FFB_UCSR_RP_BUSY 0x02000000
  298. #define FFB_UCSR_ALL_BUSY (FFB_UCSR_RP_BUSY|FFB_UCSR_FB_BUSY)
  299. static void FFBWait(ffb_fbcPtr ffb)
  300. {
  301. int limit = 100000;
  302. do {
  303. u32 regval = upa_readl(&ffb->ucsr);
  304. if ((regval & FFB_UCSR_ALL_BUSY) == 0)
  305. break;
  306. } while (--limit);
  307. }
  308. int ffb_driver_context_switch(drm_device_t * dev, int old, int new)
  309. {
  310. ffb_dev_priv_t *fpriv = (ffb_dev_priv_t *) dev->dev_private;
  311. #ifdef DRM_DMA_HISTOGRAM
  312. dev->ctx_start = get_cycles();
  313. #endif
  314. DRM_DEBUG("Context switch from %d to %d\n", old, new);
  315. if (new == dev->last_context || dev->last_context == 0) {
  316. dev->last_context = new;
  317. return 0;
  318. }
  319. FFBWait(fpriv->regs);
  320. ffb_save_context(fpriv, old);
  321. ffb_restore_context(fpriv, old, new);
  322. FFBWait(fpriv->regs);
  323. dev->last_context = new;
  324. return 0;
  325. }
  326. int ffb_driver_resctx(struct inode *inode, struct file *filp, unsigned int cmd,
  327. unsigned long arg)
  328. {
  329. drm_ctx_res_t res;
  330. drm_ctx_t ctx;
  331. int i;
  332. DRM_DEBUG("%d\n", DRM_RESERVED_CONTEXTS);
  333. if (copy_from_user(&res, (drm_ctx_res_t __user *) arg, sizeof(res)))
  334. return -EFAULT;
  335. if (res.count >= DRM_RESERVED_CONTEXTS) {
  336. memset(&ctx, 0, sizeof(ctx));
  337. for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
  338. ctx.handle = i;
  339. if (copy_to_user(&res.contexts[i], &i, sizeof(i)))
  340. return -EFAULT;
  341. }
  342. }
  343. res.count = DRM_RESERVED_CONTEXTS;
  344. if (copy_to_user((drm_ctx_res_t __user *) arg, &res, sizeof(res)))
  345. return -EFAULT;
  346. return 0;
  347. }
  348. int ffb_driver_addctx(struct inode *inode, struct file *filp, unsigned int cmd,
  349. unsigned long arg)
  350. {
  351. drm_file_t *priv = filp->private_data;
  352. drm_device_t *dev = priv->dev;
  353. drm_ctx_t ctx;
  354. int idx;
  355. if (copy_from_user(&ctx, (drm_ctx_t __user *) arg, sizeof(ctx)))
  356. return -EFAULT;
  357. idx = DRM(alloc_queue) (dev, (ctx.flags & _DRM_CONTEXT_2DONLY));
  358. if (idx < 0)
  359. return -ENFILE;
  360. DRM_DEBUG("%d\n", ctx.handle);
  361. ctx.handle = idx;
  362. if (copy_to_user((drm_ctx_t __user *) arg, &ctx, sizeof(ctx)))
  363. return -EFAULT;
  364. return 0;
  365. }
  366. int ffb_driver_modctx(struct inode *inode, struct file *filp, unsigned int cmd,
  367. unsigned long arg)
  368. {
  369. drm_file_t *priv = filp->private_data;
  370. drm_device_t *dev = priv->dev;
  371. ffb_dev_priv_t *fpriv = (ffb_dev_priv_t *) dev->dev_private;
  372. struct ffb_hw_context *hwctx;
  373. drm_ctx_t ctx;
  374. int idx;
  375. if (copy_from_user(&ctx, (drm_ctx_t __user *) arg, sizeof(ctx)))
  376. return -EFAULT;
  377. idx = ctx.handle;
  378. if (idx <= 0 || idx >= FFB_MAX_CTXS)
  379. return -EINVAL;
  380. hwctx = fpriv->hw_state[idx - 1];
  381. if (hwctx == NULL)
  382. return -EINVAL;
  383. if ((ctx.flags & _DRM_CONTEXT_2DONLY) == 0)
  384. hwctx->is_2d_only = 0;
  385. else
  386. hwctx->is_2d_only = 1;
  387. return 0;
  388. }
  389. int ffb_driver_getctx(struct inode *inode, struct file *filp, unsigned int cmd,
  390. unsigned long arg)
  391. {
  392. drm_file_t *priv = filp->private_data;
  393. drm_device_t *dev = priv->dev;
  394. ffb_dev_priv_t *fpriv = (ffb_dev_priv_t *) dev->dev_private;
  395. struct ffb_hw_context *hwctx;
  396. drm_ctx_t ctx;
  397. int idx;
  398. if (copy_from_user(&ctx, (drm_ctx_t __user *) arg, sizeof(ctx)))
  399. return -EFAULT;
  400. idx = ctx.handle;
  401. if (idx <= 0 || idx >= FFB_MAX_CTXS)
  402. return -EINVAL;
  403. hwctx = fpriv->hw_state[idx - 1];
  404. if (hwctx == NULL)
  405. return -EINVAL;
  406. if (hwctx->is_2d_only != 0)
  407. ctx.flags = _DRM_CONTEXT_2DONLY;
  408. else
  409. ctx.flags = 0;
  410. if (copy_to_user((drm_ctx_t __user *) arg, &ctx, sizeof(ctx)))
  411. return -EFAULT;
  412. return 0;
  413. }
  414. int ffb_driver_switchctx(struct inode *inode, struct file *filp,
  415. unsigned int cmd, unsigned long arg)
  416. {
  417. drm_file_t *priv = filp->private_data;
  418. drm_device_t *dev = priv->dev;
  419. drm_ctx_t ctx;
  420. if (copy_from_user(&ctx, (drm_ctx_t __user *) arg, sizeof(ctx)))
  421. return -EFAULT;
  422. DRM_DEBUG("%d\n", ctx.handle);
  423. return ffb_driver_context_switch(dev, dev->last_context, ctx.handle);
  424. }
  425. int ffb_driver_newctx(struct inode *inode, struct file *filp, unsigned int cmd,
  426. unsigned long arg)
  427. {
  428. drm_ctx_t ctx;
  429. if (copy_from_user(&ctx, (drm_ctx_t __user *) arg, sizeof(ctx)))
  430. return -EFAULT;
  431. DRM_DEBUG("%d\n", ctx.handle);
  432. return 0;
  433. }
  434. int ffb_driver_rmctx(struct inode *inode, struct file *filp, unsigned int cmd,
  435. unsigned long arg)
  436. {
  437. drm_ctx_t ctx;
  438. drm_file_t *priv = filp->private_data;
  439. drm_device_t *dev = priv->dev;
  440. ffb_dev_priv_t *fpriv = (ffb_dev_priv_t *) dev->dev_private;
  441. int idx;
  442. if (copy_from_user(&ctx, (drm_ctx_t __user *) arg, sizeof(ctx)))
  443. return -EFAULT;
  444. DRM_DEBUG("%d\n", ctx.handle);
  445. idx = ctx.handle - 1;
  446. if (idx < 0 || idx >= FFB_MAX_CTXS)
  447. return -EINVAL;
  448. kfree(fpriv->hw_state[idx]);
  449. fpriv->hw_state[idx] = NULL;
  450. return 0;
  451. }
  452. void ffb_set_context_ioctls(void)
  453. {
  454. DRM(ioctls)[DRM_IOCTL_NR(DRM_IOCTL_ADD_CTX)].func = ffb_driver_addctx;
  455. DRM(ioctls)[DRM_IOCTL_NR(DRM_IOCTL_RM_CTX)].func = ffb_driver_rmctx;
  456. DRM(ioctls)[DRM_IOCTL_NR(DRM_IOCTL_MOD_CTX)].func = ffb_driver_modctx;
  457. DRM(ioctls)[DRM_IOCTL_NR(DRM_IOCTL_GET_CTX)].func = ffb_driver_getctx;
  458. DRM(ioctls)[DRM_IOCTL_NR(DRM_IOCTL_SWITCH_CTX)].func =
  459. ffb_driver_switchctx;
  460. DRM(ioctls)[DRM_IOCTL_NR(DRM_IOCTL_NEW_CTX)].func = ffb_driver_newctx;
  461. DRM(ioctls)[DRM_IOCTL_NR(DRM_IOCTL_RES_CTX)].func = ffb_driver_resctx;
  462. }