ffb_context.c 18 KB

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