PageRenderTime 69ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/char/drm/radeon_state.c

https://bitbucket.org/evzijst/gittest
C | 3102 lines | 2319 code | 481 blank | 302 comment | 325 complexity | ffee6bcbcc422e06c42ca5a737156a5c MD5 | raw file
Possible License(s): CC-BY-SA-3.0, GPL-2.0, LGPL-2.0
  1. /* radeon_state.c -- State support for Radeon -*- linux-c -*-
  2. *
  3. * Copyright 2000 VA Linux Systems, Inc., Fremont, California.
  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 "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice (including the next
  14. * paragraph) shall be included in all copies or substantial portions of the
  15. * Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  21. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  22. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. *
  25. * Authors:
  26. * Gareth Hughes <gareth@valinux.com>
  27. * Kevin E. Martin <martin@valinux.com>
  28. */
  29. #include "drmP.h"
  30. #include "drm.h"
  31. #include "drm_sarea.h"
  32. #include "radeon_drm.h"
  33. #include "radeon_drv.h"
  34. /* ================================================================
  35. * Helper functions for client state checking and fixup
  36. */
  37. static __inline__ int radeon_check_and_fixup_offset( drm_radeon_private_t *dev_priv,
  38. drm_file_t *filp_priv,
  39. u32 *offset ) {
  40. u32 off = *offset;
  41. struct drm_radeon_driver_file_fields *radeon_priv;
  42. if ( off >= dev_priv->fb_location &&
  43. off < ( dev_priv->gart_vm_start + dev_priv->gart_size ) )
  44. return 0;
  45. radeon_priv = filp_priv->driver_priv;
  46. off += radeon_priv->radeon_fb_delta;
  47. DRM_DEBUG( "offset fixed up to 0x%x\n", off );
  48. if ( off < dev_priv->fb_location ||
  49. off >= ( dev_priv->gart_vm_start + dev_priv->gart_size ) )
  50. return DRM_ERR( EINVAL );
  51. *offset = off;
  52. return 0;
  53. }
  54. static __inline__ int radeon_check_and_fixup_packets( drm_radeon_private_t *dev_priv,
  55. drm_file_t *filp_priv,
  56. int id,
  57. u32 __user *data ) {
  58. switch ( id ) {
  59. case RADEON_EMIT_PP_MISC:
  60. if ( radeon_check_and_fixup_offset( dev_priv, filp_priv,
  61. &data[( RADEON_RB3D_DEPTHOFFSET
  62. - RADEON_PP_MISC ) / 4] ) ) {
  63. DRM_ERROR( "Invalid depth buffer offset\n" );
  64. return DRM_ERR( EINVAL );
  65. }
  66. break;
  67. case RADEON_EMIT_PP_CNTL:
  68. if ( radeon_check_and_fixup_offset( dev_priv, filp_priv,
  69. &data[( RADEON_RB3D_COLOROFFSET
  70. - RADEON_PP_CNTL ) / 4] ) ) {
  71. DRM_ERROR( "Invalid colour buffer offset\n" );
  72. return DRM_ERR( EINVAL );
  73. }
  74. break;
  75. case R200_EMIT_PP_TXOFFSET_0:
  76. case R200_EMIT_PP_TXOFFSET_1:
  77. case R200_EMIT_PP_TXOFFSET_2:
  78. case R200_EMIT_PP_TXOFFSET_3:
  79. case R200_EMIT_PP_TXOFFSET_4:
  80. case R200_EMIT_PP_TXOFFSET_5:
  81. if ( radeon_check_and_fixup_offset( dev_priv, filp_priv,
  82. &data[0] ) ) {
  83. DRM_ERROR( "Invalid R200 texture offset\n" );
  84. return DRM_ERR( EINVAL );
  85. }
  86. break;
  87. case RADEON_EMIT_PP_TXFILTER_0:
  88. case RADEON_EMIT_PP_TXFILTER_1:
  89. case RADEON_EMIT_PP_TXFILTER_2:
  90. if ( radeon_check_and_fixup_offset( dev_priv, filp_priv,
  91. &data[( RADEON_PP_TXOFFSET_0
  92. - RADEON_PP_TXFILTER_0 ) / 4] ) ) {
  93. DRM_ERROR( "Invalid R100 texture offset\n" );
  94. return DRM_ERR( EINVAL );
  95. }
  96. break;
  97. case R200_EMIT_PP_CUBIC_OFFSETS_0:
  98. case R200_EMIT_PP_CUBIC_OFFSETS_1:
  99. case R200_EMIT_PP_CUBIC_OFFSETS_2:
  100. case R200_EMIT_PP_CUBIC_OFFSETS_3:
  101. case R200_EMIT_PP_CUBIC_OFFSETS_4:
  102. case R200_EMIT_PP_CUBIC_OFFSETS_5: {
  103. int i;
  104. for ( i = 0; i < 5; i++ ) {
  105. if ( radeon_check_and_fixup_offset( dev_priv, filp_priv,
  106. &data[i] ) ) {
  107. DRM_ERROR( "Invalid R200 cubic texture offset\n" );
  108. return DRM_ERR( EINVAL );
  109. }
  110. }
  111. break;
  112. }
  113. case RADEON_EMIT_PP_CUBIC_OFFSETS_T0:
  114. case RADEON_EMIT_PP_CUBIC_OFFSETS_T1:
  115. case RADEON_EMIT_PP_CUBIC_OFFSETS_T2:{
  116. int i;
  117. for (i = 0; i < 5; i++) {
  118. if (radeon_check_and_fixup_offset(dev_priv,
  119. filp_priv,
  120. &data[i])) {
  121. DRM_ERROR
  122. ("Invalid R100 cubic texture offset\n");
  123. return DRM_ERR(EINVAL);
  124. }
  125. }
  126. }
  127. break;
  128. case RADEON_EMIT_RB3D_COLORPITCH:
  129. case RADEON_EMIT_RE_LINE_PATTERN:
  130. case RADEON_EMIT_SE_LINE_WIDTH:
  131. case RADEON_EMIT_PP_LUM_MATRIX:
  132. case RADEON_EMIT_PP_ROT_MATRIX_0:
  133. case RADEON_EMIT_RB3D_STENCILREFMASK:
  134. case RADEON_EMIT_SE_VPORT_XSCALE:
  135. case RADEON_EMIT_SE_CNTL:
  136. case RADEON_EMIT_SE_CNTL_STATUS:
  137. case RADEON_EMIT_RE_MISC:
  138. case RADEON_EMIT_PP_BORDER_COLOR_0:
  139. case RADEON_EMIT_PP_BORDER_COLOR_1:
  140. case RADEON_EMIT_PP_BORDER_COLOR_2:
  141. case RADEON_EMIT_SE_ZBIAS_FACTOR:
  142. case RADEON_EMIT_SE_TCL_OUTPUT_VTX_FMT:
  143. case RADEON_EMIT_SE_TCL_MATERIAL_EMMISSIVE_RED:
  144. case R200_EMIT_PP_TXCBLEND_0:
  145. case R200_EMIT_PP_TXCBLEND_1:
  146. case R200_EMIT_PP_TXCBLEND_2:
  147. case R200_EMIT_PP_TXCBLEND_3:
  148. case R200_EMIT_PP_TXCBLEND_4:
  149. case R200_EMIT_PP_TXCBLEND_5:
  150. case R200_EMIT_PP_TXCBLEND_6:
  151. case R200_EMIT_PP_TXCBLEND_7:
  152. case R200_EMIT_TCL_LIGHT_MODEL_CTL_0:
  153. case R200_EMIT_TFACTOR_0:
  154. case R200_EMIT_VTX_FMT_0:
  155. case R200_EMIT_VAP_CTL:
  156. case R200_EMIT_MATRIX_SELECT_0:
  157. case R200_EMIT_TEX_PROC_CTL_2:
  158. case R200_EMIT_TCL_UCP_VERT_BLEND_CTL:
  159. case R200_EMIT_PP_TXFILTER_0:
  160. case R200_EMIT_PP_TXFILTER_1:
  161. case R200_EMIT_PP_TXFILTER_2:
  162. case R200_EMIT_PP_TXFILTER_3:
  163. case R200_EMIT_PP_TXFILTER_4:
  164. case R200_EMIT_PP_TXFILTER_5:
  165. case R200_EMIT_VTE_CNTL:
  166. case R200_EMIT_OUTPUT_VTX_COMP_SEL:
  167. case R200_EMIT_PP_TAM_DEBUG3:
  168. case R200_EMIT_PP_CNTL_X:
  169. case R200_EMIT_RB3D_DEPTHXY_OFFSET:
  170. case R200_EMIT_RE_AUX_SCISSOR_CNTL:
  171. case R200_EMIT_RE_SCISSOR_TL_0:
  172. case R200_EMIT_RE_SCISSOR_TL_1:
  173. case R200_EMIT_RE_SCISSOR_TL_2:
  174. case R200_EMIT_SE_VAP_CNTL_STATUS:
  175. case R200_EMIT_SE_VTX_STATE_CNTL:
  176. case R200_EMIT_RE_POINTSIZE:
  177. case R200_EMIT_TCL_INPUT_VTX_VECTOR_ADDR_0:
  178. case R200_EMIT_PP_CUBIC_FACES_0:
  179. case R200_EMIT_PP_CUBIC_FACES_1:
  180. case R200_EMIT_PP_CUBIC_FACES_2:
  181. case R200_EMIT_PP_CUBIC_FACES_3:
  182. case R200_EMIT_PP_CUBIC_FACES_4:
  183. case R200_EMIT_PP_CUBIC_FACES_5:
  184. case RADEON_EMIT_PP_TEX_SIZE_0:
  185. case RADEON_EMIT_PP_TEX_SIZE_1:
  186. case RADEON_EMIT_PP_TEX_SIZE_2:
  187. case R200_EMIT_RB3D_BLENDCOLOR:
  188. case R200_EMIT_TCL_POINT_SPRITE_CNTL:
  189. case RADEON_EMIT_PP_CUBIC_FACES_0:
  190. case RADEON_EMIT_PP_CUBIC_FACES_1:
  191. case RADEON_EMIT_PP_CUBIC_FACES_2:
  192. case R200_EMIT_PP_TRI_PERF_CNTL:
  193. /* These packets don't contain memory offsets */
  194. break;
  195. default:
  196. DRM_ERROR( "Unknown state packet ID %d\n", id );
  197. return DRM_ERR( EINVAL );
  198. }
  199. return 0;
  200. }
  201. static __inline__ int radeon_check_and_fixup_packet3( drm_radeon_private_t *dev_priv,
  202. drm_file_t *filp_priv,
  203. drm_radeon_cmd_buffer_t *cmdbuf,
  204. unsigned int *cmdsz ) {
  205. u32 *cmd = (u32 *) cmdbuf->buf;
  206. *cmdsz = 2 + ( ( cmd[0] & RADEON_CP_PACKET_COUNT_MASK ) >> 16 );
  207. if ( ( cmd[0] & 0xc0000000 ) != RADEON_CP_PACKET3 ) {
  208. DRM_ERROR( "Not a type 3 packet\n" );
  209. return DRM_ERR( EINVAL );
  210. }
  211. if ( 4 * *cmdsz > cmdbuf->bufsz ) {
  212. DRM_ERROR( "Packet size larger than size of data provided\n" );
  213. return DRM_ERR( EINVAL );
  214. }
  215. /* Check client state and fix it up if necessary */
  216. if ( cmd[0] & 0x8000 ) { /* MSB of opcode: next DWORD GUI_CNTL */
  217. u32 offset;
  218. if ( cmd[1] & ( RADEON_GMC_SRC_PITCH_OFFSET_CNTL
  219. | RADEON_GMC_DST_PITCH_OFFSET_CNTL ) ) {
  220. offset = cmd[2] << 10;
  221. if ( radeon_check_and_fixup_offset( dev_priv, filp_priv, &offset ) ) {
  222. DRM_ERROR( "Invalid first packet offset\n" );
  223. return DRM_ERR( EINVAL );
  224. }
  225. cmd[2] = ( cmd[2] & 0xffc00000 ) | offset >> 10;
  226. }
  227. if ( ( cmd[1] & RADEON_GMC_SRC_PITCH_OFFSET_CNTL ) &&
  228. ( cmd[1] & RADEON_GMC_DST_PITCH_OFFSET_CNTL ) ) {
  229. offset = cmd[3] << 10;
  230. if ( radeon_check_and_fixup_offset( dev_priv, filp_priv, &offset ) ) {
  231. DRM_ERROR( "Invalid second packet offset\n" );
  232. return DRM_ERR( EINVAL );
  233. }
  234. cmd[3] = ( cmd[3] & 0xffc00000 ) | offset >> 10;
  235. }
  236. }
  237. return 0;
  238. }
  239. /* ================================================================
  240. * CP hardware state programming functions
  241. */
  242. static __inline__ void radeon_emit_clip_rect( drm_radeon_private_t *dev_priv,
  243. drm_clip_rect_t *box )
  244. {
  245. RING_LOCALS;
  246. DRM_DEBUG( " box: x1=%d y1=%d x2=%d y2=%d\n",
  247. box->x1, box->y1, box->x2, box->y2 );
  248. BEGIN_RING( 4 );
  249. OUT_RING( CP_PACKET0( RADEON_RE_TOP_LEFT, 0 ) );
  250. OUT_RING( (box->y1 << 16) | box->x1 );
  251. OUT_RING( CP_PACKET0( RADEON_RE_WIDTH_HEIGHT, 0 ) );
  252. OUT_RING( ((box->y2 - 1) << 16) | (box->x2 - 1) );
  253. ADVANCE_RING();
  254. }
  255. /* Emit 1.1 state
  256. */
  257. static int radeon_emit_state( drm_radeon_private_t *dev_priv,
  258. drm_file_t *filp_priv,
  259. drm_radeon_context_regs_t *ctx,
  260. drm_radeon_texture_regs_t *tex,
  261. unsigned int dirty )
  262. {
  263. RING_LOCALS;
  264. DRM_DEBUG( "dirty=0x%08x\n", dirty );
  265. if ( dirty & RADEON_UPLOAD_CONTEXT ) {
  266. if ( radeon_check_and_fixup_offset( dev_priv, filp_priv,
  267. &ctx->rb3d_depthoffset ) ) {
  268. DRM_ERROR( "Invalid depth buffer offset\n" );
  269. return DRM_ERR( EINVAL );
  270. }
  271. if ( radeon_check_and_fixup_offset( dev_priv, filp_priv,
  272. &ctx->rb3d_coloroffset ) ) {
  273. DRM_ERROR( "Invalid depth buffer offset\n" );
  274. return DRM_ERR( EINVAL );
  275. }
  276. BEGIN_RING( 14 );
  277. OUT_RING( CP_PACKET0( RADEON_PP_MISC, 6 ) );
  278. OUT_RING( ctx->pp_misc );
  279. OUT_RING( ctx->pp_fog_color );
  280. OUT_RING( ctx->re_solid_color );
  281. OUT_RING( ctx->rb3d_blendcntl );
  282. OUT_RING( ctx->rb3d_depthoffset );
  283. OUT_RING( ctx->rb3d_depthpitch );
  284. OUT_RING( ctx->rb3d_zstencilcntl );
  285. OUT_RING( CP_PACKET0( RADEON_PP_CNTL, 2 ) );
  286. OUT_RING( ctx->pp_cntl );
  287. OUT_RING( ctx->rb3d_cntl );
  288. OUT_RING( ctx->rb3d_coloroffset );
  289. OUT_RING( CP_PACKET0( RADEON_RB3D_COLORPITCH, 0 ) );
  290. OUT_RING( ctx->rb3d_colorpitch );
  291. ADVANCE_RING();
  292. }
  293. if ( dirty & RADEON_UPLOAD_VERTFMT ) {
  294. BEGIN_RING( 2 );
  295. OUT_RING( CP_PACKET0( RADEON_SE_COORD_FMT, 0 ) );
  296. OUT_RING( ctx->se_coord_fmt );
  297. ADVANCE_RING();
  298. }
  299. if ( dirty & RADEON_UPLOAD_LINE ) {
  300. BEGIN_RING( 5 );
  301. OUT_RING( CP_PACKET0( RADEON_RE_LINE_PATTERN, 1 ) );
  302. OUT_RING( ctx->re_line_pattern );
  303. OUT_RING( ctx->re_line_state );
  304. OUT_RING( CP_PACKET0( RADEON_SE_LINE_WIDTH, 0 ) );
  305. OUT_RING( ctx->se_line_width );
  306. ADVANCE_RING();
  307. }
  308. if ( dirty & RADEON_UPLOAD_BUMPMAP ) {
  309. BEGIN_RING( 5 );
  310. OUT_RING( CP_PACKET0( RADEON_PP_LUM_MATRIX, 0 ) );
  311. OUT_RING( ctx->pp_lum_matrix );
  312. OUT_RING( CP_PACKET0( RADEON_PP_ROT_MATRIX_0, 1 ) );
  313. OUT_RING( ctx->pp_rot_matrix_0 );
  314. OUT_RING( ctx->pp_rot_matrix_1 );
  315. ADVANCE_RING();
  316. }
  317. if ( dirty & RADEON_UPLOAD_MASKS ) {
  318. BEGIN_RING( 4 );
  319. OUT_RING( CP_PACKET0( RADEON_RB3D_STENCILREFMASK, 2 ) );
  320. OUT_RING( ctx->rb3d_stencilrefmask );
  321. OUT_RING( ctx->rb3d_ropcntl );
  322. OUT_RING( ctx->rb3d_planemask );
  323. ADVANCE_RING();
  324. }
  325. if ( dirty & RADEON_UPLOAD_VIEWPORT ) {
  326. BEGIN_RING( 7 );
  327. OUT_RING( CP_PACKET0( RADEON_SE_VPORT_XSCALE, 5 ) );
  328. OUT_RING( ctx->se_vport_xscale );
  329. OUT_RING( ctx->se_vport_xoffset );
  330. OUT_RING( ctx->se_vport_yscale );
  331. OUT_RING( ctx->se_vport_yoffset );
  332. OUT_RING( ctx->se_vport_zscale );
  333. OUT_RING( ctx->se_vport_zoffset );
  334. ADVANCE_RING();
  335. }
  336. if ( dirty & RADEON_UPLOAD_SETUP ) {
  337. BEGIN_RING( 4 );
  338. OUT_RING( CP_PACKET0( RADEON_SE_CNTL, 0 ) );
  339. OUT_RING( ctx->se_cntl );
  340. OUT_RING( CP_PACKET0( RADEON_SE_CNTL_STATUS, 0 ) );
  341. OUT_RING( ctx->se_cntl_status );
  342. ADVANCE_RING();
  343. }
  344. if ( dirty & RADEON_UPLOAD_MISC ) {
  345. BEGIN_RING( 2 );
  346. OUT_RING( CP_PACKET0( RADEON_RE_MISC, 0 ) );
  347. OUT_RING( ctx->re_misc );
  348. ADVANCE_RING();
  349. }
  350. if ( dirty & RADEON_UPLOAD_TEX0 ) {
  351. if ( radeon_check_and_fixup_offset( dev_priv, filp_priv,
  352. &tex[0].pp_txoffset ) ) {
  353. DRM_ERROR( "Invalid texture offset for unit 0\n" );
  354. return DRM_ERR( EINVAL );
  355. }
  356. BEGIN_RING( 9 );
  357. OUT_RING( CP_PACKET0( RADEON_PP_TXFILTER_0, 5 ) );
  358. OUT_RING( tex[0].pp_txfilter );
  359. OUT_RING( tex[0].pp_txformat );
  360. OUT_RING( tex[0].pp_txoffset );
  361. OUT_RING( tex[0].pp_txcblend );
  362. OUT_RING( tex[0].pp_txablend );
  363. OUT_RING( tex[0].pp_tfactor );
  364. OUT_RING( CP_PACKET0( RADEON_PP_BORDER_COLOR_0, 0 ) );
  365. OUT_RING( tex[0].pp_border_color );
  366. ADVANCE_RING();
  367. }
  368. if ( dirty & RADEON_UPLOAD_TEX1 ) {
  369. if ( radeon_check_and_fixup_offset( dev_priv, filp_priv,
  370. &tex[1].pp_txoffset ) ) {
  371. DRM_ERROR( "Invalid texture offset for unit 1\n" );
  372. return DRM_ERR( EINVAL );
  373. }
  374. BEGIN_RING( 9 );
  375. OUT_RING( CP_PACKET0( RADEON_PP_TXFILTER_1, 5 ) );
  376. OUT_RING( tex[1].pp_txfilter );
  377. OUT_RING( tex[1].pp_txformat );
  378. OUT_RING( tex[1].pp_txoffset );
  379. OUT_RING( tex[1].pp_txcblend );
  380. OUT_RING( tex[1].pp_txablend );
  381. OUT_RING( tex[1].pp_tfactor );
  382. OUT_RING( CP_PACKET0( RADEON_PP_BORDER_COLOR_1, 0 ) );
  383. OUT_RING( tex[1].pp_border_color );
  384. ADVANCE_RING();
  385. }
  386. if ( dirty & RADEON_UPLOAD_TEX2 ) {
  387. if ( radeon_check_and_fixup_offset( dev_priv, filp_priv,
  388. &tex[2].pp_txoffset ) ) {
  389. DRM_ERROR( "Invalid texture offset for unit 2\n" );
  390. return DRM_ERR( EINVAL );
  391. }
  392. BEGIN_RING( 9 );
  393. OUT_RING( CP_PACKET0( RADEON_PP_TXFILTER_2, 5 ) );
  394. OUT_RING( tex[2].pp_txfilter );
  395. OUT_RING( tex[2].pp_txformat );
  396. OUT_RING( tex[2].pp_txoffset );
  397. OUT_RING( tex[2].pp_txcblend );
  398. OUT_RING( tex[2].pp_txablend );
  399. OUT_RING( tex[2].pp_tfactor );
  400. OUT_RING( CP_PACKET0( RADEON_PP_BORDER_COLOR_2, 0 ) );
  401. OUT_RING( tex[2].pp_border_color );
  402. ADVANCE_RING();
  403. }
  404. return 0;
  405. }
  406. /* Emit 1.2 state
  407. */
  408. static int radeon_emit_state2( drm_radeon_private_t *dev_priv,
  409. drm_file_t *filp_priv,
  410. drm_radeon_state_t *state )
  411. {
  412. RING_LOCALS;
  413. if (state->dirty & RADEON_UPLOAD_ZBIAS) {
  414. BEGIN_RING( 3 );
  415. OUT_RING( CP_PACKET0( RADEON_SE_ZBIAS_FACTOR, 1 ) );
  416. OUT_RING( state->context2.se_zbias_factor );
  417. OUT_RING( state->context2.se_zbias_constant );
  418. ADVANCE_RING();
  419. }
  420. return radeon_emit_state( dev_priv, filp_priv, &state->context,
  421. state->tex, state->dirty );
  422. }
  423. /* New (1.3) state mechanism. 3 commands (packet, scalar, vector) in
  424. * 1.3 cmdbuffers allow all previous state to be updated as well as
  425. * the tcl scalar and vector areas.
  426. */
  427. static struct {
  428. int start;
  429. int len;
  430. const char *name;
  431. } packet[RADEON_MAX_STATE_PACKETS] = {
  432. { RADEON_PP_MISC,7,"RADEON_PP_MISC" },
  433. { RADEON_PP_CNTL,3,"RADEON_PP_CNTL" },
  434. { RADEON_RB3D_COLORPITCH,1,"RADEON_RB3D_COLORPITCH" },
  435. { RADEON_RE_LINE_PATTERN,2,"RADEON_RE_LINE_PATTERN" },
  436. { RADEON_SE_LINE_WIDTH,1,"RADEON_SE_LINE_WIDTH" },
  437. { RADEON_PP_LUM_MATRIX,1,"RADEON_PP_LUM_MATRIX" },
  438. { RADEON_PP_ROT_MATRIX_0,2,"RADEON_PP_ROT_MATRIX_0" },
  439. { RADEON_RB3D_STENCILREFMASK,3,"RADEON_RB3D_STENCILREFMASK" },
  440. { RADEON_SE_VPORT_XSCALE,6,"RADEON_SE_VPORT_XSCALE" },
  441. { RADEON_SE_CNTL,2,"RADEON_SE_CNTL" },
  442. { RADEON_SE_CNTL_STATUS,1,"RADEON_SE_CNTL_STATUS" },
  443. { RADEON_RE_MISC,1,"RADEON_RE_MISC" },
  444. { RADEON_PP_TXFILTER_0,6,"RADEON_PP_TXFILTER_0" },
  445. { RADEON_PP_BORDER_COLOR_0,1,"RADEON_PP_BORDER_COLOR_0" },
  446. { RADEON_PP_TXFILTER_1,6,"RADEON_PP_TXFILTER_1" },
  447. { RADEON_PP_BORDER_COLOR_1,1,"RADEON_PP_BORDER_COLOR_1" },
  448. { RADEON_PP_TXFILTER_2,6,"RADEON_PP_TXFILTER_2" },
  449. { RADEON_PP_BORDER_COLOR_2,1,"RADEON_PP_BORDER_COLOR_2" },
  450. { RADEON_SE_ZBIAS_FACTOR,2,"RADEON_SE_ZBIAS_FACTOR" },
  451. { RADEON_SE_TCL_OUTPUT_VTX_FMT,11,"RADEON_SE_TCL_OUTPUT_VTX_FMT" },
  452. { RADEON_SE_TCL_MATERIAL_EMMISSIVE_RED,17,"RADEON_SE_TCL_MATERIAL_EMMISSIVE_RED" },
  453. { R200_PP_TXCBLEND_0, 4, "R200_PP_TXCBLEND_0" },
  454. { R200_PP_TXCBLEND_1, 4, "R200_PP_TXCBLEND_1" },
  455. { R200_PP_TXCBLEND_2, 4, "R200_PP_TXCBLEND_2" },
  456. { R200_PP_TXCBLEND_3, 4, "R200_PP_TXCBLEND_3" },
  457. { R200_PP_TXCBLEND_4, 4, "R200_PP_TXCBLEND_4" },
  458. { R200_PP_TXCBLEND_5, 4, "R200_PP_TXCBLEND_5" },
  459. { R200_PP_TXCBLEND_6, 4, "R200_PP_TXCBLEND_6" },
  460. { R200_PP_TXCBLEND_7, 4, "R200_PP_TXCBLEND_7" },
  461. { R200_SE_TCL_LIGHT_MODEL_CTL_0, 6, "R200_SE_TCL_LIGHT_MODEL_CTL_0" },
  462. { R200_PP_TFACTOR_0, 6, "R200_PP_TFACTOR_0" },
  463. { R200_SE_VTX_FMT_0, 4, "R200_SE_VTX_FMT_0" },
  464. { R200_SE_VAP_CNTL, 1, "R200_SE_VAP_CNTL" },
  465. { R200_SE_TCL_MATRIX_SEL_0, 5, "R200_SE_TCL_MATRIX_SEL_0" },
  466. { R200_SE_TCL_TEX_PROC_CTL_2, 5, "R200_SE_TCL_TEX_PROC_CTL_2" },
  467. { R200_SE_TCL_UCP_VERT_BLEND_CTL, 1, "R200_SE_TCL_UCP_VERT_BLEND_CTL" },
  468. { R200_PP_TXFILTER_0, 6, "R200_PP_TXFILTER_0" },
  469. { R200_PP_TXFILTER_1, 6, "R200_PP_TXFILTER_1" },
  470. { R200_PP_TXFILTER_2, 6, "R200_PP_TXFILTER_2" },
  471. { R200_PP_TXFILTER_3, 6, "R200_PP_TXFILTER_3" },
  472. { R200_PP_TXFILTER_4, 6, "R200_PP_TXFILTER_4" },
  473. { R200_PP_TXFILTER_5, 6, "R200_PP_TXFILTER_5" },
  474. { R200_PP_TXOFFSET_0, 1, "R200_PP_TXOFFSET_0" },
  475. { R200_PP_TXOFFSET_1, 1, "R200_PP_TXOFFSET_1" },
  476. { R200_PP_TXOFFSET_2, 1, "R200_PP_TXOFFSET_2" },
  477. { R200_PP_TXOFFSET_3, 1, "R200_PP_TXOFFSET_3" },
  478. { R200_PP_TXOFFSET_4, 1, "R200_PP_TXOFFSET_4" },
  479. { R200_PP_TXOFFSET_5, 1, "R200_PP_TXOFFSET_5" },
  480. { R200_SE_VTE_CNTL, 1, "R200_SE_VTE_CNTL" },
  481. { R200_SE_TCL_OUTPUT_VTX_COMP_SEL, 1, "R200_SE_TCL_OUTPUT_VTX_COMP_SEL" },
  482. { R200_PP_TAM_DEBUG3, 1, "R200_PP_TAM_DEBUG3" },
  483. { R200_PP_CNTL_X, 1, "R200_PP_CNTL_X" },
  484. { R200_RB3D_DEPTHXY_OFFSET, 1, "R200_RB3D_DEPTHXY_OFFSET" },
  485. { R200_RE_AUX_SCISSOR_CNTL, 1, "R200_RE_AUX_SCISSOR_CNTL" },
  486. { R200_RE_SCISSOR_TL_0, 2, "R200_RE_SCISSOR_TL_0" },
  487. { R200_RE_SCISSOR_TL_1, 2, "R200_RE_SCISSOR_TL_1" },
  488. { R200_RE_SCISSOR_TL_2, 2, "R200_RE_SCISSOR_TL_2" },
  489. { R200_SE_VAP_CNTL_STATUS, 1, "R200_SE_VAP_CNTL_STATUS" },
  490. { R200_SE_VTX_STATE_CNTL, 1, "R200_SE_VTX_STATE_CNTL" },
  491. { R200_RE_POINTSIZE, 1, "R200_RE_POINTSIZE" },
  492. { R200_SE_TCL_INPUT_VTX_VECTOR_ADDR_0, 4, "R200_SE_TCL_INPUT_VTX_VECTOR_ADDR_0" },
  493. { R200_PP_CUBIC_FACES_0, 1, "R200_PP_CUBIC_FACES_0" }, /* 61 */
  494. { R200_PP_CUBIC_OFFSET_F1_0, 5, "R200_PP_CUBIC_OFFSET_F1_0" }, /* 62 */
  495. { R200_PP_CUBIC_FACES_1, 1, "R200_PP_CUBIC_FACES_1" },
  496. { R200_PP_CUBIC_OFFSET_F1_1, 5, "R200_PP_CUBIC_OFFSET_F1_1" },
  497. { R200_PP_CUBIC_FACES_2, 1, "R200_PP_CUBIC_FACES_2" },
  498. { R200_PP_CUBIC_OFFSET_F1_2, 5, "R200_PP_CUBIC_OFFSET_F1_2" },
  499. { R200_PP_CUBIC_FACES_3, 1, "R200_PP_CUBIC_FACES_3" },
  500. { R200_PP_CUBIC_OFFSET_F1_3, 5, "R200_PP_CUBIC_OFFSET_F1_3" },
  501. { R200_PP_CUBIC_FACES_4, 1, "R200_PP_CUBIC_FACES_4" },
  502. { R200_PP_CUBIC_OFFSET_F1_4, 5, "R200_PP_CUBIC_OFFSET_F1_4" },
  503. { R200_PP_CUBIC_FACES_5, 1, "R200_PP_CUBIC_FACES_5" },
  504. { R200_PP_CUBIC_OFFSET_F1_5, 5, "R200_PP_CUBIC_OFFSET_F1_5" },
  505. { RADEON_PP_TEX_SIZE_0, 2, "RADEON_PP_TEX_SIZE_0" },
  506. { RADEON_PP_TEX_SIZE_1, 2, "RADEON_PP_TEX_SIZE_1" },
  507. { RADEON_PP_TEX_SIZE_2, 2, "RADEON_PP_TEX_SIZE_2" },
  508. { R200_RB3D_BLENDCOLOR, 3, "R200_RB3D_BLENDCOLOR" },
  509. { R200_SE_TCL_POINT_SPRITE_CNTL, 1, "R200_SE_TCL_POINT_SPRITE_CNTL" },
  510. { RADEON_PP_CUBIC_FACES_0, 1, "RADEON_PP_CUBIC_FACES_0"},
  511. { RADEON_PP_CUBIC_OFFSET_T0_0, 5, "RADEON_PP_CUBIC_OFFSET_T0_0"},
  512. { RADEON_PP_CUBIC_FACES_1, 1, "RADEON_PP_CUBIC_FACES_1"},
  513. { RADEON_PP_CUBIC_OFFSET_T1_0, 5, "RADEON_PP_CUBIC_OFFSET_T1_0"},
  514. { RADEON_PP_CUBIC_FACES_2, 1, "RADEON_PP_CUBIC_FACES_2"},
  515. { RADEON_PP_CUBIC_OFFSET_T2_0, 5, "RADEON_PP_CUBIC_OFFSET_T2_0"},
  516. { R200_PP_TRI_PERF, 2, "R200_PP_TRI_PERF"},
  517. };
  518. /* ================================================================
  519. * Performance monitoring functions
  520. */
  521. static void radeon_clear_box( drm_radeon_private_t *dev_priv,
  522. int x, int y, int w, int h,
  523. int r, int g, int b )
  524. {
  525. u32 color;
  526. RING_LOCALS;
  527. x += dev_priv->sarea_priv->boxes[0].x1;
  528. y += dev_priv->sarea_priv->boxes[0].y1;
  529. switch ( dev_priv->color_fmt ) {
  530. case RADEON_COLOR_FORMAT_RGB565:
  531. color = (((r & 0xf8) << 8) |
  532. ((g & 0xfc) << 3) |
  533. ((b & 0xf8) >> 3));
  534. break;
  535. case RADEON_COLOR_FORMAT_ARGB8888:
  536. default:
  537. color = (((0xff) << 24) | (r << 16) | (g << 8) | b);
  538. break;
  539. }
  540. BEGIN_RING( 4 );
  541. RADEON_WAIT_UNTIL_3D_IDLE();
  542. OUT_RING( CP_PACKET0( RADEON_DP_WRITE_MASK, 0 ) );
  543. OUT_RING( 0xffffffff );
  544. ADVANCE_RING();
  545. BEGIN_RING( 6 );
  546. OUT_RING( CP_PACKET3( RADEON_CNTL_PAINT_MULTI, 4 ) );
  547. OUT_RING( RADEON_GMC_DST_PITCH_OFFSET_CNTL |
  548. RADEON_GMC_BRUSH_SOLID_COLOR |
  549. (dev_priv->color_fmt << 8) |
  550. RADEON_GMC_SRC_DATATYPE_COLOR |
  551. RADEON_ROP3_P |
  552. RADEON_GMC_CLR_CMP_CNTL_DIS );
  553. if ( dev_priv->page_flipping && dev_priv->current_page == 1 ) {
  554. OUT_RING( dev_priv->front_pitch_offset );
  555. } else {
  556. OUT_RING( dev_priv->back_pitch_offset );
  557. }
  558. OUT_RING( color );
  559. OUT_RING( (x << 16) | y );
  560. OUT_RING( (w << 16) | h );
  561. ADVANCE_RING();
  562. }
  563. static void radeon_cp_performance_boxes( drm_radeon_private_t *dev_priv )
  564. {
  565. /* Collapse various things into a wait flag -- trying to
  566. * guess if userspase slept -- better just to have them tell us.
  567. */
  568. if (dev_priv->stats.last_frame_reads > 1 ||
  569. dev_priv->stats.last_clear_reads > dev_priv->stats.clears) {
  570. dev_priv->stats.boxes |= RADEON_BOX_WAIT_IDLE;
  571. }
  572. if (dev_priv->stats.freelist_loops) {
  573. dev_priv->stats.boxes |= RADEON_BOX_WAIT_IDLE;
  574. }
  575. /* Purple box for page flipping
  576. */
  577. if ( dev_priv->stats.boxes & RADEON_BOX_FLIP )
  578. radeon_clear_box( dev_priv, 4, 4, 8, 8, 255, 0, 255 );
  579. /* Red box if we have to wait for idle at any point
  580. */
  581. if ( dev_priv->stats.boxes & RADEON_BOX_WAIT_IDLE )
  582. radeon_clear_box( dev_priv, 16, 4, 8, 8, 255, 0, 0 );
  583. /* Blue box: lost context?
  584. */
  585. /* Yellow box for texture swaps
  586. */
  587. if ( dev_priv->stats.boxes & RADEON_BOX_TEXTURE_LOAD )
  588. radeon_clear_box( dev_priv, 40, 4, 8, 8, 255, 255, 0 );
  589. /* Green box if hardware never idles (as far as we can tell)
  590. */
  591. if ( !(dev_priv->stats.boxes & RADEON_BOX_DMA_IDLE) )
  592. radeon_clear_box( dev_priv, 64, 4, 8, 8, 0, 255, 0 );
  593. /* Draw bars indicating number of buffers allocated
  594. * (not a great measure, easily confused)
  595. */
  596. if (dev_priv->stats.requested_bufs) {
  597. if (dev_priv->stats.requested_bufs > 100)
  598. dev_priv->stats.requested_bufs = 100;
  599. radeon_clear_box( dev_priv, 4, 16,
  600. dev_priv->stats.requested_bufs, 4,
  601. 196, 128, 128 );
  602. }
  603. memset( &dev_priv->stats, 0, sizeof(dev_priv->stats) );
  604. }
  605. /* ================================================================
  606. * CP command dispatch functions
  607. */
  608. static void radeon_cp_dispatch_clear( drm_device_t *dev,
  609. drm_radeon_clear_t *clear,
  610. drm_radeon_clear_rect_t *depth_boxes )
  611. {
  612. drm_radeon_private_t *dev_priv = dev->dev_private;
  613. drm_radeon_sarea_t *sarea_priv = dev_priv->sarea_priv;
  614. drm_radeon_depth_clear_t *depth_clear = &dev_priv->depth_clear;
  615. int nbox = sarea_priv->nbox;
  616. drm_clip_rect_t *pbox = sarea_priv->boxes;
  617. unsigned int flags = clear->flags;
  618. u32 rb3d_cntl = 0, rb3d_stencilrefmask= 0;
  619. int i;
  620. RING_LOCALS;
  621. DRM_DEBUG( "flags = 0x%x\n", flags );
  622. dev_priv->stats.clears++;
  623. if ( dev_priv->page_flipping && dev_priv->current_page == 1 ) {
  624. unsigned int tmp = flags;
  625. flags &= ~(RADEON_FRONT | RADEON_BACK);
  626. if ( tmp & RADEON_FRONT ) flags |= RADEON_BACK;
  627. if ( tmp & RADEON_BACK ) flags |= RADEON_FRONT;
  628. }
  629. if ( flags & (RADEON_FRONT | RADEON_BACK) ) {
  630. BEGIN_RING( 4 );
  631. /* Ensure the 3D stream is idle before doing a
  632. * 2D fill to clear the front or back buffer.
  633. */
  634. RADEON_WAIT_UNTIL_3D_IDLE();
  635. OUT_RING( CP_PACKET0( RADEON_DP_WRITE_MASK, 0 ) );
  636. OUT_RING( clear->color_mask );
  637. ADVANCE_RING();
  638. /* Make sure we restore the 3D state next time.
  639. */
  640. dev_priv->sarea_priv->ctx_owner = 0;
  641. for ( i = 0 ; i < nbox ; i++ ) {
  642. int x = pbox[i].x1;
  643. int y = pbox[i].y1;
  644. int w = pbox[i].x2 - x;
  645. int h = pbox[i].y2 - y;
  646. DRM_DEBUG( "dispatch clear %d,%d-%d,%d flags 0x%x\n",
  647. x, y, w, h, flags );
  648. if ( flags & RADEON_FRONT ) {
  649. BEGIN_RING( 6 );
  650. OUT_RING( CP_PACKET3( RADEON_CNTL_PAINT_MULTI, 4 ) );
  651. OUT_RING( RADEON_GMC_DST_PITCH_OFFSET_CNTL |
  652. RADEON_GMC_BRUSH_SOLID_COLOR |
  653. (dev_priv->color_fmt << 8) |
  654. RADEON_GMC_SRC_DATATYPE_COLOR |
  655. RADEON_ROP3_P |
  656. RADEON_GMC_CLR_CMP_CNTL_DIS );
  657. OUT_RING( dev_priv->front_pitch_offset );
  658. OUT_RING( clear->clear_color );
  659. OUT_RING( (x << 16) | y );
  660. OUT_RING( (w << 16) | h );
  661. ADVANCE_RING();
  662. }
  663. if ( flags & RADEON_BACK ) {
  664. BEGIN_RING( 6 );
  665. OUT_RING( CP_PACKET3( RADEON_CNTL_PAINT_MULTI, 4 ) );
  666. OUT_RING( RADEON_GMC_DST_PITCH_OFFSET_CNTL |
  667. RADEON_GMC_BRUSH_SOLID_COLOR |
  668. (dev_priv->color_fmt << 8) |
  669. RADEON_GMC_SRC_DATATYPE_COLOR |
  670. RADEON_ROP3_P |
  671. RADEON_GMC_CLR_CMP_CNTL_DIS );
  672. OUT_RING( dev_priv->back_pitch_offset );
  673. OUT_RING( clear->clear_color );
  674. OUT_RING( (x << 16) | y );
  675. OUT_RING( (w << 16) | h );
  676. ADVANCE_RING();
  677. }
  678. }
  679. }
  680. /* hyper z clear */
  681. /* no docs available, based on reverse engeneering by Stephane Marchesin */
  682. if ((flags & (RADEON_DEPTH | RADEON_STENCIL)) && (flags & RADEON_CLEAR_FASTZ)) {
  683. int i;
  684. int depthpixperline = dev_priv->depth_fmt==RADEON_DEPTH_FORMAT_16BIT_INT_Z?
  685. (dev_priv->depth_pitch / 2): (dev_priv->depth_pitch / 4);
  686. u32 clearmask;
  687. u32 tempRB3D_DEPTHCLEARVALUE = clear->clear_depth |
  688. ((clear->depth_mask & 0xff) << 24);
  689. /* Make sure we restore the 3D state next time.
  690. * we haven't touched any "normal" state - still need this?
  691. */
  692. dev_priv->sarea_priv->ctx_owner = 0;
  693. if ((dev_priv->flags & CHIP_HAS_HIERZ) && (flags & RADEON_USE_HIERZ)) {
  694. /* FIXME : reverse engineer that for Rx00 cards */
  695. /* FIXME : the mask supposedly contains low-res z values. So can't set
  696. just to the max (0xff? or actually 0x3fff?), need to take z clear
  697. value into account? */
  698. /* pattern seems to work for r100, though get slight
  699. rendering errors with glxgears. If hierz is not enabled for r100,
  700. only 4 bits which indicate clear (15,16,31,32, all zero) matter, the
  701. other ones are ignored, and the same clear mask can be used. That's
  702. very different behaviour than R200 which needs different clear mask
  703. and different number of tiles to clear if hierz is enabled or not !?!
  704. */
  705. clearmask = (0xff<<22)|(0xff<<6)| 0x003f003f;
  706. }
  707. else {
  708. /* clear mask : chooses the clearing pattern.
  709. rv250: could be used to clear only parts of macrotiles
  710. (but that would get really complicated...)?
  711. bit 0 and 1 (either or both of them ?!?!) are used to
  712. not clear tile (or maybe one of the bits indicates if the tile is
  713. compressed or not), bit 2 and 3 to not clear tile 1,...,.
  714. Pattern is as follows:
  715. | 0,1 | 4,5 | 8,9 |12,13|16,17|20,21|24,25|28,29|
  716. bits -------------------------------------------------
  717. | 2,3 | 6,7 |10,11|14,15|18,19|22,23|26,27|30,31|
  718. rv100: clearmask covers 2x8 4x1 tiles, but one clear still
  719. covers 256 pixels ?!?
  720. */
  721. clearmask = 0x0;
  722. }
  723. BEGIN_RING( 8 );
  724. RADEON_WAIT_UNTIL_2D_IDLE();
  725. OUT_RING_REG( RADEON_RB3D_DEPTHCLEARVALUE,
  726. tempRB3D_DEPTHCLEARVALUE);
  727. /* what offset is this exactly ? */
  728. OUT_RING_REG( RADEON_RB3D_ZMASKOFFSET, 0 );
  729. /* need ctlstat, otherwise get some strange black flickering */
  730. OUT_RING_REG( RADEON_RB3D_ZCACHE_CTLSTAT, RADEON_RB3D_ZC_FLUSH_ALL );
  731. ADVANCE_RING();
  732. for (i = 0; i < nbox; i++) {
  733. int tileoffset, nrtilesx, nrtilesy, j;
  734. /* it looks like r200 needs rv-style clears, at least if hierz is not enabled? */
  735. if ((dev_priv->flags&CHIP_HAS_HIERZ) && !(dev_priv->microcode_version==UCODE_R200)) {
  736. /* FIXME : figure this out for r200 (when hierz is enabled). Or
  737. maybe r200 actually doesn't need to put the low-res z value into
  738. the tile cache like r100, but just needs to clear the hi-level z-buffer?
  739. Works for R100, both with hierz and without.
  740. R100 seems to operate on 2x1 8x8 tiles, but...
  741. odd: offset/nrtiles need to be 64 pix (4 block) aligned? Potentially
  742. problematic with resolutions which are not 64 pix aligned? */
  743. tileoffset = ((pbox[i].y1 >> 3) * depthpixperline + pbox[i].x1) >> 6;
  744. nrtilesx = ((pbox[i].x2 & ~63) - (pbox[i].x1 & ~63)) >> 4;
  745. nrtilesy = (pbox[i].y2 >> 3) - (pbox[i].y1 >> 3);
  746. for (j = 0; j <= nrtilesy; j++) {
  747. BEGIN_RING( 4 );
  748. OUT_RING( CP_PACKET3( RADEON_3D_CLEAR_ZMASK, 2 ) );
  749. /* first tile */
  750. OUT_RING( tileoffset * 8 );
  751. /* the number of tiles to clear */
  752. OUT_RING( nrtilesx + 4 );
  753. /* clear mask : chooses the clearing pattern. */
  754. OUT_RING( clearmask );
  755. ADVANCE_RING();
  756. tileoffset += depthpixperline >> 6;
  757. }
  758. }
  759. else if (dev_priv->microcode_version==UCODE_R200) {
  760. /* works for rv250. */
  761. /* find first macro tile (8x2 4x4 z-pixels on rv250) */
  762. tileoffset = ((pbox[i].y1 >> 3) * depthpixperline + pbox[i].x1) >> 5;
  763. nrtilesx = (pbox[i].x2 >> 5) - (pbox[i].x1 >> 5);
  764. nrtilesy = (pbox[i].y2 >> 3) - (pbox[i].y1 >> 3);
  765. for (j = 0; j <= nrtilesy; j++) {
  766. BEGIN_RING( 4 );
  767. OUT_RING( CP_PACKET3( RADEON_3D_CLEAR_ZMASK, 2 ) );
  768. /* first tile */
  769. /* judging by the first tile offset needed, could possibly
  770. directly address/clear 4x4 tiles instead of 8x2 * 4x4
  771. macro tiles, though would still need clear mask for
  772. right/bottom if truely 4x4 granularity is desired ? */
  773. OUT_RING( tileoffset * 16 );
  774. /* the number of tiles to clear */
  775. OUT_RING( nrtilesx + 1 );
  776. /* clear mask : chooses the clearing pattern. */
  777. OUT_RING( clearmask );
  778. ADVANCE_RING();
  779. tileoffset += depthpixperline >> 5;
  780. }
  781. }
  782. else { /* rv 100 */
  783. /* rv100 might not need 64 pix alignment, who knows */
  784. /* offsets are, hmm, weird */
  785. tileoffset = ((pbox[i].y1 >> 4) * depthpixperline + pbox[i].x1) >> 6;
  786. nrtilesx = ((pbox[i].x2 & ~63) - (pbox[i].x1 & ~63)) >> 4;
  787. nrtilesy = (pbox[i].y2 >> 4) - (pbox[i].y1 >> 4);
  788. for (j = 0; j <= nrtilesy; j++) {
  789. BEGIN_RING( 4 );
  790. OUT_RING( CP_PACKET3( RADEON_3D_CLEAR_ZMASK, 2 ) );
  791. OUT_RING( tileoffset * 128 );
  792. /* the number of tiles to clear */
  793. OUT_RING( nrtilesx + 4 );
  794. /* clear mask : chooses the clearing pattern. */
  795. OUT_RING( clearmask );
  796. ADVANCE_RING();
  797. tileoffset += depthpixperline >> 6;
  798. }
  799. }
  800. }
  801. /* TODO don't always clear all hi-level z tiles */
  802. if ((dev_priv->flags & CHIP_HAS_HIERZ) && (dev_priv->microcode_version==UCODE_R200)
  803. && (flags & RADEON_USE_HIERZ))
  804. /* r100 and cards without hierarchical z-buffer have no high-level z-buffer */
  805. /* FIXME : the mask supposedly contains low-res z values. So can't set
  806. just to the max (0xff? or actually 0x3fff?), need to take z clear
  807. value into account? */
  808. {
  809. BEGIN_RING( 4 );
  810. OUT_RING( CP_PACKET3( RADEON_3D_CLEAR_HIZ, 2 ) );
  811. OUT_RING( 0x0 ); /* First tile */
  812. OUT_RING( 0x3cc0 );
  813. OUT_RING( (0xff<<22)|(0xff<<6)| 0x003f003f);
  814. ADVANCE_RING();
  815. }
  816. }
  817. /* We have to clear the depth and/or stencil buffers by
  818. * rendering a quad into just those buffers. Thus, we have to
  819. * make sure the 3D engine is configured correctly.
  820. */
  821. if ((dev_priv->microcode_version == UCODE_R200) &&
  822. (flags & (RADEON_DEPTH | RADEON_STENCIL))) {
  823. int tempPP_CNTL;
  824. int tempRE_CNTL;
  825. int tempRB3D_CNTL;
  826. int tempRB3D_ZSTENCILCNTL;
  827. int tempRB3D_STENCILREFMASK;
  828. int tempRB3D_PLANEMASK;
  829. int tempSE_CNTL;
  830. int tempSE_VTE_CNTL;
  831. int tempSE_VTX_FMT_0;
  832. int tempSE_VTX_FMT_1;
  833. int tempSE_VAP_CNTL;
  834. int tempRE_AUX_SCISSOR_CNTL;
  835. tempPP_CNTL = 0;
  836. tempRE_CNTL = 0;
  837. tempRB3D_CNTL = depth_clear->rb3d_cntl;
  838. tempRB3D_ZSTENCILCNTL = depth_clear->rb3d_zstencilcntl;
  839. tempRB3D_STENCILREFMASK = 0x0;
  840. tempSE_CNTL = depth_clear->se_cntl;
  841. /* Disable TCL */
  842. tempSE_VAP_CNTL = (/* SE_VAP_CNTL__FORCE_W_TO_ONE_MASK | */
  843. (0x9 << SE_VAP_CNTL__VF_MAX_VTX_NUM__SHIFT));
  844. tempRB3D_PLANEMASK = 0x0;
  845. tempRE_AUX_SCISSOR_CNTL = 0x0;
  846. tempSE_VTE_CNTL =
  847. SE_VTE_CNTL__VTX_XY_FMT_MASK |
  848. SE_VTE_CNTL__VTX_Z_FMT_MASK;
  849. /* Vertex format (X, Y, Z, W)*/
  850. tempSE_VTX_FMT_0 =
  851. SE_VTX_FMT_0__VTX_Z0_PRESENT_MASK |
  852. SE_VTX_FMT_0__VTX_W0_PRESENT_MASK;
  853. tempSE_VTX_FMT_1 = 0x0;
  854. /*
  855. * Depth buffer specific enables
  856. */
  857. if (flags & RADEON_DEPTH) {
  858. /* Enable depth buffer */
  859. tempRB3D_CNTL |= RADEON_Z_ENABLE;
  860. } else {
  861. /* Disable depth buffer */
  862. tempRB3D_CNTL &= ~RADEON_Z_ENABLE;
  863. }
  864. /*
  865. * Stencil buffer specific enables
  866. */
  867. if ( flags & RADEON_STENCIL ) {
  868. tempRB3D_CNTL |= RADEON_STENCIL_ENABLE;
  869. tempRB3D_STENCILREFMASK = clear->depth_mask;
  870. } else {
  871. tempRB3D_CNTL &= ~RADEON_STENCIL_ENABLE;
  872. tempRB3D_STENCILREFMASK = 0x00000000;
  873. }
  874. if (flags & RADEON_USE_COMP_ZBUF) {
  875. tempRB3D_ZSTENCILCNTL |= RADEON_Z_COMPRESSION_ENABLE |
  876. RADEON_Z_DECOMPRESSION_ENABLE;
  877. }
  878. if (flags & RADEON_USE_HIERZ) {
  879. tempRB3D_ZSTENCILCNTL |= RADEON_Z_HIERARCHY_ENABLE;
  880. }
  881. BEGIN_RING( 26 );
  882. RADEON_WAIT_UNTIL_2D_IDLE();
  883. OUT_RING_REG( RADEON_PP_CNTL, tempPP_CNTL );
  884. OUT_RING_REG( R200_RE_CNTL, tempRE_CNTL );
  885. OUT_RING_REG( RADEON_RB3D_CNTL, tempRB3D_CNTL );
  886. OUT_RING_REG( RADEON_RB3D_ZSTENCILCNTL,
  887. tempRB3D_ZSTENCILCNTL );
  888. OUT_RING_REG( RADEON_RB3D_STENCILREFMASK,
  889. tempRB3D_STENCILREFMASK );
  890. OUT_RING_REG( RADEON_RB3D_PLANEMASK, tempRB3D_PLANEMASK );
  891. OUT_RING_REG( RADEON_SE_CNTL, tempSE_CNTL );
  892. OUT_RING_REG( R200_SE_VTE_CNTL, tempSE_VTE_CNTL );
  893. OUT_RING_REG( R200_SE_VTX_FMT_0, tempSE_VTX_FMT_0 );
  894. OUT_RING_REG( R200_SE_VTX_FMT_1, tempSE_VTX_FMT_1 );
  895. OUT_RING_REG( R200_SE_VAP_CNTL, tempSE_VAP_CNTL );
  896. OUT_RING_REG( R200_RE_AUX_SCISSOR_CNTL,
  897. tempRE_AUX_SCISSOR_CNTL );
  898. ADVANCE_RING();
  899. /* Make sure we restore the 3D state next time.
  900. */
  901. dev_priv->sarea_priv->ctx_owner = 0;
  902. for ( i = 0 ; i < nbox ; i++ ) {
  903. /* Funny that this should be required --
  904. * sets top-left?
  905. */
  906. radeon_emit_clip_rect( dev_priv,
  907. &sarea_priv->boxes[i] );
  908. BEGIN_RING( 14 );
  909. OUT_RING( CP_PACKET3( R200_3D_DRAW_IMMD_2, 12 ) );
  910. OUT_RING( (RADEON_PRIM_TYPE_RECT_LIST |
  911. RADEON_PRIM_WALK_RING |
  912. (3 << RADEON_NUM_VERTICES_SHIFT)) );
  913. OUT_RING( depth_boxes[i].ui[CLEAR_X1] );
  914. OUT_RING( depth_boxes[i].ui[CLEAR_Y1] );
  915. OUT_RING( depth_boxes[i].ui[CLEAR_DEPTH] );
  916. OUT_RING( 0x3f800000 );
  917. OUT_RING( depth_boxes[i].ui[CLEAR_X1] );
  918. OUT_RING( depth_boxes[i].ui[CLEAR_Y2] );
  919. OUT_RING( depth_boxes[i].ui[CLEAR_DEPTH] );
  920. OUT_RING( 0x3f800000 );
  921. OUT_RING( depth_boxes[i].ui[CLEAR_X2] );
  922. OUT_RING( depth_boxes[i].ui[CLEAR_Y2] );
  923. OUT_RING( depth_boxes[i].ui[CLEAR_DEPTH] );
  924. OUT_RING( 0x3f800000 );
  925. ADVANCE_RING();
  926. }
  927. }
  928. else if ( (flags & (RADEON_DEPTH | RADEON_STENCIL)) ) {
  929. int tempRB3D_ZSTENCILCNTL = depth_clear->rb3d_zstencilcntl;
  930. rb3d_cntl = depth_clear->rb3d_cntl;
  931. if ( flags & RADEON_DEPTH ) {
  932. rb3d_cntl |= RADEON_Z_ENABLE;
  933. } else {
  934. rb3d_cntl &= ~RADEON_Z_ENABLE;
  935. }
  936. if ( flags & RADEON_STENCIL ) {
  937. rb3d_cntl |= RADEON_STENCIL_ENABLE;
  938. rb3d_stencilrefmask = clear->depth_mask; /* misnamed field */
  939. } else {
  940. rb3d_cntl &= ~RADEON_STENCIL_ENABLE;
  941. rb3d_stencilrefmask = 0x00000000;
  942. }
  943. if (flags & RADEON_USE_COMP_ZBUF) {
  944. tempRB3D_ZSTENCILCNTL |= RADEON_Z_COMPRESSION_ENABLE |
  945. RADEON_Z_DECOMPRESSION_ENABLE;
  946. }
  947. if (flags & RADEON_USE_HIERZ) {
  948. tempRB3D_ZSTENCILCNTL |= RADEON_Z_HIERARCHY_ENABLE;
  949. }
  950. BEGIN_RING( 13 );
  951. RADEON_WAIT_UNTIL_2D_IDLE();
  952. OUT_RING( CP_PACKET0( RADEON_PP_CNTL, 1 ) );
  953. OUT_RING( 0x00000000 );
  954. OUT_RING( rb3d_cntl );
  955. OUT_RING_REG( RADEON_RB3D_ZSTENCILCNTL, tempRB3D_ZSTENCILCNTL );
  956. OUT_RING_REG( RADEON_RB3D_STENCILREFMASK,
  957. rb3d_stencilrefmask );
  958. OUT_RING_REG( RADEON_RB3D_PLANEMASK,
  959. 0x00000000 );
  960. OUT_RING_REG( RADEON_SE_CNTL,
  961. depth_clear->se_cntl );
  962. ADVANCE_RING();
  963. /* Make sure we restore the 3D state next time.
  964. */
  965. dev_priv->sarea_priv->ctx_owner = 0;
  966. for ( i = 0 ; i < nbox ; i++ ) {
  967. /* Funny that this should be required --
  968. * sets top-left?
  969. */
  970. radeon_emit_clip_rect( dev_priv,
  971. &sarea_priv->boxes[i] );
  972. BEGIN_RING( 15 );
  973. OUT_RING( CP_PACKET3( RADEON_3D_DRAW_IMMD, 13 ) );
  974. OUT_RING( RADEON_VTX_Z_PRESENT |
  975. RADEON_VTX_PKCOLOR_PRESENT);
  976. OUT_RING( (RADEON_PRIM_TYPE_RECT_LIST |
  977. RADEON_PRIM_WALK_RING |
  978. RADEON_MAOS_ENABLE |
  979. RADEON_VTX_FMT_RADEON_MODE |
  980. (3 << RADEON_NUM_VERTICES_SHIFT)) );
  981. OUT_RING( depth_boxes[i].ui[CLEAR_X1] );
  982. OUT_RING( depth_boxes[i].ui[CLEAR_Y1] );
  983. OUT_RING( depth_boxes[i].ui[CLEAR_DEPTH] );
  984. OUT_RING( 0x0 );
  985. OUT_RING( depth_boxes[i].ui[CLEAR_X1] );
  986. OUT_RING( depth_boxes[i].ui[CLEAR_Y2] );
  987. OUT_RING( depth_boxes[i].ui[CLEAR_DEPTH] );
  988. OUT_RING( 0x0 );
  989. OUT_RING( depth_boxes[i].ui[CLEAR_X2] );
  990. OUT_RING( depth_boxes[i].ui[CLEAR_Y2] );
  991. OUT_RING( depth_boxes[i].ui[CLEAR_DEPTH] );
  992. OUT_RING( 0x0 );
  993. ADVANCE_RING();
  994. }
  995. }
  996. /* Increment the clear counter. The client-side 3D driver must
  997. * wait on this value before performing the clear ioctl. We
  998. * need this because the card's so damned fast...
  999. */
  1000. dev_priv->sarea_priv->last_clear++;
  1001. BEGIN_RING( 4 );
  1002. RADEON_CLEAR_AGE( dev_priv->sarea_priv->last_clear );
  1003. RADEON_WAIT_UNTIL_IDLE();
  1004. ADVANCE_RING();
  1005. }
  1006. static void radeon_cp_dispatch_swap( drm_device_t *dev )
  1007. {
  1008. drm_radeon_private_t *dev_priv = dev->dev_private;
  1009. drm_radeon_sarea_t *sarea_priv = dev_priv->sarea_priv;
  1010. int nbox = sarea_priv->nbox;
  1011. drm_clip_rect_t *pbox = sarea_priv->boxes;
  1012. int i;
  1013. RING_LOCALS;
  1014. DRM_DEBUG( "\n" );
  1015. /* Do some trivial performance monitoring...
  1016. */
  1017. if (dev_priv->do_boxes)
  1018. radeon_cp_performance_boxes( dev_priv );
  1019. /* Wait for the 3D stream to idle before dispatching the bitblt.
  1020. * This will prevent data corruption between the two streams.
  1021. */
  1022. BEGIN_RING( 2 );
  1023. RADEON_WAIT_UNTIL_3D_IDLE();
  1024. ADVANCE_RING();
  1025. for ( i = 0 ; i < nbox ; i++ ) {
  1026. int x = pbox[i].x1;
  1027. int y = pbox[i].y1;
  1028. int w = pbox[i].x2 - x;
  1029. int h = pbox[i].y2 - y;
  1030. DRM_DEBUG( "dispatch swap %d,%d-%d,%d\n",
  1031. x, y, w, h );
  1032. BEGIN_RING( 7 );
  1033. OUT_RING( CP_PACKET3( RADEON_CNTL_BITBLT_MULTI, 5 ) );
  1034. OUT_RING( RADEON_GMC_SRC_PITCH_OFFSET_CNTL |
  1035. RADEON_GMC_DST_PITCH_OFFSET_CNTL |
  1036. RADEON_GMC_BRUSH_NONE |
  1037. (dev_priv->color_fmt << 8) |
  1038. RADEON_GMC_SRC_DATATYPE_COLOR |
  1039. RADEON_ROP3_S |
  1040. RADEON_DP_SRC_SOURCE_MEMORY |
  1041. RADEON_GMC_CLR_CMP_CNTL_DIS |
  1042. RADEON_GMC_WR_MSK_DIS );
  1043. /* Make this work even if front & back are flipped:
  1044. */
  1045. if (dev_priv->current_page == 0) {
  1046. OUT_RING( dev_priv->back_pitch_offset );
  1047. OUT_RING( dev_priv->front_pitch_offset );
  1048. }
  1049. else {
  1050. OUT_RING( dev_priv->front_pitch_offset );
  1051. OUT_RING( dev_priv->back_pitch_offset );
  1052. }
  1053. OUT_RING( (x << 16) | y );
  1054. OUT_RING( (x << 16) | y );
  1055. OUT_RING( (w << 16) | h );
  1056. ADVANCE_RING();
  1057. }
  1058. /* Increment the frame counter. The client-side 3D driver must
  1059. * throttle the framerate by waiting for this value before
  1060. * performing the swapbuffer ioctl.
  1061. */
  1062. dev_priv->sarea_priv->last_frame++;
  1063. BEGIN_RING( 4 );
  1064. RADEON_FRAME_AGE( dev_priv->sarea_priv->last_frame );
  1065. RADEON_WAIT_UNTIL_2D_IDLE();
  1066. ADVANCE_RING();
  1067. }
  1068. static void radeon_cp_dispatch_flip( drm_device_t *dev )
  1069. {
  1070. drm_radeon_private_t *dev_priv = dev->dev_private;
  1071. drm_sarea_t *sarea = (drm_sarea_t *)dev_priv->sarea->handle;
  1072. int offset = (dev_priv->current_page == 1)
  1073. ? dev_priv->front_offset : dev_priv->back_offset;
  1074. RING_LOCALS;
  1075. DRM_DEBUG( "%s: page=%d pfCurrentPage=%d\n",
  1076. __FUNCTION__,
  1077. dev_priv->current_page,
  1078. dev_priv->sarea_priv->pfCurrentPage);
  1079. /* Do some trivial performance monitoring...
  1080. */
  1081. if (dev_priv->do_boxes) {
  1082. dev_priv->stats.boxes |= RADEON_BOX_FLIP;
  1083. radeon_cp_performance_boxes( dev_priv );
  1084. }
  1085. /* Update the frame offsets for both CRTCs
  1086. */
  1087. BEGIN_RING( 6 );
  1088. RADEON_WAIT_UNTIL_3D_IDLE();
  1089. OUT_RING_REG( RADEON_CRTC_OFFSET, ( ( sarea->frame.y * dev_priv->front_pitch
  1090. + sarea->frame.x
  1091. * ( dev_priv->color_fmt - 2 ) ) & ~7 )
  1092. + offset );
  1093. OUT_RING_REG( RADEON_CRTC2_OFFSET, dev_priv->sarea_priv->crtc2_base
  1094. + offset );
  1095. ADVANCE_RING();
  1096. /* Increment the frame counter. The client-side 3D driver must
  1097. * throttle the framerate by waiting for this value before
  1098. * performing the swapbuffer ioctl.
  1099. */
  1100. dev_priv->sarea_priv->last_frame++;
  1101. dev_priv->sarea_priv->pfCurrentPage = dev_priv->current_page =
  1102. 1 - dev_priv->current_page;
  1103. BEGIN_RING( 2 );
  1104. RADEON_FRAME_AGE( dev_priv->sarea_priv->last_frame );
  1105. ADVANCE_RING();
  1106. }
  1107. static int bad_prim_vertex_nr( int primitive, int nr )
  1108. {
  1109. switch (primitive & RADEON_PRIM_TYPE_MASK) {
  1110. case RADEON_PRIM_TYPE_NONE:
  1111. case RADEON_PRIM_TYPE_POINT:
  1112. return nr < 1;
  1113. case RADEON_PRIM_TYPE_LINE:
  1114. return (nr & 1) || nr == 0;
  1115. case RADEON_PRIM_TYPE_LINE_STRIP:
  1116. return nr < 2;
  1117. case RADEON_PRIM_TYPE_TRI_LIST:
  1118. case RADEON_PRIM_TYPE_3VRT_POINT_LIST:
  1119. case RADEON_PRIM_TYPE_3VRT_LINE_LIST:
  1120. case RADEON_PRIM_TYPE_RECT_LIST:
  1121. return nr % 3 || nr == 0;
  1122. case RADEON_PRIM_TYPE_TRI_FAN:
  1123. case RADEON_PRIM_TYPE_TRI_STRIP:
  1124. return nr < 3;
  1125. default:
  1126. return 1;
  1127. }
  1128. }
  1129. typedef struct {
  1130. unsigned int start;
  1131. unsigned int finish;
  1132. unsigned int prim;
  1133. unsigned int numverts;
  1134. unsigned int offset;
  1135. unsigned int vc_format;
  1136. } drm_radeon_tcl_prim_t;
  1137. static void radeon_cp_dispatch_vertex( drm_device_t *dev,
  1138. drm_buf_t *buf,
  1139. drm_radeon_tcl_prim_t *prim )
  1140. {
  1141. drm_radeon_private_t *dev_priv = dev->dev_private;
  1142. drm_radeon_sarea_t *sarea_priv = dev_priv->sarea_priv;
  1143. int offset = dev_priv->gart_buffers_offset + buf->offset + prim->start;
  1144. int numverts = (int)prim->numverts;
  1145. int nbox = sarea_priv->nbox;
  1146. int i = 0;
  1147. RING_LOCALS;
  1148. DRM_DEBUG("hwprim 0x%x vfmt 0x%x %d..%d %d verts\n",
  1149. prim->prim,
  1150. prim->vc_format,
  1151. prim->start,
  1152. prim->finish,
  1153. prim->numverts);
  1154. if (bad_prim_vertex_nr( prim->prim, prim->numverts )) {
  1155. DRM_ERROR( "bad prim %x numverts %d\n",
  1156. prim->prim, prim->numverts );
  1157. return;
  1158. }
  1159. do {
  1160. /* Emit the next cliprect */
  1161. if ( i < nbox ) {
  1162. radeon_emit_clip_rect( dev_priv,
  1163. &sarea_priv->boxes[i] );
  1164. }
  1165. /* Emit the vertex buffer rendering commands */
  1166. BEGIN_RING( 5 );
  1167. OUT_RING( CP_PACKET3( RADEON_3D_RNDR_GEN_INDX_PRIM, 3 ) );
  1168. OUT_RING( offset );
  1169. OUT_RING( numverts );
  1170. OUT_RING( prim->vc_format );
  1171. OUT_RING( prim->prim | RADEON_PRIM_WALK_LIST |
  1172. RADEON_COLOR_ORDER_RGBA |
  1173. RADEON_VTX_FMT_RADEON_MODE |
  1174. (numverts << RADEON_NUM_VERTICES_SHIFT) );
  1175. ADVANCE_RING();
  1176. i++;
  1177. } while ( i < nbox );
  1178. }
  1179. static void radeon_cp_discard_buffer( drm_device_t *dev, drm_buf_t *buf )
  1180. {
  1181. drm_radeon_private_t *dev_priv = dev->dev_private;
  1182. drm_radeon_buf_priv_t *buf_priv = buf->dev_private;
  1183. RING_LOCALS;
  1184. buf_priv->age = ++dev_priv->sarea_priv->last_dispatch;
  1185. /* Emit the vertex buffer age */
  1186. BEGIN_RING( 2 );
  1187. RADEON_DISPATCH_AGE( buf_priv->age );
  1188. ADVANCE_RING();
  1189. buf->pending = 1;
  1190. buf->used = 0;
  1191. }
  1192. static void radeon_cp_dispatch_indirect( drm_device_t *dev,
  1193. drm_buf_t *buf,
  1194. int start, int end )
  1195. {
  1196. drm_radeon_private_t *dev_priv = dev->dev_private;
  1197. RING_LOCALS;
  1198. DRM_DEBUG( "indirect: buf=%d s=0x%x e=0x%x\n",
  1199. buf->idx, start, end );
  1200. if ( start != end ) {
  1201. int offset = (dev_priv->gart_buffers_offset
  1202. + buf->offset + start);
  1203. int dwords = (end - start + 3) / sizeof(u32);
  1204. /* Indirect buffer data must be an even number of
  1205. * dwords, so if we've been given an odd number we must
  1206. * pad the data with a Type-2 CP packet.
  1207. */
  1208. if ( dwords & 1 ) {
  1209. u32 *data = (u32 *)
  1210. ((char *)dev->agp_buffer_map->handle
  1211. + buf->offset + start);
  1212. data[dwords++] = RADEON_CP_PACKET2;
  1213. }
  1214. /* Fire off the indirect buffer */
  1215. BEGIN_RING( 3 );
  1216. OUT_RING( CP_PACKET0( RADEON_CP_IB_BASE, 1 ) );
  1217. OUT_RING( offset );
  1218. OUT_RING( dwords );
  1219. ADVANCE_RING();
  1220. }
  1221. }
  1222. static void radeon_cp_dispatch_indices( drm_device_t *dev,
  1223. drm_buf_t *elt_buf,
  1224. drm_radeon_tcl_prim_t *prim )
  1225. {
  1226. drm_radeon_private_t *dev_priv = dev->dev_private;
  1227. drm_radeon_sarea_t *sarea_priv = dev_priv->sarea_priv;
  1228. int offset = dev_priv->gart_buffers_offset + prim->offset;
  1229. u32 *data;
  1230. int dwords;
  1231. int i = 0;
  1232. int start = prim->start + RADEON_INDEX_PRIM_OFFSET;
  1233. int count = (prim->finish - start) / sizeof(u16);
  1234. int nbox = sarea_priv->nbox;
  1235. DRM_DEBUG("hwprim 0x%x vfmt 0x%x %d..%d offset: %x nr %d\n",
  1236. prim->prim,
  1237. prim->vc_format,
  1238. prim->start,
  1239. prim->finish,
  1240. prim->offset,
  1241. prim->numverts);
  1242. if (bad_prim_vertex_nr( prim->prim, count )) {
  1243. DRM_ERROR( "bad prim %x count %d\n",
  1244. prim->prim, count );
  1245. return;
  1246. }
  1247. if ( start >= prim->finish ||
  1248. (prim->start & 0x7) ) {
  1249. DRM_ERROR( "buffer prim %d\n", prim->prim );
  1250. return;
  1251. }
  1252. dwords = (prim->finish - prim->start + 3) / sizeof(u32);
  1253. data = (u32 *)((char *)dev->agp_buffer_map->handle +
  1254. elt_buf->offset + prim->start);
  1255. data[0] = CP_PACKET3( RADEON_3D_RNDR_GEN_INDX_PRIM, dwords-2 );
  1256. data[1] = offset;
  1257. data[2] = prim->numverts;
  1258. data[3] = prim->vc_format;
  1259. data[4] = (prim->prim |
  1260. RADEON_PRIM_WALK_IND |
  1261. RADEON_COLOR_ORDER_RGBA |
  1262. RADEON_VTX_FMT_RADEON_MODE |
  1263. (count << RADEON_NUM_VERTICES_SHIFT) );
  1264. do {
  1265. if ( i < nbox )
  1266. radeon_emit_clip_rect( dev_priv,
  1267. &sarea_priv->boxes[i] );
  1268. radeon_cp_dispatch_indirect( dev, elt_buf,
  1269. prim->start,
  1270. prim->finish );
  1271. i++;
  1272. } while ( i < nbox );
  1273. }
  1274. #define RADEON_MAX_TEXTURE_SIZE (RADEON_BUFFER_SIZE - 8 * sizeof(u32))
  1275. static int radeon_cp_dispatch_texture( DRMFILE filp,
  1276. drm_device_t *dev,
  1277. drm_radeon_texture_t *tex,
  1278. drm_radeon_tex_image_t *image )
  1279. {
  1280. drm_radeon_private_t *dev_priv = dev->dev_private;
  1281. drm_file_t *filp_priv;
  1282. drm_buf_t *buf;
  1283. u32 format;
  1284. u32 *buffer;
  1285. const u8 __user *data;
  1286. int size, dwords, tex_width, blit_width;
  1287. u32 height;
  1288. int i;
  1289. u32 texpitch, microtile;
  1290. RING_LOCALS;
  1291. DRM_GET_PRIV_WITH_RETURN( filp_priv, filp );
  1292. if ( radeon_check_and_fixup_offset( dev_priv, filp_priv, &tex->offset ) ) {
  1293. DRM_ERROR( "Invalid destination offset\n" );
  1294. return DRM_ERR( EINVAL );
  1295. }
  1296. dev_priv->stats.boxes |= RADEON_BOX_TEXTURE_LOAD;
  1297. /* Flush the pixel cache. This ensures no pixel data gets mixed
  1298. * up with the texture data from the host data blit, otherwise
  1299. * part of the texture image may be corrupted.
  1300. */
  1301. BEGIN_RING( 4 );
  1302. RADEON_FLUSH_CACHE();
  1303. RADEON_WAIT_UNTIL_IDLE();
  1304. ADVANCE_RING();
  1305. #ifdef __BIG_ENDIAN
  1306. /* The Mesa texture functions provide the data in little endian as the
  1307. * chip wants it, but we need to compensate for the fact that the CP
  1308. * ring gets byte-swapped
  1309. */
  1310. BEGIN_RING( 2 );
  1311. OUT_RING_REG( RADEON_RBBM_GUICNTL, RADEON_HOST_DATA_SWAP_32BIT );
  1312. ADVANCE_RING();
  1313. #endif
  1314. /* The compiler won't optimize away a division by a variable,
  1315. * even if the only legal values are powers of two. Thus, we'll
  1316. * use a shift instead.
  1317. */
  1318. switch ( tex->format ) {
  1319. case RADEON_TXFORMAT_ARGB8888:
  1320. case RADEON_TXFORMAT_RGBA8888:
  1321. format = RADEON_COLOR_FORMAT_ARGB8888;
  1322. tex_width = tex->width * 4;
  1323. blit_width = image->width * 4;
  1324. break;
  1325. case RADEON_TXFORMAT_AI88:
  1326. case RADEON_TXFORMAT_ARGB1555:
  1327. case RADEON_TXFORMAT_RGB565:
  1328. case RADEON_TXFORMAT_ARGB4444:
  1329. case RADEON_TXFORMAT_VYUY422:
  1330. case RADEON_TXFORMAT_YVYU422:
  1331. format = RADEON_COLOR_FORMAT_RGB565;
  1332. tex_width = tex->width * 2;
  1333. blit_width = image->width * 2;
  1334. break;
  1335. case RADEON_TXFORMAT_I8:
  1336. case RADEON_TXFORMAT_RGB332:
  1337. format = RADEON_COLOR_FORMAT_CI8;
  1338. tex_width = tex->width * 1;
  1339. blit_width = image->width * 1;
  1340. break;
  1341. default:
  1342. DRM_ERROR( "invalid texture format %d\n", tex->format );
  1343. return DRM_ERR(EINVAL);
  1344. }
  1345. texpitch = tex->pitch;
  1346. if ((texpitch << 22) & RADEON_DST_TILE_MICRO) {
  1347. microtile = 1;
  1348. if (tex_width < 64) {
  1349. texpitch &= ~(RADEON_DST_TILE_MICRO >> 22);
  1350. /* we got tiled coordinates, untile them */
  1351. image->x *= 2;
  1352. }
  1353. }
  1354. else microtile = 0;
  1355. DRM_DEBUG("tex=%dx%d blit=%d\n", tex_width, tex->height, blit_width );
  1356. do {
  1357. DRM_DEBUG( "tex: ofs=0x%x p=%d f=%d x=%hd y=%hd w=%hd h=%hd\n",
  1358. tex->offset >> 10, tex->pitch, tex->format,
  1359. image->x, image->y, image->width, image->height );
  1360. /* Make a copy of some parameters in case we have to
  1361. * update them for a multi-pass texture blit.
  1362. */
  1363. height = image->height;
  1364. data = (const u8 __user *)image->data;
  1365. size = height * blit_width;
  1366. if ( size > RADEON_MAX_TEXTURE_SIZE ) {
  1367. height = RADEON_MAX_TEXTURE_SIZE / blit_width;
  1368. size = height * blit_width;
  1369. } else if ( size < 4 && size > 0 ) {
  1370. size = 4;
  1371. } else if ( size == 0 ) {
  1372. return 0;
  1373. }
  1374. buf = radeon_freelist_get( dev );
  1375. if ( 0 && !buf ) {
  1376. radeon_do_cp_idle( dev_priv );
  1377. buf = radeon_freelist_get( dev );
  1378. }
  1379. if ( !buf ) {
  1380. DRM_DEBUG("radeon_cp_dispatch_texture: EAGAIN\n");
  1381. if (DRM_COPY_TO_USER( tex->image, image, sizeof(*image) ))
  1382. return DRM_ERR(EFAULT);
  1383. return DRM_ERR(EAGAIN);
  1384. }
  1385. /* Dispatch the indirect buffer.
  1386. */
  1387. buffer = (u32*)((char*)dev->agp_buffer_map->handle + buf->offset);
  1388. dwords = size / 4;
  1389. buffer[0] = CP_PACKET3( RADEON_CNTL_HOSTDATA_BLT, dwords + 6 );
  1390. buffer[1] = (RADEON_GMC_DST_PITCH_OFFSET_CNTL |
  1391. RADEON_GMC_BRUSH_NONE |
  1392. (format << 8) |
  1393. RADEON_GMC_SRC_DATATYPE_COLOR |
  1394. RADEON_ROP3_S |
  1395. RADEON_DP_SRC_SOURCE_HOST_DATA |
  1396. RADEON_GMC_CLR_CMP_CNTL_DIS |
  1397. RADEON_GMC_WR_MSK_DIS);
  1398. buffer[2] = (texpitch << 22) | (tex->offset >> 10);
  1399. buffer[3] = 0xffffffff;
  1400. buffer[4] = 0xffffffff;
  1401. buffer[5] = (image->y << 16) | image->x;
  1402. buffer[6] = (height << 16) | image->width;
  1403. buffer[7] = dwords;
  1404. buffer += 8;
  1405. if (microtile) {
  1406. /* texture micro tiling in use, minimum texture width is thus 16 bytes.
  1407. however, we cannot use blitter directly for texture width < 64 bytes,
  1408. since minimum tex pitch is 64 bytes and we need this to match
  1409. the texture width, otherwise the blitter will tile it wrong.
  1410. Thus, tiling manually in this case. Additionally, need to special
  1411. case tex height = 1, since our actual image will have height 2
  1412. and we need to ensure we don't read beyond the texture size
  1413. from user space. */
  1414. if (tex->height == 1) {
  1415. if (tex_width >= 64 || tex_width <= 16) {
  1416. if (DRM_COPY_FROM_USER(buffer, data,
  1417. tex_width * sizeof(u32))) {
  1418. DRM_ERROR("EFAULT on pad, %d bytes\n",
  1419. tex_width);
  1420. return DRM_ERR(EFAULT);
  1421. }
  1422. } else if (tex_width == 32) {
  1423. if (DRM_COPY_FROM_USER(buffer, data, 16)) {
  1424. DRM_ERROR("EFAULT on pad, %d bytes\n",
  1425. tex_width);
  1426. return DRM_ERR(EFAULT);
  1427. }
  1428. if (DRM_COPY_FROM_USER(buffer + 8, data + 16, 16)) {
  1429. DRM_ERROR("EFAULT on pad, %d bytes\n",
  1430. tex_width);
  1431. return DRM_ERR(EFAULT);
  1432. }
  1433. }
  1434. } else if (tex_width >= 64 || tex_width == 16) {
  1435. if (DRM_COPY_FROM_USER(buffer, data,
  1436. dwords * sizeof(u32))) {
  1437. DRM_ERROR("EFAULT on data, %d dwords\n",
  1438. dwords);
  1439. return DRM_ERR(EFAULT);
  1440. }
  1441. } else if (tex_width < 16) {
  1442. for (i = 0; i < tex->height; i++) {
  1443. if (DRM_COPY_FROM_USER(buffer, data, tex_width)) {
  1444. DRM_ERROR("EFAULT on pad, %d bytes\n",
  1445. tex_width);
  1446. return DRM_ERR(EFAULT);
  1447. }
  1448. buffer += 4;
  1449. data += tex_width;
  1450. }
  1451. } else if (tex_width == 32) {
  1452. /* TODO: make sure this works when not fitting in one buffer
  1453. (i.e. 32bytes x 2048...) */
  1454. for (i = 0; i < tex->height; i += 2) {
  1455. if (DRM_COPY_FROM_USER(buffer, data, 16)) {
  1456. DRM_ERROR("EFAULT on pad, %d bytes\n",
  1457. tex_width);
  1458. return DRM_ERR(EFAULT);
  1459. }
  1460. data += 16;
  1461. if (DRM_COPY_FROM_USER(buffer + 8, data, 16)) {
  1462. DRM_ERROR("EFAULT on pad, %d bytes\n",
  1463. tex_width);
  1464. return DRM_ERR(EFAULT);
  1465. }
  1466. data += 16;
  1467. if (DRM_COPY_FROM_USER(buffer + 4, data, 16)) {
  1468. DRM_ERROR("EFAULT on pad, %d bytes\n",
  1469. tex_width);
  1470. return DRM_ERR(EFAULT);
  1471. }
  1472. data += 16;
  1473. if (DRM_COPY_FROM_USER(buffer + 12, data, 16)) {
  1474. DRM_ERROR("EFAULT on pad, %d bytes\n",
  1475. tex_width);
  1476. return DRM_ERR(EFAULT);
  1477. }
  1478. data += 16;
  1479. buffer += 16;
  1480. }
  1481. }
  1482. }
  1483. else {
  1484. if (tex_width >= 32) {
  1485. /* Texture image width is larger than the minimum, so we
  1486. * can upload it directly.
  1487. */
  1488. if (DRM_COPY_FROM_USER(buffer, data,
  1489. dwords * sizeof(u32))) {
  1490. DRM_ERROR("EFAULT on data, %d dwords\n",
  1491. dwords);
  1492. return DRM_ERR(EFAULT);
  1493. }
  1494. } else {
  1495. /* Texture image width is less than the minimum, so we
  1496. * need to pad out each image scanline to the minimum
  1497. * width.
  1498. */
  1499. for (i = 0 ; i < tex->height ; i++) {
  1500. if (DRM_COPY_FROM_USER(buffer, data, tex_width )) {
  1501. DRM_ERROR("EFAULT on pad, %d bytes\n", tex_width);
  1502. return DRM_ERR(EFAULT);
  1503. }
  1504. buffer += 8;
  1505. data += tex_width;
  1506. }
  1507. }
  1508. }
  1509. buf->filp = filp;
  1510. buf->used = (dwords + 8) * sizeof(u32);
  1511. radeon_cp_dispatch_indirect( dev, buf, 0, buf->used );
  1512. radeon_cp_discard_buffer( dev, buf );
  1513. /* Update the input parameters for next time */
  1514. image->y += height;
  1515. image->height -= height;
  1516. image->data = (const u8 __user *)image->data + size;
  1517. } while (image->height > 0);
  1518. /* Flush the pixel cache after the blit completes. This ensures
  1519. * the texture data is written out to memory before rendering
  1520. * continues.
  1521. */
  1522. BEGIN_RING( 4 );
  1523. RADEON_FLUSH_CACHE();
  1524. RADEON_WAIT_UNTIL_2D_IDLE();
  1525. ADVANCE_RING();
  1526. return 0;
  1527. }
  1528. static void radeon_cp_dispatch_stipple( drm_device_t *dev, u32 *stipple )
  1529. {
  1530. drm_radeon_private_t *dev_priv = dev->dev_private;
  1531. int i;
  1532. RING_LOCALS;
  1533. DRM_DEBUG( "\n" );
  1534. BEGIN_RING( 35 );
  1535. OUT_RING( CP_PACKET0( RADEON_RE_STIPPLE_ADDR, 0 ) );
  1536. OUT_RING( 0x00000000 );
  1537. OUT_RING( CP_PACKET0_TABLE( RADEON_RE_STIPPLE_DATA, 31 ) );
  1538. for ( i = 0 ; i < 32 ; i++ ) {
  1539. OUT_RING( stipple[i] );
  1540. }
  1541. ADVANCE_RING();
  1542. }
  1543. static void radeon_apply_surface_regs(int surf_index, drm_radeon_private_t *dev_priv)
  1544. {
  1545. if (!dev_priv->mmio)
  1546. return;
  1547. radeon_do_cp_idle(dev_priv);
  1548. RADEON_WRITE(RADEON_SURFACE0_INFO + 16*surf_index,
  1549. dev_priv->surfaces[surf_index].flags);
  1550. RADEON_WRITE(RADEON_SURFACE0_LOWER_BOUND + 16*surf_index,
  1551. dev_priv->surfaces[surf_index].lower);
  1552. RADEON_WRITE(RADEON_SURFACE0_UPPER_BOUND + 16*surf_index,
  1553. dev_priv->surfaces[surf_index].upper);
  1554. }
  1555. /* Allocates a virtual surface
  1556. * doesn't always allocate a real surface, will stretch an existing
  1557. * surface when possible.
  1558. *
  1559. * Note that refcount can be at most 2, since during a free refcount=3
  1560. * might mean we have to allocate a new surface which might not always
  1561. * be available.
  1562. * For example : we allocate three contigous surfaces ABC. If B is
  1563. * freed, we suddenly need two surfaces to store A and C, which might
  1564. * not always be available.
  1565. */
  1566. static int alloc_surface(drm_radeon_surface_alloc_t* new, drm_radeon_private_t *dev_priv, DRMFILE filp)
  1567. {
  1568. struct radeon_virt_surface *s;
  1569. int i;
  1570. int virt_surface_index;
  1571. uint32_t new_upper, new_lower;
  1572. new_lower = new->address;
  1573. new_upper = new_lower + new->size - 1;
  1574. /* sanity check */
  1575. if ((new_lower >= new_upper) || (new->flags == 0) || (new->size == 0) ||
  1576. ((new_upper & RADEON_SURF_ADDRESS_FIXED_MASK) != RADEON_SURF_ADDRESS_FIXED_MASK) ||
  1577. ((new_lower & RADEON_SURF_ADDRESS_FIXED_MASK) != 0))
  1578. return -1;
  1579. /* make sure there is no overlap with existing surfaces */
  1580. for (i = 0; i < RADEON_MAX_SURFACES; i++) {
  1581. if ((dev_priv->surfaces[i].refcount != 0) &&
  1582. (( (new_lower >= dev_priv->surfaces[i].lower) &&
  1583. (new_lower < dev_priv->surfaces[i].upper) ) ||
  1584. ( (new_lower < dev_priv->surfaces[i].lower) &&
  1585. (new_upper > dev_priv->surfaces[i].lower) )) ){
  1586. return -1;}
  1587. }
  1588. /* find a virtual surface */
  1589. for (i = 0; i < 2*RADEON_MAX_SURFACES; i++)
  1590. if (dev_priv->virt_surfaces[i].filp == 0)
  1591. break;
  1592. if (i == 2*RADEON_MAX_SURFACES) {
  1593. return -1;}
  1594. virt_surface_index = i;
  1595. /* try to reuse an existing surface */
  1596. for (i = 0; i < RADEON_MAX_SURFACES; i++) {
  1597. /* extend before */
  1598. if ((dev_priv->surfaces[i].refcount == 1) &&
  1599. (new->flags == dev_priv->surfaces[i].flags) &&
  1600. (new_upper + 1 == dev_priv->surfaces[i].lower)) {
  1601. s = &(dev_priv->virt_surfaces[virt_surface_index]);
  1602. s->surface_index = i;
  1603. s->lower = new_lower;
  1604. s->upper = new_upper;
  1605. s->flags = new->flags;
  1606. s->filp = filp;
  1607. dev_priv->surfaces[i].refcount++;
  1608. dev_priv->surfaces[i].lower = s->lower;
  1609. radeon_apply_surface_regs(s->surface_index, dev_priv);
  1610. return virt_surface_index;
  1611. }
  1612. /* extend after */
  1613. if ((dev_priv->surfaces[i].refcount == 1) &&
  1614. (new->flags == dev_priv->surfaces[i].flags) &&
  1615. (new_lower == dev_priv->surfaces[i].upper + 1)) {
  1616. s = &(dev_priv->virt_surfaces[virt_surface_index]);
  1617. s->surface_index = i;
  1618. s->lower = new_lower;
  1619. s->upper = new_upper;
  1620. s->flags = new->flags;
  1621. s->filp = filp;
  1622. dev_priv->surfaces[i].refcount++;
  1623. dev_priv->surfaces[i].upper = s->upper;
  1624. radeon_apply_surface_regs(s->surface_index, dev_priv);
  1625. return virt_surface_index;
  1626. }
  1627. }
  1628. /* okay, we need a new one */
  1629. for (i = 0; i < RADEON_MAX_SURFACES; i++) {
  1630. if (dev_priv->surfaces[i].refcount == 0) {
  1631. s = &(dev_priv->virt_surfaces[virt_surface_index]);
  1632. s->surface_index = i;
  1633. s->lower = new_lower;
  1634. s->upper = new_upper;
  1635. s->flags = new->flags;
  1636. s->filp = filp;
  1637. dev_priv->surfaces[i].refcount = 1;
  1638. dev_priv->surfaces[i].lower = s->lower;
  1639. dev_priv->surfaces[i].upper = s->upper;
  1640. dev_priv->surfaces[i].flags = s->flags;
  1641. radeon_apply_surface_regs(s->surface_index, dev_priv);
  1642. return virt_surface_index;
  1643. }
  1644. }
  1645. /* we didn't find anything */
  1646. return -1;
  1647. }
  1648. static int free_surface(DRMFILE filp, drm_radeon_private_t *dev_priv, int lower)
  1649. {
  1650. struct radeon_virt_surface *s;
  1651. int i;
  1652. /* find the virtual surface */
  1653. for(i = 0; i < 2*RADEON_MAX_SURFACES; i++) {
  1654. s = &(dev_priv->virt_surfaces[i]);
  1655. if (s->filp) {
  1656. if ((lower == s->lower) && (filp == s->filp)) {
  1657. if (dev_priv->surfaces[s->surface_index].lower == s->lower)
  1658. dev_priv->surfaces[s->surface_index].lower = s->upper;
  1659. if (dev_priv->surfaces[s->surface_index].upper == s->upper)
  1660. dev_priv->surfaces[s->surface_index].upper = s->lower;
  1661. dev_priv->surfaces[s->surface_index].refcount--;
  1662. if (dev_priv->surfaces[s->surface_index].refcount == 0)
  1663. dev_priv->surfaces[s->surface_index].flags = 0;
  1664. s->filp = NULL;
  1665. radeon_apply_surface_regs(s->surface_index, dev_priv);
  1666. return 0;
  1667. }
  1668. }
  1669. }
  1670. return 1;
  1671. }
  1672. static void radeon_surfaces_release(DRMFILE filp, drm_radeon_private_t *dev_priv)
  1673. {
  1674. int i;
  1675. for( i = 0; i < 2*RADEON_MAX_SURFACES; i++)
  1676. {
  1677. if (dev_priv->virt_surfaces[i].filp == filp)
  1678. free_surface(filp, dev_priv, dev_priv->virt_surfaces[i].lower);
  1679. }
  1680. }
  1681. /* ================================================================
  1682. * IOCTL functions
  1683. */
  1684. static int radeon_surface_alloc(DRM_IOCTL_ARGS)
  1685. {
  1686. DRM_DEVICE;
  1687. drm_radeon_private_t *dev_priv = dev->dev_private;
  1688. drm_radeon_surface_alloc_t alloc;
  1689. if (!dev_priv) {
  1690. DRM_ERROR( "%s called with no initialization\n", __FUNCTION__ );
  1691. return DRM_ERR(EINVAL);
  1692. }
  1693. DRM_COPY_FROM_USER_IOCTL(alloc, (drm_radeon_surface_alloc_t __user *)data,
  1694. sizeof(alloc));
  1695. if (alloc_surface(&alloc, dev_priv, filp) == -1)
  1696. return DRM_ERR(EINVAL);
  1697. else
  1698. return 0;
  1699. }
  1700. static int radeon_surface_free(DRM_IOCTL_ARGS)
  1701. {
  1702. DRM_DEVICE;
  1703. drm_radeon_private_t *dev_priv = dev->dev_private;
  1704. drm_radeon_surface_free_t memfree;
  1705. if (!dev_priv) {
  1706. DRM_ERROR( "%s called with no initialization\n", __FUNCTION__ );
  1707. return DRM_ERR(EINVAL);
  1708. }
  1709. DRM_COPY_FROM_USER_IOCTL(memfree, (drm_radeon_mem_free_t __user *)data,
  1710. sizeof(memfree) );
  1711. if (free_surface(filp, dev_priv, memfree.address))
  1712. return DRM_ERR(EINVAL);
  1713. else
  1714. return 0;
  1715. }
  1716. static int radeon_cp_clear( DRM_IOCTL_ARGS )
  1717. {
  1718. DRM_DEVICE;
  1719. drm_radeon_private_t *dev_priv = dev->dev_private;
  1720. drm_radeon_sarea_t *sarea_priv = dev_priv->sarea_priv;
  1721. drm_radeon_clear_t clear;
  1722. drm_radeon_clear_rect_t depth_boxes[RADEON_NR_SAREA_CLIPRECTS];
  1723. DRM_DEBUG( "\n" );
  1724. LOCK_TEST_WITH_RETURN( dev, filp );
  1725. DRM_COPY_FROM_USER_IOCTL( clear, (drm_radeon_clear_t __user *)data,
  1726. sizeof(clear) );
  1727. RING_SPACE_TEST_WITH_RETURN( dev_priv );
  1728. if ( sarea_priv->nbox > RADEON_NR_SAREA_CLIPRECTS )
  1729. sarea_priv->nbox = RADEON_NR_SAREA_CLIPRECTS;
  1730. if ( DRM_COPY_FROM_USER( &depth_boxes, clear.depth_boxes,
  1731. sarea_priv->nbox * sizeof(depth_boxes[0]) ) )
  1732. return DRM_ERR(EFAULT);
  1733. radeon_cp_dispatch_clear( dev, &clear, depth_boxes );
  1734. COMMIT_RING();
  1735. return 0;
  1736. }
  1737. /* Not sure why this isn't set all the time:
  1738. */
  1739. static int radeon_do_init_pageflip( drm_device_t *dev )
  1740. {
  1741. drm_radeon_private_t *dev_priv = dev->dev_private;
  1742. RING_LOCALS;
  1743. DRM_DEBUG( "\n" );
  1744. BEGIN_RING( 6 );
  1745. RADEON_WAIT_UNTIL_3D_IDLE();
  1746. OUT_RING( CP_PACKET0( RADEON_CRTC_OFFSET_CNTL, 0 ) );
  1747. OUT_RING( RADEON_READ( RADEON_CRTC_OFFSET_CNTL ) | RADEON_CRTC_OFFSET_FLIP_CNTL );
  1748. OUT_RING( CP_PACKET0( RADEON_CRTC2_OFFSET_CNTL, 0 ) );
  1749. OUT_RING( RADEON_READ( RADEON_CRTC2_OFFSET_CNTL ) | RADEON_CRTC_OFFSET_FLIP_CNTL );
  1750. ADVANCE_RING();
  1751. dev_priv->page_flipping = 1;
  1752. dev_priv->current_page = 0;
  1753. dev_priv->sarea_priv->pfCurrentPage = dev_priv->current_page;
  1754. return 0;
  1755. }
  1756. /* Called whenever a client dies, from drm_release.
  1757. * NOTE: Lock isn't necessarily held when this is called!
  1758. */
  1759. static int radeon_do_cleanup_pageflip( drm_device_t *dev )
  1760. {
  1761. drm_radeon_private_t *dev_priv = dev->dev_private;
  1762. DRM_DEBUG( "\n" );
  1763. if (dev_priv->current_page != 0)
  1764. radeon_cp_dispatch_flip( dev );
  1765. dev_priv->page_flipping = 0;
  1766. return 0;
  1767. }
  1768. /* Swapping and flipping are different operations, need different ioctls.
  1769. * They can & should be intermixed to support multiple 3d windows.
  1770. */
  1771. static int radeon_cp_flip( DRM_IOCTL_ARGS )
  1772. {
  1773. DRM_DEVICE;
  1774. drm_radeon_private_t *dev_priv = dev->dev_private;
  1775. DRM_DEBUG( "\n" );
  1776. LOCK_TEST_WITH_RETURN( dev, filp );
  1777. RING_SPACE_TEST_WITH_RETURN( dev_priv );
  1778. if (!dev_priv->page_flipping)
  1779. radeon_do_init_pageflip( dev );
  1780. radeon_cp_dispatch_flip( dev );
  1781. COMMIT_RING();
  1782. return 0;
  1783. }
  1784. static int radeon_cp_swap( DRM_IOCTL_ARGS )
  1785. {
  1786. DRM_DEVICE;
  1787. drm_radeon_private_t *dev_priv = dev->dev_private;
  1788. drm_radeon_sarea_t *sarea_priv = dev_priv->sarea_priv;
  1789. DRM_DEBUG( "\n" );
  1790. LOCK_TEST_WITH_RETURN( dev, filp );
  1791. RING_SPACE_TEST_WITH_RETURN( dev_priv );
  1792. if ( sarea_priv->nbox > RADEON_NR_SAREA_CLIPRECTS )
  1793. sarea_priv->nbox = RADEON_NR_SAREA_CLIPRECTS;
  1794. radeon_cp_dispatch_swap( dev );
  1795. dev_priv->sarea_priv->ctx_owner = 0;
  1796. COMMIT_RING();
  1797. return 0;
  1798. }
  1799. static int radeon_cp_vertex( DRM_IOCTL_ARGS )
  1800. {
  1801. DRM_DEVICE;
  1802. drm_radeon_private_t *dev_priv = dev->dev_private;
  1803. drm_file_t *filp_priv;
  1804. drm_radeon_sarea_t *sarea_priv = dev_priv->sarea_priv;
  1805. drm_device_dma_t *dma = dev->dma;
  1806. drm_buf_t *buf;
  1807. drm_radeon_vertex_t vertex;
  1808. drm_radeon_tcl_prim_t prim;
  1809. LOCK_TEST_WITH_RETURN( dev, filp );
  1810. DRM_GET_PRIV_WITH_RETURN( filp_priv, filp );
  1811. DRM_COPY_FROM_USER_IOCTL( vertex, (drm_radeon_vertex_t __user *)data,
  1812. sizeof(vertex) );
  1813. DRM_DEBUG( "pid=%d index=%d count=%d discard=%d\n",
  1814. DRM_CURRENTPID,
  1815. vertex.idx, vertex.count, vertex.discard );
  1816. if ( vertex.idx < 0 || vertex.idx >= dma->buf_count ) {
  1817. DRM_ERROR( "buffer index %d (of %d max)\n",
  1818. vertex.idx, dma->buf_count - 1 );
  1819. return DRM_ERR(EINVAL);
  1820. }
  1821. if ( vertex.prim < 0 ||
  1822. vertex.prim > RADEON_PRIM_TYPE_3VRT_LINE_LIST ) {
  1823. DRM_ERROR( "buffer prim %d\n", vertex.prim );
  1824. return DRM_ERR(EINVAL);
  1825. }
  1826. RING_SPACE_TEST_WITH_RETURN( dev_priv );
  1827. VB_AGE_TEST_WITH_RETURN( dev_priv );
  1828. buf = dma->buflist[vertex.idx];
  1829. if ( buf->filp != filp ) {
  1830. DRM_ERROR( "process %d using buffer owned by %p\n",
  1831. DRM_CURRENTPID, buf->filp );
  1832. return DRM_ERR(EINVAL);
  1833. }
  1834. if ( buf->pending ) {
  1835. DRM_ERROR( "sending pending buffer %d\n", vertex.idx );
  1836. return DRM_ERR(EINVAL);
  1837. }
  1838. /* Build up a prim_t record:
  1839. */
  1840. if (vertex.count) {
  1841. buf->used = vertex.count; /* not used? */
  1842. if ( sarea_priv->dirty & ~RADEON_UPLOAD_CLIPRECTS ) {
  1843. if ( radeon_emit_state( dev_priv, filp_priv,
  1844. &sarea_priv->context_state,
  1845. sarea_priv->tex_state,
  1846. sarea_priv->dirty ) ) {
  1847. DRM_ERROR( "radeon_emit_state failed\n" );
  1848. return DRM_ERR( EINVAL );
  1849. }
  1850. sarea_priv->dirty &= ~(RADEON_UPLOAD_TEX0IMAGES |
  1851. RADEON_UPLOAD_TEX1IMAGES |
  1852. RADEON_UPLOAD_TEX2IMAGES |
  1853. RADEON_REQUIRE_QUIESCENCE);
  1854. }
  1855. prim.start = 0;
  1856. prim.finish = vertex.count; /* unused */
  1857. prim.prim = vertex.prim;
  1858. prim.numverts = vertex.count;
  1859. prim.vc_format = dev_priv->sarea_priv->vc_format;
  1860. radeon_cp_dispatch_vertex( dev, buf, &prim );
  1861. }
  1862. if (vertex.discard) {
  1863. radeon_cp_discard_buffer( dev, buf );
  1864. }
  1865. COMMIT_RING();
  1866. return 0;
  1867. }
  1868. static int radeon_cp_indices( DRM_IOCTL_ARGS )
  1869. {
  1870. DRM_DEVICE;
  1871. drm_radeon_private_t *dev_priv = dev->dev_private;
  1872. drm_file_t *filp_priv;
  1873. drm_radeon_sarea_t *sarea_priv = dev_priv->sarea_priv;
  1874. drm_device_dma_t *dma = dev->dma;
  1875. drm_buf_t *buf;
  1876. drm_radeon_indices_t elts;
  1877. drm_radeon_tcl_prim_t prim;
  1878. int count;
  1879. LOCK_TEST_WITH_RETURN( dev, filp );
  1880. if ( !dev_priv ) {
  1881. DRM_ERROR( "%s called with no initialization\n", __FUNCTION__ );
  1882. return DRM_ERR(EINVAL);
  1883. }
  1884. DRM_GET_PRIV_WITH_RETURN( filp_priv, filp );
  1885. DRM_COPY_FROM_USER_IOCTL( elts, (drm_radeon_indices_t __user *)data,
  1886. sizeof(elts) );
  1887. DRM_DEBUG( "pid=%d index=%d start=%d end=%d discard=%d\n",
  1888. DRM_CURRENTPID,
  1889. elts.idx, elts.start, elts.end, elts.discard );
  1890. if ( elts.idx < 0 || elts.idx >= dma->buf_count ) {
  1891. DRM_ERROR( "buffer index %d (of %d max)\n",
  1892. elts.idx, dma->buf_count - 1 );
  1893. return DRM_ERR(EINVAL);
  1894. }
  1895. if ( elts.prim < 0 ||
  1896. elts.prim > RADEON_PRIM_TYPE_3VRT_LINE_LIST ) {
  1897. DRM_ERROR( "buffer prim %d\n", elts.prim );
  1898. return DRM_ERR(EINVAL);
  1899. }
  1900. RING_SPACE_TEST_WITH_RETURN( dev_priv );
  1901. VB_AGE_TEST_WITH_RETURN( dev_priv );
  1902. buf = dma->buflist[elts.idx];
  1903. if ( buf->filp != filp ) {
  1904. DRM_ERROR( "process %d using buffer owned by %p\n",
  1905. DRM_CURRENTPID, buf->filp );
  1906. return DRM_ERR(EINVAL);
  1907. }
  1908. if ( buf->pending ) {
  1909. DRM_ERROR( "sending pending buffer %d\n", elts.idx );
  1910. return DRM_ERR(EINVAL);
  1911. }
  1912. count = (elts.end - elts.start) / sizeof(u16);
  1913. elts.start -= RADEON_INDEX_PRIM_OFFSET;
  1914. if ( elts.start & 0x7 ) {
  1915. DRM_ERROR( "misaligned buffer 0x%x\n", elts.start );
  1916. return DRM_ERR(EINVAL);
  1917. }
  1918. if ( elts.start < buf->used ) {
  1919. DRM_ERROR( "no header 0x%x - 0x%x\n", elts.start, buf->used );
  1920. return DRM_ERR(EINVAL);
  1921. }
  1922. buf->used = elts.end;
  1923. if ( sarea_priv->dirty & ~RADEON_UPLOAD_CLIPRECTS ) {
  1924. if ( radeon_emit_state( dev_priv, filp_priv,
  1925. &sarea_priv->context_state,
  1926. sarea_priv->tex_state,
  1927. sarea_priv->dirty ) ) {
  1928. DRM_ERROR( "radeon_emit_state failed\n" );
  1929. return DRM_ERR( EINVAL );
  1930. }
  1931. sarea_priv->dirty &= ~(RADEON_UPLOAD_TEX0IMAGES |
  1932. RADEON_UPLOAD_TEX1IMAGES |
  1933. RADEON_UPLOAD_TEX2IMAGES |
  1934. RADEON_REQUIRE_QUIESCENCE);
  1935. }
  1936. /* Build up a prim_t record:
  1937. */
  1938. prim.start = elts.start;
  1939. prim.finish = elts.end;
  1940. prim.prim = elts.prim;
  1941. prim.offset = 0; /* offset from start of dma buffers */
  1942. prim.numverts = RADEON_MAX_VB_VERTS; /* duh */
  1943. prim.vc_format = dev_priv->sarea_priv->vc_format;
  1944. radeon_cp_dispatch_indices( dev, buf, &prim );
  1945. if (elts.discard) {
  1946. radeon_cp_discard_buffer( dev, buf );
  1947. }
  1948. COMMIT_RING();
  1949. return 0;
  1950. }
  1951. static int radeon_cp_texture( DRM_IOCTL_ARGS )
  1952. {
  1953. DRM_DEVICE;
  1954. drm_radeon_private_t *dev_priv = dev->dev_private;
  1955. drm_radeon_texture_t tex;
  1956. drm_radeon_tex_image_t image;
  1957. int ret;
  1958. LOCK_TEST_WITH_RETURN( dev, filp );
  1959. DRM_COPY_FROM_USER_IOCTL( tex, (drm_radeon_texture_t __user *)data, sizeof(tex) );
  1960. if ( tex.image == NULL ) {
  1961. DRM_ERROR( "null texture image!\n" );
  1962. return DRM_ERR(EINVAL);
  1963. }
  1964. if ( DRM_COPY_FROM_USER( &image,
  1965. (drm_radeon_tex_image_t __user *)tex.image,
  1966. sizeof(image) ) )
  1967. return DRM_ERR(EFAULT);
  1968. RING_SPACE_TEST_WITH_RETURN( dev_priv );
  1969. VB_AGE_TEST_WITH_RETURN( dev_priv );
  1970. ret = radeon_cp_dispatch_texture( filp, dev, &tex, &image );
  1971. COMMIT_RING();
  1972. return ret;
  1973. }
  1974. static int radeon_cp_stipple( DRM_IOCTL_ARGS )
  1975. {
  1976. DRM_DEVICE;
  1977. drm_radeon_private_t *dev_priv = dev->dev_private;
  1978. drm_radeon_stipple_t stipple;
  1979. u32 mask[32];
  1980. LOCK_TEST_WITH_RETURN( dev, filp );
  1981. DRM_COPY_FROM_USER_IOCTL( stipple, (drm_radeon_stipple_t __user *)data,
  1982. sizeof(stipple) );
  1983. if ( DRM_COPY_FROM_USER( &mask, stipple.mask, 32 * sizeof(u32) ) )
  1984. return DRM_ERR(EFAULT);
  1985. RING_SPACE_TEST_WITH_RETURN( dev_priv );
  1986. radeon_cp_dispatch_stipple( dev, mask );
  1987. COMMIT_RING();
  1988. return 0;
  1989. }
  1990. static int radeon_cp_indirect( DRM_IOCTL_ARGS )
  1991. {
  1992. DRM_DEVICE;
  1993. drm_radeon_private_t *dev_priv = dev->dev_private;
  1994. drm_device_dma_t *dma = dev->dma;
  1995. drm_buf_t *buf;
  1996. drm_radeon_indirect_t indirect;
  1997. RING_LOCALS;
  1998. LOCK_TEST_WITH_RETURN( dev, filp );
  1999. if ( !dev_priv ) {
  2000. DRM_ERROR( "%s called with no initialization\n", __FUNCTION__ );
  2001. return DRM_ERR(EINVAL);
  2002. }
  2003. DRM_COPY_FROM_USER_IOCTL( indirect, (drm_radeon_indirect_t __user *)data,
  2004. sizeof(indirect) );
  2005. DRM_DEBUG( "indirect: idx=%d s=%d e=%d d=%d\n",
  2006. indirect.idx, indirect.start,
  2007. indirect.end, indirect.discard );
  2008. if ( indirect.idx < 0 || indirect.idx >= dma->buf_count ) {
  2009. DRM_ERROR( "buffer index %d (of %d max)\n",
  2010. indirect.idx, dma->buf_count - 1 );
  2011. return DRM_ERR(EINVAL);
  2012. }
  2013. buf = dma->buflist[indirect.idx];
  2014. if ( buf->filp != filp ) {
  2015. DRM_ERROR( "process %d using buffer owned by %p\n",
  2016. DRM_CURRENTPID, buf->filp );
  2017. return DRM_ERR(EINVAL);
  2018. }
  2019. if ( buf->pending ) {
  2020. DRM_ERROR( "sending pending buffer %d\n", indirect.idx );
  2021. return DRM_ERR(EINVAL);
  2022. }
  2023. if ( indirect.start < buf->used ) {
  2024. DRM_ERROR( "reusing indirect: start=0x%x actual=0x%x\n",
  2025. indirect.start, buf->used );
  2026. return DRM_ERR(EINVAL);
  2027. }
  2028. RING_SPACE_TEST_WITH_RETURN( dev_priv );
  2029. VB_AGE_TEST_WITH_RETURN( dev_priv );
  2030. buf->used = indirect.end;
  2031. /* Wait for the 3D stream to idle before the indirect buffer
  2032. * containing 2D acceleration commands is processed.
  2033. */
  2034. BEGIN_RING( 2 );
  2035. RADEON_WAIT_UNTIL_3D_IDLE();
  2036. ADVANCE_RING();
  2037. /* Dispatch the indirect buffer full of commands from the
  2038. * X server. This is insecure and is thus only available to
  2039. * privileged clients.
  2040. */
  2041. radeon_cp_dispatch_indirect( dev, buf, indirect.start, indirect.end );
  2042. if (indirect.discard) {
  2043. radeon_cp_discard_buffer( dev, buf );
  2044. }
  2045. COMMIT_RING();
  2046. return 0;
  2047. }
  2048. static int radeon_cp_vertex2( DRM_IOCTL_ARGS )
  2049. {
  2050. DRM_DEVICE;
  2051. drm_radeon_private_t *dev_priv = dev->dev_private;
  2052. drm_file_t *filp_priv;
  2053. drm_radeon_sarea_t *sarea_priv = dev_priv->sarea_priv;
  2054. drm_device_dma_t *dma = dev->dma;
  2055. drm_buf_t *buf;
  2056. drm_radeon_vertex2_t vertex;
  2057. int i;
  2058. unsigned char laststate;
  2059. LOCK_TEST_WITH_RETURN( dev, filp );
  2060. if ( !dev_priv ) {
  2061. DRM_ERROR( "%s called with no initialization\n", __FUNCTION__ );
  2062. return DRM_ERR(EINVAL);
  2063. }
  2064. DRM_GET_PRIV_WITH_RETURN( filp_priv, filp );
  2065. DRM_COPY_FROM_USER_IOCTL( vertex, (drm_radeon_vertex2_t __user *)data,
  2066. sizeof(vertex) );
  2067. DRM_DEBUG( "pid=%d index=%d discard=%d\n",
  2068. DRM_CURRENTPID,
  2069. vertex.idx, vertex.discard );
  2070. if ( vertex.idx < 0 || vertex.idx >= dma->buf_count ) {
  2071. DRM_ERROR( "buffer index %d (of %d max)\n",
  2072. vertex.idx, dma->buf_count - 1 );
  2073. return DRM_ERR(EINVAL);
  2074. }
  2075. RING_SPACE_TEST_WITH_RETURN( dev_priv );
  2076. VB_AGE_TEST_WITH_RETURN( dev_priv );
  2077. buf = dma->buflist[vertex.idx];
  2078. if ( buf->filp != filp ) {
  2079. DRM_ERROR( "process %d using buffer owned by %p\n",
  2080. DRM_CURRENTPID, buf->filp );
  2081. return DRM_ERR(EINVAL);
  2082. }
  2083. if ( buf->pending ) {
  2084. DRM_ERROR( "sending pending buffer %d\n", vertex.idx );
  2085. return DRM_ERR(EINVAL);
  2086. }
  2087. if (sarea_priv->nbox > RADEON_NR_SAREA_CLIPRECTS)
  2088. return DRM_ERR(EINVAL);
  2089. for (laststate = 0xff, i = 0 ; i < vertex.nr_prims ; i++) {
  2090. drm_radeon_prim_t prim;
  2091. drm_radeon_tcl_prim_t tclprim;
  2092. if ( DRM_COPY_FROM_USER( &prim, &vertex.prim[i], sizeof(prim) ) )
  2093. return DRM_ERR(EFAULT);
  2094. if ( prim.stateidx != laststate ) {
  2095. drm_radeon_state_t state;
  2096. if ( DRM_COPY_FROM_USER( &state,
  2097. &vertex.state[prim.stateidx],
  2098. sizeof(state) ) )
  2099. return DRM_ERR(EFAULT);
  2100. if ( radeon_emit_state2( dev_priv, filp_priv, &state ) ) {
  2101. DRM_ERROR( "radeon_emit_state2 failed\n" );
  2102. return DRM_ERR( EINVAL );
  2103. }
  2104. laststate = prim.stateidx;
  2105. }
  2106. tclprim.start = prim.start;
  2107. tclprim.finish = prim.finish;
  2108. tclprim.prim = prim.prim;
  2109. tclprim.vc_format = prim.vc_format;
  2110. if ( prim.prim & RADEON_PRIM_WALK_IND ) {
  2111. tclprim.offset = prim.numverts * 64;
  2112. tclprim.numverts = RADEON_MAX_VB_VERTS; /* duh */
  2113. radeon_cp_dispatch_indices( dev, buf, &tclprim );
  2114. } else {
  2115. tclprim.numverts = prim.numverts;
  2116. tclprim.offset = 0; /* not used */
  2117. radeon_cp_dispatch_vertex( dev, buf, &tclprim );
  2118. }
  2119. if (sarea_priv->nbox == 1)
  2120. sarea_priv->nbox = 0;
  2121. }
  2122. if ( vertex.discard ) {
  2123. radeon_cp_discard_buffer( dev, buf );
  2124. }
  2125. COMMIT_RING();
  2126. return 0;
  2127. }
  2128. static int radeon_emit_packets(
  2129. drm_radeon_private_t *dev_priv,
  2130. drm_file_t *filp_priv,
  2131. drm_radeon_cmd_header_t header,
  2132. drm_radeon_cmd_buffer_t *cmdbuf )
  2133. {
  2134. int id = (int)header.packet.packet_id;
  2135. int sz, reg;
  2136. int *data = (int *)cmdbuf->buf;
  2137. RING_LOCALS;
  2138. if (id >= RADEON_MAX_STATE_PACKETS)
  2139. return DRM_ERR(EINVAL);
  2140. sz = packet[id].len;
  2141. reg = packet[id].start;
  2142. if (sz * sizeof(int) > cmdbuf->bufsz) {
  2143. DRM_ERROR( "Packet size provided larger than data provided\n" );
  2144. return DRM_ERR(EINVAL);
  2145. }
  2146. if ( radeon_check_and_fixup_packets( dev_priv, filp_priv, id, data ) ) {
  2147. DRM_ERROR( "Packet verification failed\n" );
  2148. return DRM_ERR( EINVAL );
  2149. }
  2150. BEGIN_RING(sz+1);
  2151. OUT_RING( CP_PACKET0( reg, (sz-1) ) );
  2152. OUT_RING_TABLE( data, sz );
  2153. ADVANCE_RING();
  2154. cmdbuf->buf += sz * sizeof(int);
  2155. cmdbuf->bufsz -= sz * sizeof(int);
  2156. return 0;
  2157. }
  2158. static __inline__ int radeon_emit_scalars(
  2159. drm_radeon_private_t *dev_priv,
  2160. drm_radeon_cmd_header_t header,
  2161. drm_radeon_cmd_buffer_t *cmdbuf )
  2162. {
  2163. int sz = header.scalars.count;
  2164. int start = header.scalars.offset;
  2165. int stride = header.scalars.stride;
  2166. RING_LOCALS;
  2167. BEGIN_RING( 3+sz );
  2168. OUT_RING( CP_PACKET0( RADEON_SE_TCL_SCALAR_INDX_REG, 0 ) );
  2169. OUT_RING( start | (stride << RADEON_SCAL_INDX_DWORD_STRIDE_SHIFT));
  2170. OUT_RING( CP_PACKET0_TABLE( RADEON_SE_TCL_SCALAR_DATA_REG, sz-1 ) );
  2171. OUT_RING_TABLE( cmdbuf->buf, sz );
  2172. ADVANCE_RING();
  2173. cmdbuf->buf += sz * sizeof(int);
  2174. cmdbuf->bufsz -= sz * sizeof(int);
  2175. return 0;
  2176. }
  2177. /* God this is ugly
  2178. */
  2179. static __inline__ int radeon_emit_scalars2(
  2180. drm_radeon_private_t *dev_priv,
  2181. drm_radeon_cmd_header_t header,
  2182. drm_radeon_cmd_buffer_t *cmdbuf )
  2183. {
  2184. int sz = header.scalars.count;
  2185. int start = ((unsigned int)header.scalars.offset) + 0x100;
  2186. int stride = header.scalars.stride;
  2187. RING_LOCALS;
  2188. BEGIN_RING( 3+sz );
  2189. OUT_RING( CP_PACKET0( RADEON_SE_TCL_SCALAR_INDX_REG, 0 ) );
  2190. OUT_RING( start | (stride << RADEON_SCAL_INDX_DWORD_STRIDE_SHIFT));
  2191. OUT_RING( CP_PACKET0_TABLE( RADEON_SE_TCL_SCALAR_DATA_REG, sz-1 ) );
  2192. OUT_RING_TABLE( cmdbuf->buf, sz );
  2193. ADVANCE_RING();
  2194. cmdbuf->buf += sz * sizeof(int);
  2195. cmdbuf->bufsz -= sz * sizeof(int);
  2196. return 0;
  2197. }
  2198. static __inline__ int radeon_emit_vectors(
  2199. drm_radeon_private_t *dev_priv,
  2200. drm_radeon_cmd_header_t header,
  2201. drm_radeon_cmd_buffer_t *cmdbuf )
  2202. {
  2203. int sz = header.vectors.count;
  2204. int start = header.vectors.offset;
  2205. int stride = header.vectors.stride;
  2206. RING_LOCALS;
  2207. BEGIN_RING( 3+sz );
  2208. OUT_RING( CP_PACKET0( RADEON_SE_TCL_VECTOR_INDX_REG, 0 ) );
  2209. OUT_RING( start | (stride << RADEON_VEC_INDX_OCTWORD_STRIDE_SHIFT));
  2210. OUT_RING( CP_PACKET0_TABLE( RADEON_SE_TCL_VECTOR_DATA_REG, (sz-1) ) );
  2211. OUT_RING_TABLE( cmdbuf->buf, sz );
  2212. ADVANCE_RING();
  2213. cmdbuf->buf += sz * sizeof(int);
  2214. cmdbuf->bufsz -= sz * sizeof(int);
  2215. return 0;
  2216. }
  2217. static int radeon_emit_packet3( drm_device_t *dev,
  2218. drm_file_t *filp_priv,
  2219. drm_radeon_cmd_buffer_t *cmdbuf )
  2220. {
  2221. drm_radeon_private_t *dev_priv = dev->dev_private;
  2222. unsigned int cmdsz;
  2223. int ret;
  2224. RING_LOCALS;
  2225. DRM_DEBUG("\n");
  2226. if ( ( ret = radeon_check_and_fixup_packet3( dev_priv, filp_priv,
  2227. cmdbuf, &cmdsz ) ) ) {
  2228. DRM_ERROR( "Packet verification failed\n" );
  2229. return ret;
  2230. }
  2231. BEGIN_RING( cmdsz );
  2232. OUT_RING_TABLE( cmdbuf->buf, cmdsz );
  2233. ADVANCE_RING();
  2234. cmdbuf->buf += cmdsz * 4;
  2235. cmdbuf->bufsz -= cmdsz * 4;
  2236. return 0;
  2237. }
  2238. static int radeon_emit_packet3_cliprect( drm_device_t *dev,
  2239. drm_file_t *filp_priv,
  2240. drm_radeon_cmd_buffer_t *cmdbuf,
  2241. int orig_nbox )
  2242. {
  2243. drm_radeon_private_t *dev_priv = dev->dev_private;
  2244. drm_clip_rect_t box;
  2245. unsigned int cmdsz;
  2246. int ret;
  2247. drm_clip_rect_t __user *boxes = cmdbuf->boxes;
  2248. int i = 0;
  2249. RING_LOCALS;
  2250. DRM_DEBUG("\n");
  2251. if ( ( ret = radeon_check_and_fixup_packet3( dev_priv, filp_priv,
  2252. cmdbuf, &cmdsz ) ) ) {
  2253. DRM_ERROR( "Packet verification failed\n" );
  2254. return ret;
  2255. }
  2256. if (!orig_nbox)
  2257. goto out;
  2258. do {
  2259. if ( i < cmdbuf->nbox ) {
  2260. if (DRM_COPY_FROM_USER( &box, &boxes[i], sizeof(box) ))
  2261. return DRM_ERR(EFAULT);
  2262. /* FIXME The second and subsequent times round
  2263. * this loop, send a WAIT_UNTIL_3D_IDLE before
  2264. * calling emit_clip_rect(). This fixes a
  2265. * lockup on fast machines when sending
  2266. * several cliprects with a cmdbuf, as when
  2267. * waving a 2D window over a 3D
  2268. * window. Something in the commands from user
  2269. * space seems to hang the card when they're
  2270. * sent several times in a row. That would be
  2271. * the correct place to fix it but this works
  2272. * around it until I can figure that out - Tim
  2273. * Smith */
  2274. if ( i ) {
  2275. BEGIN_RING( 2 );
  2276. RADEON_WAIT_UNTIL_3D_IDLE();
  2277. ADVANCE_RING();
  2278. }
  2279. radeon_emit_clip_rect( dev_priv, &box );
  2280. }
  2281. BEGIN_RING( cmdsz );
  2282. OUT_RING_TABLE( cmdbuf->buf, cmdsz );
  2283. ADVANCE_RING();
  2284. } while ( ++i < cmdbuf->nbox );
  2285. if (cmdbuf->nbox == 1)
  2286. cmdbuf->nbox = 0;
  2287. out:
  2288. cmdbuf->buf += cmdsz * 4;
  2289. cmdbuf->bufsz -= cmdsz * 4;
  2290. return 0;
  2291. }
  2292. static int radeon_emit_wait( drm_device_t *dev, int flags )
  2293. {
  2294. drm_radeon_private_t *dev_priv = dev->dev_private;
  2295. RING_LOCALS;
  2296. DRM_DEBUG("%s: %x\n", __FUNCTION__, flags);
  2297. switch (flags) {
  2298. case RADEON_WAIT_2D:
  2299. BEGIN_RING( 2 );
  2300. RADEON_WAIT_UNTIL_2D_IDLE();
  2301. ADVANCE_RING();
  2302. break;
  2303. case RADEON_WAIT_3D:
  2304. BEGIN_RING( 2 );
  2305. RADEON_WAIT_UNTIL_3D_IDLE();
  2306. ADVANCE_RING();
  2307. break;
  2308. case RADEON_WAIT_2D|RADEON_WAIT_3D:
  2309. BEGIN_RING( 2 );
  2310. RADEON_WAIT_UNTIL_IDLE();
  2311. ADVANCE_RING();
  2312. break;
  2313. default:
  2314. return DRM_ERR(EINVAL);
  2315. }
  2316. return 0;
  2317. }
  2318. static int radeon_cp_cmdbuf( DRM_IOCTL_ARGS )
  2319. {
  2320. DRM_DEVICE;
  2321. drm_radeon_private_t *dev_priv = dev->dev_private;
  2322. drm_file_t *filp_priv;
  2323. drm_device_dma_t *dma = dev->dma;
  2324. drm_buf_t *buf = NULL;
  2325. int idx;
  2326. drm_radeon_cmd_buffer_t cmdbuf;
  2327. drm_radeon_cmd_header_t header;
  2328. int orig_nbox, orig_bufsz;
  2329. char *kbuf=NULL;
  2330. LOCK_TEST_WITH_RETURN( dev, filp );
  2331. if ( !dev_priv ) {
  2332. DRM_ERROR( "%s called with no initialization\n", __FUNCTION__ );
  2333. return DRM_ERR(EINVAL);
  2334. }
  2335. DRM_GET_PRIV_WITH_RETURN( filp_priv, filp );
  2336. DRM_COPY_FROM_USER_IOCTL( cmdbuf, (drm_radeon_cmd_buffer_t __user *)data,
  2337. sizeof(cmdbuf) );
  2338. RING_SPACE_TEST_WITH_RETURN( dev_priv );
  2339. VB_AGE_TEST_WITH_RETURN( dev_priv );
  2340. if (cmdbuf.bufsz > 64*1024 || cmdbuf.bufsz<0) {
  2341. return DRM_ERR(EINVAL);
  2342. }
  2343. /* Allocate an in-kernel area and copy in the cmdbuf. Do this to avoid
  2344. * races between checking values and using those values in other code,
  2345. * and simply to avoid a lot of function calls to copy in data.
  2346. */
  2347. orig_bufsz = cmdbuf.bufsz;
  2348. if (orig_bufsz != 0) {
  2349. kbuf = drm_alloc(cmdbuf.bufsz, DRM_MEM_DRIVER);
  2350. if (kbuf == NULL)
  2351. return DRM_ERR(ENOMEM);
  2352. if (DRM_COPY_FROM_USER(kbuf, cmdbuf.buf, cmdbuf.bufsz)) {
  2353. drm_free(kbuf, orig_bufsz, DRM_MEM_DRIVER);
  2354. return DRM_ERR(EFAULT);
  2355. }
  2356. cmdbuf.buf = kbuf;
  2357. }
  2358. orig_nbox = cmdbuf.nbox;
  2359. while ( cmdbuf.bufsz >= sizeof(header) ) {
  2360. header.i = *(int *)cmdbuf.buf;
  2361. cmdbuf.buf += sizeof(header);
  2362. cmdbuf.bufsz -= sizeof(header);
  2363. switch (header.header.cmd_type) {
  2364. case RADEON_CMD_PACKET:
  2365. DRM_DEBUG("RADEON_CMD_PACKET\n");
  2366. if (radeon_emit_packets( dev_priv, filp_priv, header, &cmdbuf )) {
  2367. DRM_ERROR("radeon_emit_packets failed\n");
  2368. goto err;
  2369. }
  2370. break;
  2371. case RADEON_CMD_SCALARS:
  2372. DRM_DEBUG("RADEON_CMD_SCALARS\n");
  2373. if (radeon_emit_scalars( dev_priv, header, &cmdbuf )) {
  2374. DRM_ERROR("radeon_emit_scalars failed\n");
  2375. goto err;
  2376. }
  2377. break;
  2378. case RADEON_CMD_VECTORS:
  2379. DRM_DEBUG("RADEON_CMD_VECTORS\n");
  2380. if (radeon_emit_vectors( dev_priv, header, &cmdbuf )) {
  2381. DRM_ERROR("radeon_emit_vectors failed\n");
  2382. goto err;
  2383. }
  2384. break;
  2385. case RADEON_CMD_DMA_DISCARD:
  2386. DRM_DEBUG("RADEON_CMD_DMA_DISCARD\n");
  2387. idx = header.dma.buf_idx;
  2388. if ( idx < 0 || idx >= dma->buf_count ) {
  2389. DRM_ERROR( "buffer index %d (of %d max)\n",
  2390. idx, dma->buf_count - 1 );
  2391. goto err;
  2392. }
  2393. buf = dma->buflist[idx];
  2394. if ( buf->filp != filp || buf->pending ) {
  2395. DRM_ERROR( "bad buffer %p %p %d\n",
  2396. buf->filp, filp, buf->pending);
  2397. goto err;
  2398. }
  2399. radeon_cp_discard_buffer( dev, buf );
  2400. break;
  2401. case RADEON_CMD_PACKET3:
  2402. DRM_DEBUG("RADEON_CMD_PACKET3\n");
  2403. if (radeon_emit_packet3( dev, filp_priv, &cmdbuf )) {
  2404. DRM_ERROR("radeon_emit_packet3 failed\n");
  2405. goto err;
  2406. }
  2407. break;
  2408. case RADEON_CMD_PACKET3_CLIP:
  2409. DRM_DEBUG("RADEON_CMD_PACKET3_CLIP\n");
  2410. if (radeon_emit_packet3_cliprect( dev, filp_priv, &cmdbuf, orig_nbox )) {
  2411. DRM_ERROR("radeon_emit_packet3_clip failed\n");
  2412. goto err;
  2413. }
  2414. break;
  2415. case RADEON_CMD_SCALARS2:
  2416. DRM_DEBUG("RADEON_CMD_SCALARS2\n");
  2417. if (radeon_emit_scalars2( dev_priv, header, &cmdbuf )) {
  2418. DRM_ERROR("radeon_emit_scalars2 failed\n");
  2419. goto err;
  2420. }
  2421. break;
  2422. case RADEON_CMD_WAIT:
  2423. DRM_DEBUG("RADEON_CMD_WAIT\n");
  2424. if (radeon_emit_wait( dev, header.wait.flags )) {
  2425. DRM_ERROR("radeon_emit_wait failed\n");
  2426. goto err;
  2427. }
  2428. break;
  2429. default:
  2430. DRM_ERROR("bad cmd_type %d at %p\n",
  2431. header.header.cmd_type,
  2432. cmdbuf.buf - sizeof(header));
  2433. goto err;
  2434. }
  2435. }
  2436. if (orig_bufsz != 0)
  2437. drm_free(kbuf, orig_bufsz, DRM_MEM_DRIVER);
  2438. DRM_DEBUG("DONE\n");
  2439. COMMIT_RING();
  2440. return 0;
  2441. err:
  2442. if (orig_bufsz != 0)
  2443. drm_free(kbuf, orig_bufsz, DRM_MEM_DRIVER);
  2444. return DRM_ERR(EINVAL);
  2445. }
  2446. static int radeon_cp_getparam( DRM_IOCTL_ARGS )
  2447. {
  2448. DRM_DEVICE;
  2449. drm_radeon_private_t *dev_priv = dev->dev_private;
  2450. drm_radeon_getparam_t param;
  2451. int value;
  2452. if ( !dev_priv ) {
  2453. DRM_ERROR( "%s called with no initialization\n", __FUNCTION__ );
  2454. return DRM_ERR(EINVAL);
  2455. }
  2456. DRM_COPY_FROM_USER_IOCTL( param, (drm_radeon_getparam_t __user *)data,
  2457. sizeof(param) );
  2458. DRM_DEBUG( "pid=%d\n", DRM_CURRENTPID );
  2459. switch( param.param ) {
  2460. case RADEON_PARAM_GART_BUFFER_OFFSET:
  2461. value = dev_priv->gart_buffers_offset;
  2462. break;
  2463. case RADEON_PARAM_LAST_FRAME:
  2464. dev_priv->stats.last_frame_reads++;
  2465. value = GET_SCRATCH( 0 );
  2466. break;
  2467. case RADEON_PARAM_LAST_DISPATCH:
  2468. value = GET_SCRATCH( 1 );
  2469. break;
  2470. case RADEON_PARAM_LAST_CLEAR:
  2471. dev_priv->stats.last_clear_reads++;
  2472. value = GET_SCRATCH( 2 );
  2473. break;
  2474. case RADEON_PARAM_IRQ_NR:
  2475. value = dev->irq;
  2476. break;
  2477. case RADEON_PARAM_GART_BASE:
  2478. value = dev_priv->gart_vm_start;
  2479. break;
  2480. case RADEON_PARAM_REGISTER_HANDLE:
  2481. value = dev_priv->mmio_offset;
  2482. break;
  2483. case RADEON_PARAM_STATUS_HANDLE:
  2484. value = dev_priv->ring_rptr_offset;
  2485. break;
  2486. #if BITS_PER_LONG == 32
  2487. /*
  2488. * This ioctl() doesn't work on 64-bit platforms because hw_lock is a
  2489. * pointer which can't fit into an int-sized variable. According to
  2490. * Michel Dänzer, the ioctl() is only used on embedded platforms, so
  2491. * not supporting it shouldn't be a problem. If the same functionality
  2492. * is needed on 64-bit platforms, a new ioctl() would have to be added,
  2493. * so backwards-compatibility for the embedded platforms can be
  2494. * maintained. --davidm 4-Feb-2004.
  2495. */
  2496. case RADEON_PARAM_SAREA_HANDLE:
  2497. /* The lock is the first dword in the sarea. */
  2498. value = (long)dev->lock.hw_lock;
  2499. break;
  2500. #endif
  2501. case RADEON_PARAM_GART_TEX_HANDLE:
  2502. value = dev_priv->gart_textures_offset;
  2503. break;
  2504. default:
  2505. return DRM_ERR(EINVAL);
  2506. }
  2507. if ( DRM_COPY_TO_USER( param.value, &value, sizeof(int) ) ) {
  2508. DRM_ERROR( "copy_to_user\n" );
  2509. return DRM_ERR(EFAULT);
  2510. }
  2511. return 0;
  2512. }
  2513. static int radeon_cp_setparam( DRM_IOCTL_ARGS ) {
  2514. DRM_DEVICE;
  2515. drm_radeon_private_t *dev_priv = dev->dev_private;
  2516. drm_file_t *filp_priv;
  2517. drm_radeon_setparam_t sp;
  2518. struct drm_radeon_driver_file_fields *radeon_priv;
  2519. if ( !dev_priv ) {
  2520. DRM_ERROR( "%s called with no initialization\n", __FUNCTION__ );
  2521. return DRM_ERR( EINVAL );
  2522. }
  2523. DRM_GET_PRIV_WITH_RETURN( filp_priv, filp );
  2524. DRM_COPY_FROM_USER_IOCTL( sp, ( drm_radeon_setparam_t __user * )data,
  2525. sizeof( sp ) );
  2526. switch( sp.param ) {
  2527. case RADEON_SETPARAM_FB_LOCATION:
  2528. radeon_priv = filp_priv->driver_priv;
  2529. radeon_priv->radeon_fb_delta = dev_priv->fb_location - sp.value;
  2530. break;
  2531. case RADEON_SETPARAM_SWITCH_TILING:
  2532. if (sp.value == 0) {
  2533. DRM_DEBUG( "color tiling disabled\n" );
  2534. dev_priv->front_pitch_offset &= ~RADEON_DST_TILE_MACRO;
  2535. dev_priv->back_pitch_offset &= ~RADEON_DST_TILE_MACRO;
  2536. dev_priv->sarea_priv->tiling_enabled = 0;
  2537. }
  2538. else if (sp.value == 1) {
  2539. DRM_DEBUG( "color tiling enabled\n" );
  2540. dev_priv->front_pitch_offset |= RADEON_DST_TILE_MACRO;
  2541. dev_priv->back_pitch_offset |= RADEON_DST_TILE_MACRO;
  2542. dev_priv->sarea_priv->tiling_enabled = 1;
  2543. }
  2544. break;
  2545. default:
  2546. DRM_DEBUG( "Invalid parameter %d\n", sp.param );
  2547. return DRM_ERR( EINVAL );
  2548. }
  2549. return 0;
  2550. }
  2551. /* When a client dies:
  2552. * - Check for and clean up flipped page state
  2553. * - Free any alloced GART memory.
  2554. *
  2555. * DRM infrastructure takes care of reclaiming dma buffers.
  2556. */
  2557. void radeon_driver_prerelease(drm_device_t *dev, DRMFILE filp)
  2558. {
  2559. if ( dev->dev_private ) {
  2560. drm_radeon_private_t *dev_priv = dev->dev_private;
  2561. if ( dev_priv->page_flipping ) {
  2562. radeon_do_cleanup_pageflip( dev );
  2563. }
  2564. radeon_mem_release( filp, dev_priv->gart_heap );
  2565. radeon_mem_release( filp, dev_priv->fb_heap );
  2566. radeon_surfaces_release(filp, dev_priv);
  2567. }
  2568. }
  2569. void radeon_driver_pretakedown(drm_device_t *dev)
  2570. {
  2571. radeon_do_release(dev);
  2572. }
  2573. int radeon_driver_open_helper(drm_device_t *dev, drm_file_t *filp_priv)
  2574. {
  2575. drm_radeon_private_t *dev_priv = dev->dev_private;
  2576. struct drm_radeon_driver_file_fields *radeon_priv;
  2577. radeon_priv = (struct drm_radeon_driver_file_fields *)drm_alloc(sizeof(*radeon_priv), DRM_MEM_FILES);
  2578. if (!radeon_priv)
  2579. return -ENOMEM;
  2580. filp_priv->driver_priv = radeon_priv;
  2581. if ( dev_priv )
  2582. radeon_priv->radeon_fb_delta = dev_priv->fb_location;
  2583. else
  2584. radeon_priv->radeon_fb_delta = 0;
  2585. return 0;
  2586. }
  2587. void radeon_driver_free_filp_priv(drm_device_t *dev, drm_file_t *filp_priv)
  2588. {
  2589. struct drm_radeon_driver_file_fields *radeon_priv = filp_priv->driver_priv;
  2590. drm_free(radeon_priv, sizeof(*radeon_priv), DRM_MEM_FILES);
  2591. }
  2592. drm_ioctl_desc_t radeon_ioctls[] = {
  2593. [DRM_IOCTL_NR(DRM_RADEON_CP_INIT)] = { radeon_cp_init, 1, 1 },
  2594. [DRM_IOCTL_NR(DRM_RADEON_CP_START)] = { radeon_cp_start, 1, 1 },
  2595. [DRM_IOCTL_NR(DRM_RADEON_CP_STOP)] = { radeon_cp_stop, 1, 1 },
  2596. [DRM_IOCTL_NR(DRM_RADEON_CP_RESET)] = { radeon_cp_reset, 1, 1 },
  2597. [DRM_IOCTL_NR(DRM_RADEON_CP_IDLE)] = { radeon_cp_idle, 1, 0 },
  2598. [DRM_IOCTL_NR(DRM_RADEON_CP_RESUME)] = { radeon_cp_resume, 1, 0 },
  2599. [DRM_IOCTL_NR(DRM_RADEON_RESET)] = { radeon_engine_reset, 1, 0 },
  2600. [DRM_IOCTL_NR(DRM_RADEON_FULLSCREEN)] = { radeon_fullscreen, 1, 0 },
  2601. [DRM_IOCTL_NR(DRM_RADEON_SWAP)] = { radeon_cp_swap, 1, 0 },
  2602. [DRM_IOCTL_NR(DRM_RADEON_CLEAR)] = { radeon_cp_clear, 1, 0 },
  2603. [DRM_IOCTL_NR(DRM_RADEON_VERTEX)] = { radeon_cp_vertex, 1, 0 },
  2604. [DRM_IOCTL_NR(DRM_RADEON_INDICES)] = { radeon_cp_indices, 1, 0 },
  2605. [DRM_IOCTL_NR(DRM_RADEON_TEXTURE)] = { radeon_cp_texture, 1, 0 },
  2606. [DRM_IOCTL_NR(DRM_RADEON_STIPPLE)] = { radeon_cp_stipple, 1, 0 },
  2607. [DRM_IOCTL_NR(DRM_RADEON_INDIRECT)] = { radeon_cp_indirect, 1, 1 },
  2608. [DRM_IOCTL_NR(DRM_RADEON_VERTEX2)] = { radeon_cp_vertex2, 1, 0 },
  2609. [DRM_IOCTL_NR(DRM_RADEON_CMDBUF)] = { radeon_cp_cmdbuf, 1, 0 },
  2610. [DRM_IOCTL_NR(DRM_RADEON_GETPARAM)] = { radeon_cp_getparam, 1, 0 },
  2611. [DRM_IOCTL_NR(DRM_RADEON_FLIP)] = { radeon_cp_flip, 1, 0 },
  2612. [DRM_IOCTL_NR(DRM_RADEON_ALLOC)] = { radeon_mem_alloc, 1, 0 },
  2613. [DRM_IOCTL_NR(DRM_RADEON_FREE)] = { radeon_mem_free, 1, 0 },
  2614. [DRM_IOCTL_NR(DRM_RADEON_INIT_HEAP)] = { radeon_mem_init_heap,1, 1 },
  2615. [DRM_IOCTL_NR(DRM_RADEON_IRQ_EMIT)] = { radeon_irq_emit, 1, 0 },
  2616. [DRM_IOCTL_NR(DRM_RADEON_IRQ_WAIT)] = { radeon_irq_wait, 1, 0 },
  2617. [DRM_IOCTL_NR(DRM_RADEON_SETPARAM)] = { radeon_cp_setparam, 1, 0 },
  2618. [DRM_IOCTL_NR(DRM_RADEON_SURF_ALLOC)] = { radeon_surface_alloc,1, 0 },
  2619. [DRM_IOCTL_NR(DRM_RADEON_SURF_FREE)] = { radeon_surface_free, 1, 0 }
  2620. };
  2621. int radeon_max_ioctl = DRM_ARRAY_SIZE(radeon_ioctls);