evergreen_blit_kms.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. /*
  2. * Copyright 2010 Advanced Micro Devices, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE COPYRIGHT HOLDER(S) AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  19. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  20. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Alex Deucher <alexander.deucher@amd.com>
  25. */
  26. #include "drmP.h"
  27. #include "drm.h"
  28. #include "radeon_drm.h"
  29. #include "radeon.h"
  30. #include "evergreend.h"
  31. #include "evergreen_blit_shaders.h"
  32. #define DI_PT_RECTLIST 0x11
  33. #define DI_INDEX_SIZE_16_BIT 0x0
  34. #define DI_SRC_SEL_AUTO_INDEX 0x2
  35. #define FMT_8 0x1
  36. #define FMT_5_6_5 0x8
  37. #define FMT_8_8_8_8 0x1a
  38. #define COLOR_8 0x1
  39. #define COLOR_5_6_5 0x8
  40. #define COLOR_8_8_8_8 0x1a
  41. /* emits 17 */
  42. static void
  43. set_render_target(struct radeon_device *rdev, int format,
  44. int w, int h, u64 gpu_addr)
  45. {
  46. u32 cb_color_info;
  47. int pitch, slice;
  48. h = ALIGN(h, 8);
  49. if (h < 8)
  50. h = 8;
  51. cb_color_info = ((format << 2) | (1 << 24));
  52. pitch = (w / 8) - 1;
  53. slice = ((w * h) / 64) - 1;
  54. radeon_ring_write(rdev, PACKET3(PACKET3_SET_CONTEXT_REG, 15));
  55. radeon_ring_write(rdev, (CB_COLOR0_BASE - PACKET3_SET_CONTEXT_REG_START) >> 2);
  56. radeon_ring_write(rdev, gpu_addr >> 8);
  57. radeon_ring_write(rdev, pitch);
  58. radeon_ring_write(rdev, slice);
  59. radeon_ring_write(rdev, 0);
  60. radeon_ring_write(rdev, cb_color_info);
  61. radeon_ring_write(rdev, (1 << 4));
  62. radeon_ring_write(rdev, (w - 1) | ((h - 1) << 16));
  63. radeon_ring_write(rdev, 0);
  64. radeon_ring_write(rdev, 0);
  65. radeon_ring_write(rdev, 0);
  66. radeon_ring_write(rdev, 0);
  67. radeon_ring_write(rdev, 0);
  68. radeon_ring_write(rdev, 0);
  69. radeon_ring_write(rdev, 0);
  70. radeon_ring_write(rdev, 0);
  71. }
  72. /* emits 5dw */
  73. static void
  74. cp_set_surface_sync(struct radeon_device *rdev,
  75. u32 sync_type, u32 size,
  76. u64 mc_addr)
  77. {
  78. u32 cp_coher_size;
  79. if (size == 0xffffffff)
  80. cp_coher_size = 0xffffffff;
  81. else
  82. cp_coher_size = ((size + 255) >> 8);
  83. radeon_ring_write(rdev, PACKET3(PACKET3_SURFACE_SYNC, 3));
  84. radeon_ring_write(rdev, sync_type);
  85. radeon_ring_write(rdev, cp_coher_size);
  86. radeon_ring_write(rdev, mc_addr >> 8);
  87. radeon_ring_write(rdev, 10); /* poll interval */
  88. }
  89. /* emits 11dw + 1 surface sync = 16dw */
  90. static void
  91. set_shaders(struct radeon_device *rdev)
  92. {
  93. u64 gpu_addr;
  94. /* VS */
  95. gpu_addr = rdev->r600_blit.shader_gpu_addr + rdev->r600_blit.vs_offset;
  96. radeon_ring_write(rdev, PACKET3(PACKET3_SET_CONTEXT_REG, 3));
  97. radeon_ring_write(rdev, (SQ_PGM_START_VS - PACKET3_SET_CONTEXT_REG_START) >> 2);
  98. radeon_ring_write(rdev, gpu_addr >> 8);
  99. radeon_ring_write(rdev, 2);
  100. radeon_ring_write(rdev, 0);
  101. /* PS */
  102. gpu_addr = rdev->r600_blit.shader_gpu_addr + rdev->r600_blit.ps_offset;
  103. radeon_ring_write(rdev, PACKET3(PACKET3_SET_CONTEXT_REG, 4));
  104. radeon_ring_write(rdev, (SQ_PGM_START_PS - PACKET3_SET_CONTEXT_REG_START) >> 2);
  105. radeon_ring_write(rdev, gpu_addr >> 8);
  106. radeon_ring_write(rdev, 1);
  107. radeon_ring_write(rdev, 0);
  108. radeon_ring_write(rdev, 2);
  109. gpu_addr = rdev->r600_blit.shader_gpu_addr + rdev->r600_blit.vs_offset;
  110. cp_set_surface_sync(rdev, PACKET3_SH_ACTION_ENA, 512, gpu_addr);
  111. }
  112. /* emits 10 + 1 sync (5) = 15 */
  113. static void
  114. set_vtx_resource(struct radeon_device *rdev, u64 gpu_addr)
  115. {
  116. u32 sq_vtx_constant_word2, sq_vtx_constant_word3;
  117. /* high addr, stride */
  118. sq_vtx_constant_word2 = ((upper_32_bits(gpu_addr) & 0xff) | (16 << 8));
  119. /* xyzw swizzles */
  120. sq_vtx_constant_word3 = (0 << 3) | (1 << 6) | (2 << 9) | (3 << 12);
  121. radeon_ring_write(rdev, PACKET3(PACKET3_SET_RESOURCE, 8));
  122. radeon_ring_write(rdev, 0x580);
  123. radeon_ring_write(rdev, gpu_addr & 0xffffffff);
  124. radeon_ring_write(rdev, 48 - 1); /* size */
  125. radeon_ring_write(rdev, sq_vtx_constant_word2);
  126. radeon_ring_write(rdev, sq_vtx_constant_word3);
  127. radeon_ring_write(rdev, 0);
  128. radeon_ring_write(rdev, 0);
  129. radeon_ring_write(rdev, 0);
  130. radeon_ring_write(rdev, SQ_TEX_VTX_VALID_BUFFER << 30);
  131. if (rdev->family == CHIP_CEDAR)
  132. cp_set_surface_sync(rdev,
  133. PACKET3_TC_ACTION_ENA, 48, gpu_addr);
  134. else
  135. cp_set_surface_sync(rdev,
  136. PACKET3_VC_ACTION_ENA, 48, gpu_addr);
  137. }
  138. /* emits 10 */
  139. static void
  140. set_tex_resource(struct radeon_device *rdev,
  141. int format, int w, int h, int pitch,
  142. u64 gpu_addr)
  143. {
  144. u32 sq_tex_resource_word0, sq_tex_resource_word1;
  145. u32 sq_tex_resource_word4, sq_tex_resource_word7;
  146. if (h < 1)
  147. h = 1;
  148. sq_tex_resource_word0 = (1 << 0); /* 2D */
  149. sq_tex_resource_word0 |= ((((pitch >> 3) - 1) << 6) |
  150. ((w - 1) << 18));
  151. sq_tex_resource_word1 = ((h - 1) << 0);
  152. /* xyzw swizzles */
  153. sq_tex_resource_word4 = (0 << 16) | (1 << 19) | (2 << 22) | (3 << 25);
  154. sq_tex_resource_word7 = format | (SQ_TEX_VTX_VALID_TEXTURE << 30);
  155. radeon_ring_write(rdev, PACKET3(PACKET3_SET_RESOURCE, 8));
  156. radeon_ring_write(rdev, 0);
  157. radeon_ring_write(rdev, sq_tex_resource_word0);
  158. radeon_ring_write(rdev, sq_tex_resource_word1);
  159. radeon_ring_write(rdev, gpu_addr >> 8);
  160. radeon_ring_write(rdev, gpu_addr >> 8);
  161. radeon_ring_write(rdev, sq_tex_resource_word4);
  162. radeon_ring_write(rdev, 0);
  163. radeon_ring_write(rdev, 0);
  164. radeon_ring_write(rdev, sq_tex_resource_word7);
  165. }
  166. /* emits 12 */
  167. static void
  168. set_scissors(struct radeon_device *rdev, int x1, int y1,
  169. int x2, int y2)
  170. {
  171. radeon_ring_write(rdev, PACKET3(PACKET3_SET_CONTEXT_REG, 2));
  172. radeon_ring_write(rdev, (PA_SC_SCREEN_SCISSOR_TL - PACKET3_SET_CONTEXT_REG_START) >> 2);
  173. radeon_ring_write(rdev, (x1 << 0) | (y1 << 16));
  174. radeon_ring_write(rdev, (x2 << 0) | (y2 << 16));
  175. radeon_ring_write(rdev, PACKET3(PACKET3_SET_CONTEXT_REG, 2));
  176. radeon_ring_write(rdev, (PA_SC_GENERIC_SCISSOR_TL - PACKET3_SET_CONTEXT_REG_START) >> 2);
  177. radeon_ring_write(rdev, (x1 << 0) | (y1 << 16) | (1 << 31));
  178. radeon_ring_write(rdev, (x2 << 0) | (y2 << 16));
  179. radeon_ring_write(rdev, PACKET3(PACKET3_SET_CONTEXT_REG, 2));
  180. radeon_ring_write(rdev, (PA_SC_WINDOW_SCISSOR_TL - PACKET3_SET_CONTEXT_REG_START) >> 2);
  181. radeon_ring_write(rdev, (x1 << 0) | (y1 << 16) | (1 << 31));
  182. radeon_ring_write(rdev, (x2 << 0) | (y2 << 16));
  183. }
  184. /* emits 10 */
  185. static void
  186. draw_auto(struct radeon_device *rdev)
  187. {
  188. radeon_ring_write(rdev, PACKET3(PACKET3_SET_CONFIG_REG, 1));
  189. radeon_ring_write(rdev, (VGT_PRIMITIVE_TYPE - PACKET3_SET_CONFIG_REG_START) >> 2);
  190. radeon_ring_write(rdev, DI_PT_RECTLIST);
  191. radeon_ring_write(rdev, PACKET3(PACKET3_INDEX_TYPE, 0));
  192. radeon_ring_write(rdev, DI_INDEX_SIZE_16_BIT);
  193. radeon_ring_write(rdev, PACKET3(PACKET3_NUM_INSTANCES, 0));
  194. radeon_ring_write(rdev, 1);
  195. radeon_ring_write(rdev, PACKET3(PACKET3_DRAW_INDEX_AUTO, 1));
  196. radeon_ring_write(rdev, 3);
  197. radeon_ring_write(rdev, DI_SRC_SEL_AUTO_INDEX);
  198. }
  199. /* emits 30 */
  200. static void
  201. set_default_state(struct radeon_device *rdev)
  202. {
  203. u32 sq_config, sq_gpr_resource_mgmt_1, sq_gpr_resource_mgmt_2, sq_gpr_resource_mgmt_3;
  204. u32 sq_thread_resource_mgmt, sq_thread_resource_mgmt_2;
  205. u32 sq_stack_resource_mgmt_1, sq_stack_resource_mgmt_2, sq_stack_resource_mgmt_3;
  206. int num_ps_gprs, num_vs_gprs, num_temp_gprs;
  207. int num_gs_gprs, num_es_gprs, num_hs_gprs, num_ls_gprs;
  208. int num_ps_threads, num_vs_threads, num_gs_threads, num_es_threads;
  209. int num_hs_threads, num_ls_threads;
  210. int num_ps_stack_entries, num_vs_stack_entries, num_gs_stack_entries, num_es_stack_entries;
  211. int num_hs_stack_entries, num_ls_stack_entries;
  212. switch (rdev->family) {
  213. case CHIP_CEDAR:
  214. default:
  215. num_ps_gprs = 93;
  216. num_vs_gprs = 46;
  217. num_temp_gprs = 4;
  218. num_gs_gprs = 31;
  219. num_es_gprs = 31;
  220. num_hs_gprs = 23;
  221. num_ls_gprs = 23;
  222. num_ps_threads = 96;
  223. num_vs_threads = 16;
  224. num_gs_threads = 16;
  225. num_es_threads = 16;
  226. num_hs_threads = 16;
  227. num_ls_threads = 16;
  228. num_ps_stack_entries = 42;
  229. num_vs_stack_entries = 42;
  230. num_gs_stack_entries = 42;
  231. num_es_stack_entries = 42;
  232. num_hs_stack_entries = 42;
  233. num_ls_stack_entries = 42;
  234. break;
  235. case CHIP_REDWOOD:
  236. num_ps_gprs = 93;
  237. num_vs_gprs = 46;
  238. num_temp_gprs = 4;
  239. num_gs_gprs = 31;
  240. num_es_gprs = 31;
  241. num_hs_gprs = 23;
  242. num_ls_gprs = 23;
  243. num_ps_threads = 128;
  244. num_vs_threads = 20;
  245. num_gs_threads = 20;
  246. num_es_threads = 20;
  247. num_hs_threads = 20;
  248. num_ls_threads = 20;
  249. num_ps_stack_entries = 42;
  250. num_vs_stack_entries = 42;
  251. num_gs_stack_entries = 42;
  252. num_es_stack_entries = 42;
  253. num_hs_stack_entries = 42;
  254. num_ls_stack_entries = 42;
  255. break;
  256. case CHIP_JUNIPER:
  257. num_ps_gprs = 93;
  258. num_vs_gprs = 46;
  259. num_temp_gprs = 4;
  260. num_gs_gprs = 31;
  261. num_es_gprs = 31;
  262. num_hs_gprs = 23;
  263. num_ls_gprs = 23;
  264. num_ps_threads = 128;
  265. num_vs_threads = 20;
  266. num_gs_threads = 20;
  267. num_es_threads = 20;
  268. num_hs_threads = 20;
  269. num_ls_threads = 20;
  270. num_ps_stack_entries = 85;
  271. num_vs_stack_entries = 85;
  272. num_gs_stack_entries = 85;
  273. num_es_stack_entries = 85;
  274. num_hs_stack_entries = 85;
  275. num_ls_stack_entries = 85;
  276. break;
  277. case CHIP_CYPRESS:
  278. case CHIP_HEMLOCK:
  279. num_ps_gprs = 93;
  280. num_vs_gprs = 46;
  281. num_temp_gprs = 4;
  282. num_gs_gprs = 31;
  283. num_es_gprs = 31;
  284. num_hs_gprs = 23;
  285. num_ls_gprs = 23;
  286. num_ps_threads = 128;
  287. num_vs_threads = 20;
  288. num_gs_threads = 20;
  289. num_es_threads = 20;
  290. num_hs_threads = 20;
  291. num_ls_threads = 20;
  292. num_ps_stack_entries = 85;
  293. num_vs_stack_entries = 85;
  294. num_gs_stack_entries = 85;
  295. num_es_stack_entries = 85;
  296. num_hs_stack_entries = 85;
  297. num_ls_stack_entries = 85;
  298. break;
  299. }
  300. if (rdev->family == CHIP_CEDAR)
  301. sq_config = 0;
  302. else
  303. sq_config = VC_ENABLE;
  304. sq_config |= (EXPORT_SRC_C |
  305. CS_PRIO(0) |
  306. LS_PRIO(0) |
  307. HS_PRIO(0) |
  308. PS_PRIO(0) |
  309. VS_PRIO(1) |
  310. GS_PRIO(2) |
  311. ES_PRIO(3));
  312. sq_gpr_resource_mgmt_1 = (NUM_PS_GPRS(num_ps_gprs) |
  313. NUM_VS_GPRS(num_vs_gprs) |
  314. NUM_CLAUSE_TEMP_GPRS(num_temp_gprs));
  315. sq_gpr_resource_mgmt_2 = (NUM_GS_GPRS(num_gs_gprs) |
  316. NUM_ES_GPRS(num_es_gprs));
  317. sq_gpr_resource_mgmt_3 = (NUM_HS_GPRS(num_hs_gprs) |
  318. NUM_LS_GPRS(num_ls_gprs));
  319. sq_thread_resource_mgmt = (NUM_PS_THREADS(num_ps_threads) |
  320. NUM_VS_THREADS(num_vs_threads) |
  321. NUM_GS_THREADS(num_gs_threads) |
  322. NUM_ES_THREADS(num_es_threads));
  323. sq_thread_resource_mgmt_2 = (NUM_HS_THREADS(num_hs_threads) |
  324. NUM_LS_THREADS(num_ls_threads));
  325. sq_stack_resource_mgmt_1 = (NUM_PS_STACK_ENTRIES(num_ps_stack_entries) |
  326. NUM_VS_STACK_ENTRIES(num_vs_stack_entries));
  327. sq_stack_resource_mgmt_2 = (NUM_GS_STACK_ENTRIES(num_gs_stack_entries) |
  328. NUM_ES_STACK_ENTRIES(num_es_stack_entries));
  329. sq_stack_resource_mgmt_3 = (NUM_HS_STACK_ENTRIES(num_hs_stack_entries) |
  330. NUM_LS_STACK_ENTRIES(num_ls_stack_entries));
  331. /* set clear context state */
  332. radeon_ring_write(rdev, PACKET3(PACKET3_CLEAR_STATE, 0));
  333. radeon_ring_write(rdev, 0);
  334. /* disable dyn gprs */
  335. radeon_ring_write(rdev, PACKET3(PACKET3_SET_CONFIG_REG, 1));
  336. radeon_ring_write(rdev, (SQ_DYN_GPR_CNTL_PS_FLUSH_REQ - PACKET3_SET_CONFIG_REG_START) >> 2);
  337. radeon_ring_write(rdev, 0);
  338. /* SQ config */
  339. radeon_ring_write(rdev, PACKET3(PACKET3_SET_CONFIG_REG, 11));
  340. radeon_ring_write(rdev, (SQ_CONFIG - PACKET3_SET_CONFIG_REG_START) >> 2);
  341. radeon_ring_write(rdev, sq_config);
  342. radeon_ring_write(rdev, sq_gpr_resource_mgmt_1);
  343. radeon_ring_write(rdev, sq_gpr_resource_mgmt_2);
  344. radeon_ring_write(rdev, sq_gpr_resource_mgmt_3);
  345. radeon_ring_write(rdev, 0);
  346. radeon_ring_write(rdev, 0);
  347. radeon_ring_write(rdev, sq_thread_resource_mgmt);
  348. radeon_ring_write(rdev, sq_thread_resource_mgmt_2);
  349. radeon_ring_write(rdev, sq_stack_resource_mgmt_1);
  350. radeon_ring_write(rdev, sq_stack_resource_mgmt_2);
  351. radeon_ring_write(rdev, sq_stack_resource_mgmt_3);
  352. /* CONTEXT_CONTROL */
  353. radeon_ring_write(rdev, 0xc0012800);
  354. radeon_ring_write(rdev, 0x80000000);
  355. radeon_ring_write(rdev, 0x80000000);
  356. /* SQ_VTX_BASE_VTX_LOC */
  357. radeon_ring_write(rdev, 0xc0026f00);
  358. radeon_ring_write(rdev, 0x00000000);
  359. radeon_ring_write(rdev, 0x00000000);
  360. radeon_ring_write(rdev, 0x00000000);
  361. /* SET_SAMPLER */
  362. radeon_ring_write(rdev, 0xc0036e00);
  363. radeon_ring_write(rdev, 0x00000000);
  364. radeon_ring_write(rdev, 0x00000012);
  365. radeon_ring_write(rdev, 0x00000000);
  366. radeon_ring_write(rdev, 0x00000000);
  367. }
  368. static inline uint32_t i2f(uint32_t input)
  369. {
  370. u32 result, i, exponent, fraction;
  371. if ((input & 0x3fff) == 0)
  372. result = 0; /* 0 is a special case */
  373. else {
  374. exponent = 140; /* exponent biased by 127; */
  375. fraction = (input & 0x3fff) << 10; /* cheat and only
  376. handle numbers below 2^^15 */
  377. for (i = 0; i < 14; i++) {
  378. if (fraction & 0x800000)
  379. break;
  380. else {
  381. fraction = fraction << 1; /* keep
  382. shifting left until top bit = 1 */
  383. exponent = exponent - 1;
  384. }
  385. }
  386. result = exponent << 23 | (fraction & 0x7fffff); /* mask
  387. off top bit; assumed 1 */
  388. }
  389. return result;
  390. }
  391. int evergreen_blit_init(struct radeon_device *rdev)
  392. {
  393. u32 obj_size;
  394. int r;
  395. void *ptr;
  396. /* pin copy shader into vram if already initialized */
  397. if (rdev->r600_blit.shader_obj)
  398. goto done;
  399. mutex_init(&rdev->r600_blit.mutex);
  400. rdev->r600_blit.state_offset = 0;
  401. rdev->r600_blit.state_len = 0;
  402. obj_size = 0;
  403. rdev->r600_blit.vs_offset = obj_size;
  404. obj_size += evergreen_vs_size * 4;
  405. obj_size = ALIGN(obj_size, 256);
  406. rdev->r600_blit.ps_offset = obj_size;
  407. obj_size += evergreen_ps_size * 4;
  408. obj_size = ALIGN(obj_size, 256);
  409. r = radeon_bo_create(rdev, NULL, obj_size, true, RADEON_GEM_DOMAIN_VRAM,
  410. &rdev->r600_blit.shader_obj);
  411. if (r) {
  412. DRM_ERROR("evergreen failed to allocate shader\n");
  413. return r;
  414. }
  415. DRM_DEBUG("evergreen blit allocated bo %08x vs %08x ps %08x\n",
  416. obj_size,
  417. rdev->r600_blit.vs_offset, rdev->r600_blit.ps_offset);
  418. r = radeon_bo_reserve(rdev->r600_blit.shader_obj, false);
  419. if (unlikely(r != 0))
  420. return r;
  421. r = radeon_bo_kmap(rdev->r600_blit.shader_obj, &ptr);
  422. if (r) {
  423. DRM_ERROR("failed to map blit object %d\n", r);
  424. return r;
  425. }
  426. memcpy(ptr + rdev->r600_blit.vs_offset, evergreen_vs, evergreen_vs_size * 4);
  427. memcpy(ptr + rdev->r600_blit.ps_offset, evergreen_ps, evergreen_ps_size * 4);
  428. radeon_bo_kunmap(rdev->r600_blit.shader_obj);
  429. radeon_bo_unreserve(rdev->r600_blit.shader_obj);
  430. done:
  431. r = radeon_bo_reserve(rdev->r600_blit.shader_obj, false);
  432. if (unlikely(r != 0))
  433. return r;
  434. r = radeon_bo_pin(rdev->r600_blit.shader_obj, RADEON_GEM_DOMAIN_VRAM,
  435. &rdev->r600_blit.shader_gpu_addr);
  436. radeon_bo_unreserve(rdev->r600_blit.shader_obj);
  437. if (r) {
  438. dev_err(rdev->dev, "(%d) pin blit object failed\n", r);
  439. return r;
  440. }
  441. rdev->mc.active_vram_size = rdev->mc.real_vram_size;
  442. return 0;
  443. }
  444. void evergreen_blit_fini(struct radeon_device *rdev)
  445. {
  446. int r;
  447. rdev->mc.active_vram_size = rdev->mc.visible_vram_size;
  448. if (rdev->r600_blit.shader_obj == NULL)
  449. return;
  450. /* If we can't reserve the bo, unref should be enough to destroy
  451. * it when it becomes idle.
  452. */
  453. r = radeon_bo_reserve(rdev->r600_blit.shader_obj, false);
  454. if (!r) {
  455. radeon_bo_unpin(rdev->r600_blit.shader_obj);
  456. radeon_bo_unreserve(rdev->r600_blit.shader_obj);
  457. }
  458. radeon_bo_unref(&rdev->r600_blit.shader_obj);
  459. }
  460. static int evergreen_vb_ib_get(struct radeon_device *rdev)
  461. {
  462. int r;
  463. r = radeon_ib_get(rdev, &rdev->r600_blit.vb_ib);
  464. if (r) {
  465. DRM_ERROR("failed to get IB for vertex buffer\n");
  466. return r;
  467. }
  468. rdev->r600_blit.vb_total = 64*1024;
  469. rdev->r600_blit.vb_used = 0;
  470. return 0;
  471. }
  472. static void evergreen_vb_ib_put(struct radeon_device *rdev)
  473. {
  474. radeon_fence_emit(rdev, rdev->r600_blit.vb_ib->fence);
  475. radeon_ib_free(rdev, &rdev->r600_blit.vb_ib);
  476. }
  477. int evergreen_blit_prepare_copy(struct radeon_device *rdev, int size_bytes)
  478. {
  479. int r;
  480. int ring_size, line_size;
  481. int max_size;
  482. /* loops of emits + fence emit possible */
  483. int dwords_per_loop = 74, num_loops;
  484. r = evergreen_vb_ib_get(rdev);
  485. if (r)
  486. return r;
  487. /* 8 bpp vs 32 bpp for xfer unit */
  488. if (size_bytes & 3)
  489. line_size = 8192;
  490. else
  491. line_size = 8192 * 4;
  492. max_size = 8192 * line_size;
  493. /* major loops cover the max size transfer */
  494. num_loops = ((size_bytes + max_size) / max_size);
  495. /* minor loops cover the extra non aligned bits */
  496. num_loops += ((size_bytes % line_size) ? 1 : 0);
  497. /* calculate number of loops correctly */
  498. ring_size = num_loops * dwords_per_loop;
  499. /* set default + shaders */
  500. ring_size += 46; /* shaders + def state */
  501. ring_size += 10; /* fence emit for VB IB */
  502. ring_size += 5; /* done copy */
  503. ring_size += 10; /* fence emit for done copy */
  504. r = radeon_ring_lock(rdev, ring_size);
  505. if (r)
  506. return r;
  507. set_default_state(rdev); /* 30 */
  508. set_shaders(rdev); /* 16 */
  509. return 0;
  510. }
  511. void evergreen_blit_done_copy(struct radeon_device *rdev, struct radeon_fence *fence)
  512. {
  513. int r;
  514. if (rdev->r600_blit.vb_ib)
  515. evergreen_vb_ib_put(rdev);
  516. if (fence)
  517. r = radeon_fence_emit(rdev, fence);
  518. radeon_ring_unlock_commit(rdev);
  519. }
  520. void evergreen_kms_blit_copy(struct radeon_device *rdev,
  521. u64 src_gpu_addr, u64 dst_gpu_addr,
  522. int size_bytes)
  523. {
  524. int max_bytes;
  525. u64 vb_gpu_addr;
  526. u32 *vb;
  527. DRM_DEBUG("emitting copy %16llx %16llx %d %d\n", src_gpu_addr, dst_gpu_addr,
  528. size_bytes, rdev->r600_blit.vb_used);
  529. vb = (u32 *)(rdev->r600_blit.vb_ib->ptr + rdev->r600_blit.vb_used);
  530. if ((size_bytes & 3) || (src_gpu_addr & 3) || (dst_gpu_addr & 3)) {
  531. max_bytes = 8192;
  532. while (size_bytes) {
  533. int cur_size = size_bytes;
  534. int src_x = src_gpu_addr & 255;
  535. int dst_x = dst_gpu_addr & 255;
  536. int h = 1;
  537. src_gpu_addr = src_gpu_addr & ~255ULL;
  538. dst_gpu_addr = dst_gpu_addr & ~255ULL;
  539. if (!src_x && !dst_x) {
  540. h = (cur_size / max_bytes);
  541. if (h > 8192)
  542. h = 8192;
  543. if (h == 0)
  544. h = 1;
  545. else
  546. cur_size = max_bytes;
  547. } else {
  548. if (cur_size > max_bytes)
  549. cur_size = max_bytes;
  550. if (cur_size > (max_bytes - dst_x))
  551. cur_size = (max_bytes - dst_x);
  552. if (cur_size > (max_bytes - src_x))
  553. cur_size = (max_bytes - src_x);
  554. }
  555. if ((rdev->r600_blit.vb_used + 48) > rdev->r600_blit.vb_total) {
  556. WARN_ON(1);
  557. }
  558. vb[0] = i2f(dst_x);
  559. vb[1] = 0;
  560. vb[2] = i2f(src_x);
  561. vb[3] = 0;
  562. vb[4] = i2f(dst_x);
  563. vb[5] = i2f(h);
  564. vb[6] = i2f(src_x);
  565. vb[7] = i2f(h);
  566. vb[8] = i2f(dst_x + cur_size);
  567. vb[9] = i2f(h);
  568. vb[10] = i2f(src_x + cur_size);
  569. vb[11] = i2f(h);
  570. /* src 10 */
  571. set_tex_resource(rdev, FMT_8,
  572. src_x + cur_size, h, src_x + cur_size,
  573. src_gpu_addr);
  574. /* 5 */
  575. cp_set_surface_sync(rdev,
  576. PACKET3_TC_ACTION_ENA, (src_x + cur_size * h), src_gpu_addr);
  577. /* dst 17 */
  578. set_render_target(rdev, COLOR_8,
  579. dst_x + cur_size, h,
  580. dst_gpu_addr);
  581. /* scissors 12 */
  582. set_scissors(rdev, dst_x, 0, dst_x + cur_size, h);
  583. /* 15 */
  584. vb_gpu_addr = rdev->r600_blit.vb_ib->gpu_addr + rdev->r600_blit.vb_used;
  585. set_vtx_resource(rdev, vb_gpu_addr);
  586. /* draw 10 */
  587. draw_auto(rdev);
  588. /* 5 */
  589. cp_set_surface_sync(rdev,
  590. PACKET3_CB_ACTION_ENA | PACKET3_CB0_DEST_BASE_ENA,
  591. cur_size * h, dst_gpu_addr);
  592. vb += 12;
  593. rdev->r600_blit.vb_used += 12 * 4;
  594. src_gpu_addr += cur_size * h;
  595. dst_gpu_addr += cur_size * h;
  596. size_bytes -= cur_size * h;
  597. }
  598. } else {
  599. max_bytes = 8192 * 4;
  600. while (size_bytes) {
  601. int cur_size = size_bytes;
  602. int src_x = (src_gpu_addr & 255);
  603. int dst_x = (dst_gpu_addr & 255);
  604. int h = 1;
  605. src_gpu_addr = src_gpu_addr & ~255ULL;
  606. dst_gpu_addr = dst_gpu_addr & ~255ULL;
  607. if (!src_x && !dst_x) {
  608. h = (cur_size / max_bytes);
  609. if (h > 8192)
  610. h = 8192;
  611. if (h == 0)
  612. h = 1;
  613. else
  614. cur_size = max_bytes;
  615. } else {
  616. if (cur_size > max_bytes)
  617. cur_size = max_bytes;
  618. if (cur_size > (max_bytes - dst_x))
  619. cur_size = (max_bytes - dst_x);
  620. if (cur_size > (max_bytes - src_x))
  621. cur_size = (max_bytes - src_x);
  622. }
  623. if ((rdev->r600_blit.vb_used + 48) > rdev->r600_blit.vb_total) {
  624. WARN_ON(1);
  625. }
  626. vb[0] = i2f(dst_x / 4);
  627. vb[1] = 0;
  628. vb[2] = i2f(src_x / 4);
  629. vb[3] = 0;
  630. vb[4] = i2f(dst_x / 4);
  631. vb[5] = i2f(h);
  632. vb[6] = i2f(src_x / 4);
  633. vb[7] = i2f(h);
  634. vb[8] = i2f((dst_x + cur_size) / 4);
  635. vb[9] = i2f(h);
  636. vb[10] = i2f((src_x + cur_size) / 4);
  637. vb[11] = i2f(h);
  638. /* src 10 */
  639. set_tex_resource(rdev, FMT_8_8_8_8,
  640. (src_x + cur_size) / 4,
  641. h, (src_x + cur_size) / 4,
  642. src_gpu_addr);
  643. /* 5 */
  644. cp_set_surface_sync(rdev,
  645. PACKET3_TC_ACTION_ENA, (src_x + cur_size * h), src_gpu_addr);
  646. /* dst 17 */
  647. set_render_target(rdev, COLOR_8_8_8_8,
  648. (dst_x + cur_size) / 4, h,
  649. dst_gpu_addr);
  650. /* scissors 12 */
  651. set_scissors(rdev, (dst_x / 4), 0, (dst_x + cur_size / 4), h);
  652. /* Vertex buffer setup 15 */
  653. vb_gpu_addr = rdev->r600_blit.vb_ib->gpu_addr + rdev->r600_blit.vb_used;
  654. set_vtx_resource(rdev, vb_gpu_addr);
  655. /* draw 10 */
  656. draw_auto(rdev);
  657. /* 5 */
  658. cp_set_surface_sync(rdev,
  659. PACKET3_CB_ACTION_ENA | PACKET3_CB0_DEST_BASE_ENA,
  660. cur_size * h, dst_gpu_addr);
  661. /* 74 ring dwords per loop */
  662. vb += 12;
  663. rdev->r600_blit.vb_used += 12 * 4;
  664. src_gpu_addr += cur_size * h;
  665. dst_gpu_addr += cur_size * h;
  666. size_bytes -= cur_size * h;
  667. }
  668. }
  669. }