xlnx_dp.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408
  1. /*
  2. * Xilinx Display Port
  3. *
  4. * Copyright (C) 2015 : GreenSocs Ltd
  5. * http://www.greensocs.com/ , email: info@greensocs.com
  6. *
  7. * Developed by :
  8. * Frederic Konrad <fred.konrad@greensocs.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 2 of the License, or
  13. * (at your option)any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License along
  21. * with this program; if not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. #include "qemu/osdep.h"
  25. #include "qapi/error.h"
  26. #include "qemu/error-report.h"
  27. #include "qemu/log.h"
  28. #include "qemu/module.h"
  29. #include "hw/display/xlnx_dp.h"
  30. #include "hw/irq.h"
  31. #include "migration/vmstate.h"
  32. #ifndef DEBUG_DP
  33. #define DEBUG_DP 0
  34. #endif
  35. #define DPRINTF(fmt, ...) do { \
  36. if (DEBUG_DP) { \
  37. qemu_log("xlnx_dp: " fmt , ## __VA_ARGS__); \
  38. } \
  39. } while (0)
  40. /*
  41. * Register offset for DP.
  42. */
  43. #define DP_LINK_BW_SET (0x0000 >> 2)
  44. #define DP_LANE_COUNT_SET (0x0004 >> 2)
  45. #define DP_ENHANCED_FRAME_EN (0x0008 >> 2)
  46. #define DP_TRAINING_PATTERN_SET (0x000C >> 2)
  47. #define DP_LINK_QUAL_PATTERN_SET (0x0010 >> 2)
  48. #define DP_SCRAMBLING_DISABLE (0x0014 >> 2)
  49. #define DP_DOWNSPREAD_CTRL (0x0018 >> 2)
  50. #define DP_SOFTWARE_RESET (0x001C >> 2)
  51. #define DP_TRANSMITTER_ENABLE (0x0080 >> 2)
  52. #define DP_MAIN_STREAM_ENABLE (0x0084 >> 2)
  53. #define DP_FORCE_SCRAMBLER_RESET (0x00C0 >> 2)
  54. #define DP_VERSION_REGISTER (0x00F8 >> 2)
  55. #define DP_CORE_ID (0x00FC >> 2)
  56. #define DP_AUX_COMMAND_REGISTER (0x0100 >> 2)
  57. #define AUX_ADDR_ONLY_MASK (0x1000)
  58. #define AUX_COMMAND_MASK (0x0F00)
  59. #define AUX_COMMAND_SHIFT (8)
  60. #define AUX_COMMAND_NBYTES (0x000F)
  61. #define DP_AUX_WRITE_FIFO (0x0104 >> 2)
  62. #define DP_AUX_ADDRESS (0x0108 >> 2)
  63. #define DP_AUX_CLOCK_DIVIDER (0x010C >> 2)
  64. #define DP_TX_USER_FIFO_OVERFLOW (0x0110 >> 2)
  65. #define DP_INTERRUPT_SIGNAL_STATE (0x0130 >> 2)
  66. #define DP_AUX_REPLY_DATA (0x0134 >> 2)
  67. #define DP_AUX_REPLY_CODE (0x0138 >> 2)
  68. #define DP_AUX_REPLY_COUNT (0x013C >> 2)
  69. #define DP_REPLY_DATA_COUNT (0x0148 >> 2)
  70. #define DP_REPLY_STATUS (0x014C >> 2)
  71. #define DP_HPD_DURATION (0x0150 >> 2)
  72. #define DP_MAIN_STREAM_HTOTAL (0x0180 >> 2)
  73. #define DP_MAIN_STREAM_VTOTAL (0x0184 >> 2)
  74. #define DP_MAIN_STREAM_POLARITY (0x0188 >> 2)
  75. #define DP_MAIN_STREAM_HSWIDTH (0x018C >> 2)
  76. #define DP_MAIN_STREAM_VSWIDTH (0x0190 >> 2)
  77. #define DP_MAIN_STREAM_HRES (0x0194 >> 2)
  78. #define DP_MAIN_STREAM_VRES (0x0198 >> 2)
  79. #define DP_MAIN_STREAM_HSTART (0x019C >> 2)
  80. #define DP_MAIN_STREAM_VSTART (0x01A0 >> 2)
  81. #define DP_MAIN_STREAM_MISC0 (0x01A4 >> 2)
  82. #define DP_MAIN_STREAM_MISC1 (0x01A8 >> 2)
  83. #define DP_MAIN_STREAM_M_VID (0x01AC >> 2)
  84. #define DP_MSA_TRANSFER_UNIT_SIZE (0x01B0 >> 2)
  85. #define DP_MAIN_STREAM_N_VID (0x01B4 >> 2)
  86. #define DP_USER_DATA_COUNT_PER_LANE (0x01BC >> 2)
  87. #define DP_MIN_BYTES_PER_TU (0x01C4 >> 2)
  88. #define DP_FRAC_BYTES_PER_TU (0x01C8 >> 2)
  89. #define DP_INIT_WAIT (0x01CC >> 2)
  90. #define DP_PHY_RESET (0x0200 >> 2)
  91. #define DP_PHY_VOLTAGE_DIFF_LANE_0 (0x0220 >> 2)
  92. #define DP_PHY_VOLTAGE_DIFF_LANE_1 (0x0224 >> 2)
  93. #define DP_TRANSMIT_PRBS7 (0x0230 >> 2)
  94. #define DP_PHY_CLOCK_SELECT (0x0234 >> 2)
  95. #define DP_TX_PHY_POWER_DOWN (0x0238 >> 2)
  96. #define DP_PHY_PRECURSOR_LANE_0 (0x023C >> 2)
  97. #define DP_PHY_PRECURSOR_LANE_1 (0x0240 >> 2)
  98. #define DP_PHY_POSTCURSOR_LANE_0 (0x024C >> 2)
  99. #define DP_PHY_POSTCURSOR_LANE_1 (0x0250 >> 2)
  100. #define DP_PHY_STATUS (0x0280 >> 2)
  101. #define DP_TX_AUDIO_CONTROL (0x0300 >> 2)
  102. #define DP_TX_AUD_CTRL (1)
  103. #define DP_TX_AUDIO_CHANNELS (0x0304 >> 2)
  104. #define DP_TX_AUDIO_INFO_DATA(n) ((0x0308 + 4 * n) >> 2)
  105. #define DP_TX_M_AUD (0x0328 >> 2)
  106. #define DP_TX_N_AUD (0x032C >> 2)
  107. #define DP_TX_AUDIO_EXT_DATA(n) ((0x0330 + 4 * n) >> 2)
  108. #define DP_INT_STATUS (0x03A0 >> 2)
  109. #define DP_INT_VBLNK_START (1 << 13)
  110. #define DP_INT_MASK (0x03A4 >> 2)
  111. #define DP_INT_EN (0x03A8 >> 2)
  112. #define DP_INT_DS (0x03AC >> 2)
  113. /*
  114. * Registers offset for Audio Video Buffer configuration.
  115. */
  116. #define V_BLEND_OFFSET (0xA000)
  117. #define V_BLEND_BG_CLR_0 (0x0000 >> 2)
  118. #define V_BLEND_BG_CLR_1 (0x0004 >> 2)
  119. #define V_BLEND_BG_CLR_2 (0x0008 >> 2)
  120. #define V_BLEND_SET_GLOBAL_ALPHA_REG (0x000C >> 2)
  121. #define V_BLEND_OUTPUT_VID_FORMAT (0x0014 >> 2)
  122. #define V_BLEND_LAYER0_CONTROL (0x0018 >> 2)
  123. #define V_BLEND_LAYER1_CONTROL (0x001C >> 2)
  124. #define V_BLEND_RGB2YCBCR_COEFF(n) ((0x0020 + 4 * n) >> 2)
  125. #define V_BLEND_IN1CSC_COEFF(n) ((0x0044 + 4 * n) >> 2)
  126. #define V_BLEND_LUMA_IN1CSC_OFFSET (0x0068 >> 2)
  127. #define V_BLEND_CR_IN1CSC_OFFSET (0x006C >> 2)
  128. #define V_BLEND_CB_IN1CSC_OFFSET (0x0070 >> 2)
  129. #define V_BLEND_LUMA_OUTCSC_OFFSET (0x0074 >> 2)
  130. #define V_BLEND_CR_OUTCSC_OFFSET (0x0078 >> 2)
  131. #define V_BLEND_CB_OUTCSC_OFFSET (0x007C >> 2)
  132. #define V_BLEND_IN2CSC_COEFF(n) ((0x0080 + 4 * n) >> 2)
  133. #define V_BLEND_LUMA_IN2CSC_OFFSET (0x00A4 >> 2)
  134. #define V_BLEND_CR_IN2CSC_OFFSET (0x00A8 >> 2)
  135. #define V_BLEND_CB_IN2CSC_OFFSET (0x00AC >> 2)
  136. #define V_BLEND_CHROMA_KEY_ENABLE (0x01D0 >> 2)
  137. #define V_BLEND_CHROMA_KEY_COMP1 (0x01D4 >> 2)
  138. #define V_BLEND_CHROMA_KEY_COMP2 (0x01D8 >> 2)
  139. #define V_BLEND_CHROMA_KEY_COMP3 (0x01DC >> 2)
  140. /*
  141. * Registers offset for Audio Video Buffer configuration.
  142. */
  143. #define AV_BUF_MANAGER_OFFSET (0xB000)
  144. #define AV_BUF_FORMAT (0x0000 >> 2)
  145. #define AV_BUF_NON_LIVE_LATENCY (0x0008 >> 2)
  146. #define AV_CHBUF0 (0x0010 >> 2)
  147. #define AV_CHBUF1 (0x0014 >> 2)
  148. #define AV_CHBUF2 (0x0018 >> 2)
  149. #define AV_CHBUF3 (0x001C >> 2)
  150. #define AV_CHBUF4 (0x0020 >> 2)
  151. #define AV_CHBUF5 (0x0024 >> 2)
  152. #define AV_BUF_STC_CONTROL (0x002C >> 2)
  153. #define AV_BUF_STC_INIT_VALUE0 (0x0030 >> 2)
  154. #define AV_BUF_STC_INIT_VALUE1 (0x0034 >> 2)
  155. #define AV_BUF_STC_ADJ (0x0038 >> 2)
  156. #define AV_BUF_STC_VIDEO_VSYNC_TS_REG0 (0x003C >> 2)
  157. #define AV_BUF_STC_VIDEO_VSYNC_TS_REG1 (0x0040 >> 2)
  158. #define AV_BUF_STC_EXT_VSYNC_TS_REG0 (0x0044 >> 2)
  159. #define AV_BUF_STC_EXT_VSYNC_TS_REG1 (0x0048 >> 2)
  160. #define AV_BUF_STC_CUSTOM_EVENT_TS_REG0 (0x004C >> 2)
  161. #define AV_BUF_STC_CUSTOM_EVENT_TS_REG1 (0x0050 >> 2)
  162. #define AV_BUF_STC_CUSTOM_EVENT2_TS_REG0 (0x0054 >> 2)
  163. #define AV_BUF_STC_CUSTOM_EVENT2_TS_REG1 (0x0058 >> 2)
  164. #define AV_BUF_STC_SNAPSHOT0 (0x0060 >> 2)
  165. #define AV_BUF_STC_SNAPSHOT1 (0x0064 >> 2)
  166. #define AV_BUF_OUTPUT_AUDIO_VIDEO_SELECT (0x0070 >> 2)
  167. #define AV_BUF_HCOUNT_VCOUNT_INT0 (0x0074 >> 2)
  168. #define AV_BUF_HCOUNT_VCOUNT_INT1 (0x0078 >> 2)
  169. #define AV_BUF_DITHER_CONFIG (0x007C >> 2)
  170. #define AV_BUF_DITHER_CONFIG_MAX (0x008C >> 2)
  171. #define AV_BUF_DITHER_CONFIG_MIN (0x0090 >> 2)
  172. #define AV_BUF_PATTERN_GEN_SELECT (0x0100 >> 2)
  173. #define AV_BUF_AUD_VID_CLK_SOURCE (0x0120 >> 2)
  174. #define AV_BUF_SRST_REG (0x0124 >> 2)
  175. #define AV_BUF_AUDIO_RDY_INTERVAL (0x0128 >> 2)
  176. #define AV_BUF_AUDIO_CH_CONFIG (0x012C >> 2)
  177. #define AV_BUF_GRAPHICS_COMP_SCALE_FACTOR(n)((0x0200 + 4 * n) >> 2)
  178. #define AV_BUF_VIDEO_COMP_SCALE_FACTOR(n) ((0x020C + 4 * n) >> 2)
  179. #define AV_BUF_LIVE_VIDEO_COMP_SF(n) ((0x0218 + 4 * n) >> 2)
  180. #define AV_BUF_LIVE_VID_CONFIG (0x0224 >> 2)
  181. #define AV_BUF_LIVE_GFX_COMP_SF(n) ((0x0228 + 4 * n) >> 2)
  182. #define AV_BUF_LIVE_GFX_CONFIG (0x0234 >> 2)
  183. #define AUDIO_MIXER_REGISTER_OFFSET (0xC000)
  184. #define AUDIO_MIXER_VOLUME_CONTROL (0x0000 >> 2)
  185. #define AUDIO_MIXER_META_DATA (0x0004 >> 2)
  186. #define AUD_CH_STATUS_REG(n) ((0x0008 + 4 * n) >> 2)
  187. #define AUD_CH_A_DATA_REG(n) ((0x0020 + 4 * n) >> 2)
  188. #define AUD_CH_B_DATA_REG(n) ((0x0038 + 4 * n) >> 2)
  189. #define DP_AUDIO_DMA_CHANNEL(n) (4 + n)
  190. #define DP_GRAPHIC_DMA_CHANNEL (3)
  191. #define DP_VIDEO_DMA_CHANNEL (0)
  192. enum DPGraphicFmt {
  193. DP_GRAPHIC_RGBA8888 = 0 << 8,
  194. DP_GRAPHIC_ABGR8888 = 1 << 8,
  195. DP_GRAPHIC_RGB888 = 2 << 8,
  196. DP_GRAPHIC_BGR888 = 3 << 8,
  197. DP_GRAPHIC_RGBA5551 = 4 << 8,
  198. DP_GRAPHIC_RGBA4444 = 5 << 8,
  199. DP_GRAPHIC_RGB565 = 6 << 8,
  200. DP_GRAPHIC_8BPP = 7 << 8,
  201. DP_GRAPHIC_4BPP = 8 << 8,
  202. DP_GRAPHIC_2BPP = 9 << 8,
  203. DP_GRAPHIC_1BPP = 10 << 8,
  204. DP_GRAPHIC_MASK = 0xF << 8
  205. };
  206. enum DPVideoFmt {
  207. DP_NL_VID_CB_Y0_CR_Y1 = 0,
  208. DP_NL_VID_CR_Y0_CB_Y1 = 1,
  209. DP_NL_VID_Y0_CR_Y1_CB = 2,
  210. DP_NL_VID_Y0_CB_Y1_CR = 3,
  211. DP_NL_VID_YV16 = 4,
  212. DP_NL_VID_YV24 = 5,
  213. DP_NL_VID_YV16CL = 6,
  214. DP_NL_VID_MONO = 7,
  215. DP_NL_VID_YV16CL2 = 8,
  216. DP_NL_VID_YUV444 = 9,
  217. DP_NL_VID_RGB888 = 10,
  218. DP_NL_VID_RGBA8880 = 11,
  219. DP_NL_VID_RGB888_10BPC = 12,
  220. DP_NL_VID_YUV444_10BPC = 13,
  221. DP_NL_VID_YV16CL2_10BPC = 14,
  222. DP_NL_VID_YV16CL_10BPC = 15,
  223. DP_NL_VID_YV16_10BPC = 16,
  224. DP_NL_VID_YV24_10BPC = 17,
  225. DP_NL_VID_Y_ONLY_10BPC = 18,
  226. DP_NL_VID_YV16_420 = 19,
  227. DP_NL_VID_YV16CL_420 = 20,
  228. DP_NL_VID_YV16CL2_420 = 21,
  229. DP_NL_VID_YV16_420_10BPC = 22,
  230. DP_NL_VID_YV16CL_420_10BPC = 23,
  231. DP_NL_VID_YV16CL2_420_10BPC = 24,
  232. DP_NL_VID_FMT_MASK = 0x1F
  233. };
  234. typedef enum DPGraphicFmt DPGraphicFmt;
  235. typedef enum DPVideoFmt DPVideoFmt;
  236. static const VMStateDescription vmstate_dp = {
  237. .name = TYPE_XLNX_DP,
  238. .version_id = 2,
  239. .fields = (VMStateField[]){
  240. VMSTATE_UINT32_ARRAY(core_registers, XlnxDPState,
  241. DP_CORE_REG_ARRAY_SIZE),
  242. VMSTATE_UINT32_ARRAY(avbufm_registers, XlnxDPState,
  243. DP_AVBUF_REG_ARRAY_SIZE),
  244. VMSTATE_UINT32_ARRAY(vblend_registers, XlnxDPState,
  245. DP_VBLEND_REG_ARRAY_SIZE),
  246. VMSTATE_UINT32_ARRAY(audio_registers, XlnxDPState,
  247. DP_AUDIO_REG_ARRAY_SIZE),
  248. VMSTATE_PTIMER(vblank, XlnxDPState),
  249. VMSTATE_END_OF_LIST()
  250. }
  251. };
  252. #define DP_VBLANK_PTIMER_POLICY (PTIMER_POLICY_WRAP_AFTER_ONE_PERIOD | \
  253. PTIMER_POLICY_CONTINUOUS_TRIGGER | \
  254. PTIMER_POLICY_NO_IMMEDIATE_TRIGGER)
  255. static void xlnx_dp_update_irq(XlnxDPState *s);
  256. static uint64_t xlnx_dp_audio_read(void *opaque, hwaddr offset, unsigned size)
  257. {
  258. XlnxDPState *s = XLNX_DP(opaque);
  259. offset = offset >> 2;
  260. return s->audio_registers[offset];
  261. }
  262. static void xlnx_dp_audio_write(void *opaque, hwaddr offset, uint64_t value,
  263. unsigned size)
  264. {
  265. XlnxDPState *s = XLNX_DP(opaque);
  266. offset = offset >> 2;
  267. switch (offset) {
  268. case AUDIO_MIXER_META_DATA:
  269. s->audio_registers[offset] = value & 0x00000001;
  270. break;
  271. default:
  272. s->audio_registers[offset] = value;
  273. break;
  274. }
  275. }
  276. static const MemoryRegionOps audio_ops = {
  277. .read = xlnx_dp_audio_read,
  278. .write = xlnx_dp_audio_write,
  279. .endianness = DEVICE_NATIVE_ENDIAN,
  280. };
  281. static inline uint32_t xlnx_dp_audio_get_volume(XlnxDPState *s,
  282. uint8_t channel)
  283. {
  284. switch (channel) {
  285. case 0:
  286. return extract32(s->audio_registers[AUDIO_MIXER_VOLUME_CONTROL], 0, 16);
  287. case 1:
  288. return extract32(s->audio_registers[AUDIO_MIXER_VOLUME_CONTROL], 16,
  289. 16);
  290. default:
  291. return 0;
  292. }
  293. }
  294. static inline void xlnx_dp_audio_activate(XlnxDPState *s)
  295. {
  296. bool activated = ((s->core_registers[DP_TX_AUDIO_CONTROL]
  297. & DP_TX_AUD_CTRL) != 0);
  298. AUD_set_active_out(s->amixer_output_stream, activated);
  299. xlnx_dpdma_set_host_data_location(s->dpdma, DP_AUDIO_DMA_CHANNEL(0),
  300. &s->audio_buffer_0);
  301. xlnx_dpdma_set_host_data_location(s->dpdma, DP_AUDIO_DMA_CHANNEL(1),
  302. &s->audio_buffer_1);
  303. }
  304. static inline void xlnx_dp_audio_mix_buffer(XlnxDPState *s)
  305. {
  306. /*
  307. * Audio packets are signed and have this shape:
  308. * | 16 | 16 | 16 | 16 | 16 | 16 | 16 | 16 |
  309. * | R3 | L3 | R2 | L2 | R1 | L1 | R0 | L0 |
  310. *
  311. * Output audio is 16bits saturated.
  312. */
  313. int i;
  314. if ((s->audio_data_available[0]) && (xlnx_dp_audio_get_volume(s, 0))) {
  315. for (i = 0; i < s->audio_data_available[0] / 2; i++) {
  316. s->temp_buffer[i] = (int64_t)(s->audio_buffer_0[i])
  317. * xlnx_dp_audio_get_volume(s, 0) / 8192;
  318. }
  319. s->byte_left = s->audio_data_available[0];
  320. } else {
  321. memset(s->temp_buffer, 0, s->audio_data_available[1] / 2);
  322. }
  323. if ((s->audio_data_available[1]) && (xlnx_dp_audio_get_volume(s, 1))) {
  324. if ((s->audio_data_available[0] == 0)
  325. || (s->audio_data_available[1] == s->audio_data_available[0])) {
  326. for (i = 0; i < s->audio_data_available[1] / 2; i++) {
  327. s->temp_buffer[i] += (int64_t)(s->audio_buffer_1[i])
  328. * xlnx_dp_audio_get_volume(s, 1) / 8192;
  329. }
  330. s->byte_left = s->audio_data_available[1];
  331. }
  332. }
  333. for (i = 0; i < s->byte_left / 2; i++) {
  334. s->out_buffer[i] = MAX(-32767, MIN(s->temp_buffer[i], 32767));
  335. }
  336. s->data_ptr = 0;
  337. }
  338. static void xlnx_dp_audio_callback(void *opaque, int avail)
  339. {
  340. /*
  341. * Get some data from the DPDMA and compute these datas.
  342. * Then wait for QEMU's audio subsystem to call this callback.
  343. */
  344. XlnxDPState *s = XLNX_DP(opaque);
  345. size_t written = 0;
  346. /* If there are already some data don't get more data. */
  347. if (s->byte_left == 0) {
  348. s->audio_data_available[0] = xlnx_dpdma_start_operation(s->dpdma, 4,
  349. true);
  350. s->audio_data_available[1] = xlnx_dpdma_start_operation(s->dpdma, 5,
  351. true);
  352. xlnx_dp_audio_mix_buffer(s);
  353. }
  354. /* Send the buffer through the audio. */
  355. if (s->byte_left <= MAX_QEMU_BUFFER_SIZE) {
  356. if (s->byte_left != 0) {
  357. written = AUD_write(s->amixer_output_stream,
  358. &s->out_buffer[s->data_ptr], s->byte_left);
  359. } else {
  360. int len_to_copy;
  361. /*
  362. * There is nothing to play.. We don't have any data! Fill the
  363. * buffer with zero's and send it.
  364. */
  365. written = 0;
  366. while (avail) {
  367. len_to_copy = MIN(AUD_CHBUF_MAX_DEPTH, avail);
  368. memset(s->out_buffer, 0, len_to_copy);
  369. avail -= AUD_write(s->amixer_output_stream, s->out_buffer,
  370. len_to_copy);
  371. }
  372. }
  373. } else {
  374. written = AUD_write(s->amixer_output_stream,
  375. &s->out_buffer[s->data_ptr], MAX_QEMU_BUFFER_SIZE);
  376. }
  377. s->byte_left -= written;
  378. s->data_ptr += written;
  379. }
  380. /*
  381. * AUX channel related function.
  382. */
  383. static void xlnx_dp_aux_clear_rx_fifo(XlnxDPState *s)
  384. {
  385. fifo8_reset(&s->rx_fifo);
  386. }
  387. static void xlnx_dp_aux_push_rx_fifo(XlnxDPState *s, uint8_t *buf, size_t len)
  388. {
  389. DPRINTF("Push %u data in rx_fifo\n", (unsigned)len);
  390. fifo8_push_all(&s->rx_fifo, buf, len);
  391. }
  392. static uint8_t xlnx_dp_aux_pop_rx_fifo(XlnxDPState *s)
  393. {
  394. uint8_t ret;
  395. if (fifo8_is_empty(&s->rx_fifo)) {
  396. qemu_log_mask(LOG_GUEST_ERROR,
  397. "%s: Reading empty RX_FIFO\n",
  398. __func__);
  399. /*
  400. * The datasheet is not clear about the reset value, it seems
  401. * to be unspecified. We choose to return '0'.
  402. */
  403. ret = 0;
  404. } else {
  405. ret = fifo8_pop(&s->rx_fifo);
  406. DPRINTF("pop 0x%" PRIX8 " from rx_fifo.\n", ret);
  407. }
  408. return ret;
  409. }
  410. static void xlnx_dp_aux_clear_tx_fifo(XlnxDPState *s)
  411. {
  412. fifo8_reset(&s->tx_fifo);
  413. }
  414. static void xlnx_dp_aux_push_tx_fifo(XlnxDPState *s, uint8_t *buf, size_t len)
  415. {
  416. DPRINTF("Push %u data in tx_fifo\n", (unsigned)len);
  417. fifo8_push_all(&s->tx_fifo, buf, len);
  418. }
  419. static uint8_t xlnx_dp_aux_pop_tx_fifo(XlnxDPState *s)
  420. {
  421. uint8_t ret;
  422. if (fifo8_is_empty(&s->tx_fifo)) {
  423. error_report("%s: TX_FIFO underflow", __func__);
  424. abort();
  425. }
  426. ret = fifo8_pop(&s->tx_fifo);
  427. DPRINTF("pop 0x%2.2X from tx_fifo.\n", ret);
  428. return ret;
  429. }
  430. static uint32_t xlnx_dp_aux_get_address(XlnxDPState *s)
  431. {
  432. return s->core_registers[DP_AUX_ADDRESS];
  433. }
  434. /*
  435. * Get command from the register.
  436. */
  437. static void xlnx_dp_aux_set_command(XlnxDPState *s, uint32_t value)
  438. {
  439. bool address_only = (value & AUX_ADDR_ONLY_MASK) != 0;
  440. AUXCommand cmd = (value & AUX_COMMAND_MASK) >> AUX_COMMAND_SHIFT;
  441. uint8_t nbytes = (value & AUX_COMMAND_NBYTES) + 1;
  442. uint8_t buf[16];
  443. int i;
  444. /*
  445. * When an address_only command is executed nothing happen to the fifo, so
  446. * just make nbytes = 0.
  447. */
  448. if (address_only) {
  449. nbytes = 0;
  450. }
  451. switch (cmd) {
  452. case READ_AUX:
  453. case READ_I2C:
  454. case READ_I2C_MOT:
  455. s->core_registers[DP_AUX_REPLY_CODE] = aux_request(s->aux_bus, cmd,
  456. xlnx_dp_aux_get_address(s),
  457. nbytes, buf);
  458. s->core_registers[DP_REPLY_DATA_COUNT] = nbytes;
  459. if (s->core_registers[DP_AUX_REPLY_CODE] == AUX_I2C_ACK) {
  460. xlnx_dp_aux_push_rx_fifo(s, buf, nbytes);
  461. }
  462. break;
  463. case WRITE_AUX:
  464. case WRITE_I2C:
  465. case WRITE_I2C_MOT:
  466. for (i = 0; i < nbytes; i++) {
  467. buf[i] = xlnx_dp_aux_pop_tx_fifo(s);
  468. }
  469. s->core_registers[DP_AUX_REPLY_CODE] = aux_request(s->aux_bus, cmd,
  470. xlnx_dp_aux_get_address(s),
  471. nbytes, buf);
  472. xlnx_dp_aux_clear_tx_fifo(s);
  473. break;
  474. case WRITE_I2C_STATUS:
  475. qemu_log_mask(LOG_UNIMP, "xlnx_dp: Write i2c status not implemented\n");
  476. break;
  477. default:
  478. qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid command: %u", __func__, cmd);
  479. return;
  480. }
  481. s->core_registers[DP_INTERRUPT_SIGNAL_STATE] |= 0x04;
  482. }
  483. static void xlnx_dp_set_dpdma(const Object *obj, const char *name, Object *val,
  484. Error **errp)
  485. {
  486. XlnxDPState *s = XLNX_DP(obj);
  487. if (s->console) {
  488. DisplaySurface *surface = qemu_console_surface(s->console);
  489. XlnxDPDMAState *dma = XLNX_DPDMA(val);
  490. xlnx_dpdma_set_host_data_location(dma, DP_GRAPHIC_DMA_CHANNEL,
  491. surface_data(surface));
  492. }
  493. }
  494. static inline uint8_t xlnx_dp_global_alpha_value(XlnxDPState *s)
  495. {
  496. return (s->vblend_registers[V_BLEND_SET_GLOBAL_ALPHA_REG] & 0x1FE) >> 1;
  497. }
  498. static inline bool xlnx_dp_global_alpha_enabled(XlnxDPState *s)
  499. {
  500. /*
  501. * If the alpha is totally opaque (255) we consider the alpha is disabled to
  502. * reduce CPU consumption.
  503. */
  504. return ((xlnx_dp_global_alpha_value(s) != 0xFF) &&
  505. ((s->vblend_registers[V_BLEND_SET_GLOBAL_ALPHA_REG] & 0x01) != 0));
  506. }
  507. static void xlnx_dp_recreate_surface(XlnxDPState *s)
  508. {
  509. /*
  510. * Two possibilities, if blending is enabled the console displays
  511. * bout_plane, if not g_plane is displayed.
  512. */
  513. uint16_t width = s->core_registers[DP_MAIN_STREAM_HRES];
  514. uint16_t height = s->core_registers[DP_MAIN_STREAM_VRES];
  515. DisplaySurface *current_console_surface = qemu_console_surface(s->console);
  516. if ((width != 0) && (height != 0)) {
  517. /*
  518. * As dpy_gfx_replace_surface calls qemu_free_displaysurface on the
  519. * surface we need to be careful and don't free the surface associated
  520. * to the console or double free will happen.
  521. */
  522. if (s->bout_plane.surface != current_console_surface) {
  523. qemu_free_displaysurface(s->bout_plane.surface);
  524. }
  525. if (s->v_plane.surface != current_console_surface) {
  526. qemu_free_displaysurface(s->v_plane.surface);
  527. }
  528. if (s->g_plane.surface != current_console_surface) {
  529. qemu_free_displaysurface(s->g_plane.surface);
  530. }
  531. s->g_plane.surface
  532. = qemu_create_displaysurface_from(width, height,
  533. s->g_plane.format, 0, NULL);
  534. s->v_plane.surface
  535. = qemu_create_displaysurface_from(width, height,
  536. s->v_plane.format, 0, NULL);
  537. if (xlnx_dp_global_alpha_enabled(s)) {
  538. s->bout_plane.surface =
  539. qemu_create_displaysurface_from(width,
  540. height,
  541. s->g_plane.format,
  542. 0, NULL);
  543. dpy_gfx_replace_surface(s->console, s->bout_plane.surface);
  544. } else {
  545. s->bout_plane.surface = NULL;
  546. dpy_gfx_replace_surface(s->console, s->g_plane.surface);
  547. }
  548. xlnx_dpdma_set_host_data_location(s->dpdma, DP_GRAPHIC_DMA_CHANNEL,
  549. surface_data(s->g_plane.surface));
  550. xlnx_dpdma_set_host_data_location(s->dpdma, DP_VIDEO_DMA_CHANNEL,
  551. surface_data(s->v_plane.surface));
  552. }
  553. }
  554. /*
  555. * Change the graphic format of the surface.
  556. */
  557. static void xlnx_dp_change_graphic_fmt(XlnxDPState *s)
  558. {
  559. switch (s->avbufm_registers[AV_BUF_FORMAT] & DP_GRAPHIC_MASK) {
  560. case DP_GRAPHIC_RGBA8888:
  561. s->g_plane.format = PIXMAN_r8g8b8a8;
  562. break;
  563. case DP_GRAPHIC_ABGR8888:
  564. s->g_plane.format = PIXMAN_a8b8g8r8;
  565. break;
  566. case DP_GRAPHIC_RGB565:
  567. s->g_plane.format = PIXMAN_r5g6b5;
  568. break;
  569. case DP_GRAPHIC_RGB888:
  570. s->g_plane.format = PIXMAN_r8g8b8;
  571. break;
  572. case DP_GRAPHIC_BGR888:
  573. s->g_plane.format = PIXMAN_b8g8r8;
  574. break;
  575. default:
  576. error_report("%s: unsupported graphic format %u", __func__,
  577. s->avbufm_registers[AV_BUF_FORMAT] & DP_GRAPHIC_MASK);
  578. abort();
  579. }
  580. switch (s->avbufm_registers[AV_BUF_FORMAT] & DP_NL_VID_FMT_MASK) {
  581. case 0:
  582. s->v_plane.format = PIXMAN_x8b8g8r8;
  583. break;
  584. case DP_NL_VID_Y0_CB_Y1_CR:
  585. s->v_plane.format = PIXMAN_yuy2;
  586. break;
  587. case DP_NL_VID_RGBA8880:
  588. s->v_plane.format = PIXMAN_x8b8g8r8;
  589. break;
  590. default:
  591. error_report("%s: unsupported video format %u", __func__,
  592. s->avbufm_registers[AV_BUF_FORMAT] & DP_NL_VID_FMT_MASK);
  593. abort();
  594. }
  595. xlnx_dp_recreate_surface(s);
  596. }
  597. static void xlnx_dp_update_irq(XlnxDPState *s)
  598. {
  599. uint32_t flags;
  600. flags = s->core_registers[DP_INT_STATUS] & ~s->core_registers[DP_INT_MASK];
  601. DPRINTF("update IRQ value = %" PRIx32 "\n", flags);
  602. qemu_set_irq(s->irq, flags != 0);
  603. }
  604. static uint64_t xlnx_dp_read(void *opaque, hwaddr offset, unsigned size)
  605. {
  606. XlnxDPState *s = XLNX_DP(opaque);
  607. uint64_t ret = 0;
  608. offset = offset >> 2;
  609. switch (offset) {
  610. case DP_TX_USER_FIFO_OVERFLOW:
  611. /* This register is cleared after a read */
  612. ret = s->core_registers[DP_TX_USER_FIFO_OVERFLOW];
  613. s->core_registers[DP_TX_USER_FIFO_OVERFLOW] = 0;
  614. break;
  615. case DP_AUX_REPLY_DATA:
  616. ret = xlnx_dp_aux_pop_rx_fifo(s);
  617. break;
  618. case DP_INTERRUPT_SIGNAL_STATE:
  619. /*
  620. * XXX: Not sure it is the right thing to do actually.
  621. * The register is not written by the device driver so it's stuck
  622. * to 0x04.
  623. */
  624. ret = s->core_registers[DP_INTERRUPT_SIGNAL_STATE];
  625. s->core_registers[DP_INTERRUPT_SIGNAL_STATE] &= ~0x04;
  626. break;
  627. case DP_AUX_WRITE_FIFO:
  628. case DP_TX_AUDIO_INFO_DATA(0):
  629. case DP_TX_AUDIO_INFO_DATA(1):
  630. case DP_TX_AUDIO_INFO_DATA(2):
  631. case DP_TX_AUDIO_INFO_DATA(3):
  632. case DP_TX_AUDIO_INFO_DATA(4):
  633. case DP_TX_AUDIO_INFO_DATA(5):
  634. case DP_TX_AUDIO_INFO_DATA(6):
  635. case DP_TX_AUDIO_INFO_DATA(7):
  636. case DP_TX_AUDIO_EXT_DATA(0):
  637. case DP_TX_AUDIO_EXT_DATA(1):
  638. case DP_TX_AUDIO_EXT_DATA(2):
  639. case DP_TX_AUDIO_EXT_DATA(3):
  640. case DP_TX_AUDIO_EXT_DATA(4):
  641. case DP_TX_AUDIO_EXT_DATA(5):
  642. case DP_TX_AUDIO_EXT_DATA(6):
  643. case DP_TX_AUDIO_EXT_DATA(7):
  644. case DP_TX_AUDIO_EXT_DATA(8):
  645. /* write only registers */
  646. ret = 0;
  647. break;
  648. default:
  649. assert(offset <= (0x3AC >> 2));
  650. if (offset == (0x3A8 >> 2) || offset == (0x3AC >> 2)) {
  651. ret = s->core_registers[DP_INT_MASK];
  652. } else {
  653. ret = s->core_registers[offset];
  654. }
  655. break;
  656. }
  657. DPRINTF("core read @%" PRIx64 " = 0x%8.8" PRIX64 "\n", offset << 2, ret);
  658. return ret;
  659. }
  660. static void xlnx_dp_write(void *opaque, hwaddr offset, uint64_t value,
  661. unsigned size)
  662. {
  663. XlnxDPState *s = XLNX_DP(opaque);
  664. DPRINTF("core write @%" PRIx64 " = 0x%8.8" PRIX64 "\n", offset, value);
  665. offset = offset >> 2;
  666. switch (offset) {
  667. /*
  668. * Only special write case are handled.
  669. */
  670. case DP_LINK_BW_SET:
  671. s->core_registers[offset] = value & 0x000000FF;
  672. break;
  673. case DP_LANE_COUNT_SET:
  674. case DP_MAIN_STREAM_MISC0:
  675. s->core_registers[offset] = value & 0x0000000F;
  676. break;
  677. case DP_TRAINING_PATTERN_SET:
  678. case DP_LINK_QUAL_PATTERN_SET:
  679. case DP_MAIN_STREAM_POLARITY:
  680. case DP_PHY_VOLTAGE_DIFF_LANE_0:
  681. case DP_PHY_VOLTAGE_DIFF_LANE_1:
  682. s->core_registers[offset] = value & 0x00000003;
  683. break;
  684. case DP_ENHANCED_FRAME_EN:
  685. case DP_SCRAMBLING_DISABLE:
  686. case DP_DOWNSPREAD_CTRL:
  687. case DP_MAIN_STREAM_ENABLE:
  688. case DP_TRANSMIT_PRBS7:
  689. s->core_registers[offset] = value & 0x00000001;
  690. break;
  691. case DP_PHY_CLOCK_SELECT:
  692. s->core_registers[offset] = value & 0x00000007;
  693. break;
  694. case DP_SOFTWARE_RESET:
  695. /*
  696. * No need to update this bit as it's read '0'.
  697. */
  698. /*
  699. * TODO: reset IP.
  700. */
  701. break;
  702. case DP_TRANSMITTER_ENABLE:
  703. s->core_registers[offset] = value & 0x01;
  704. ptimer_transaction_begin(s->vblank);
  705. if (value & 0x1) {
  706. ptimer_run(s->vblank, 0);
  707. } else {
  708. ptimer_stop(s->vblank);
  709. }
  710. ptimer_transaction_commit(s->vblank);
  711. break;
  712. case DP_FORCE_SCRAMBLER_RESET:
  713. /*
  714. * No need to update this bit as it's read '0'.
  715. */
  716. /*
  717. * TODO: force a scrambler reset??
  718. */
  719. break;
  720. case DP_AUX_COMMAND_REGISTER:
  721. s->core_registers[offset] = value & 0x00001F0F;
  722. xlnx_dp_aux_set_command(s, s->core_registers[offset]);
  723. break;
  724. case DP_MAIN_STREAM_HTOTAL:
  725. case DP_MAIN_STREAM_VTOTAL:
  726. case DP_MAIN_STREAM_HSTART:
  727. case DP_MAIN_STREAM_VSTART:
  728. s->core_registers[offset] = value & 0x0000FFFF;
  729. break;
  730. case DP_MAIN_STREAM_HRES:
  731. case DP_MAIN_STREAM_VRES:
  732. s->core_registers[offset] = value & 0x0000FFFF;
  733. xlnx_dp_recreate_surface(s);
  734. break;
  735. case DP_MAIN_STREAM_HSWIDTH:
  736. case DP_MAIN_STREAM_VSWIDTH:
  737. s->core_registers[offset] = value & 0x00007FFF;
  738. break;
  739. case DP_MAIN_STREAM_MISC1:
  740. s->core_registers[offset] = value & 0x00000086;
  741. break;
  742. case DP_MAIN_STREAM_M_VID:
  743. case DP_MAIN_STREAM_N_VID:
  744. s->core_registers[offset] = value & 0x00FFFFFF;
  745. break;
  746. case DP_MSA_TRANSFER_UNIT_SIZE:
  747. case DP_MIN_BYTES_PER_TU:
  748. case DP_INIT_WAIT:
  749. s->core_registers[offset] = value & 0x00000007;
  750. break;
  751. case DP_USER_DATA_COUNT_PER_LANE:
  752. s->core_registers[offset] = value & 0x0003FFFF;
  753. break;
  754. case DP_FRAC_BYTES_PER_TU:
  755. s->core_registers[offset] = value & 0x000003FF;
  756. break;
  757. case DP_PHY_RESET:
  758. s->core_registers[offset] = value & 0x00010003;
  759. /*
  760. * TODO: Reset something?
  761. */
  762. break;
  763. case DP_TX_PHY_POWER_DOWN:
  764. s->core_registers[offset] = value & 0x0000000F;
  765. /*
  766. * TODO: Power down things?
  767. */
  768. break;
  769. case DP_AUX_WRITE_FIFO: {
  770. uint8_t c = value;
  771. xlnx_dp_aux_push_tx_fifo(s, &c, 1);
  772. break;
  773. }
  774. case DP_AUX_CLOCK_DIVIDER:
  775. break;
  776. case DP_AUX_REPLY_COUNT:
  777. /*
  778. * Writing to this register clear the counter.
  779. */
  780. s->core_registers[offset] = 0x00000000;
  781. break;
  782. case DP_AUX_ADDRESS:
  783. s->core_registers[offset] = value & 0x000FFFFF;
  784. break;
  785. case DP_VERSION_REGISTER:
  786. case DP_CORE_ID:
  787. case DP_TX_USER_FIFO_OVERFLOW:
  788. case DP_AUX_REPLY_DATA:
  789. case DP_AUX_REPLY_CODE:
  790. case DP_REPLY_DATA_COUNT:
  791. case DP_REPLY_STATUS:
  792. case DP_HPD_DURATION:
  793. /*
  794. * Write to read only location..
  795. */
  796. break;
  797. case DP_TX_AUDIO_CONTROL:
  798. s->core_registers[offset] = value & 0x00000001;
  799. xlnx_dp_audio_activate(s);
  800. break;
  801. case DP_TX_AUDIO_CHANNELS:
  802. s->core_registers[offset] = value & 0x00000007;
  803. xlnx_dp_audio_activate(s);
  804. break;
  805. case DP_INT_STATUS:
  806. s->core_registers[DP_INT_STATUS] &= ~value;
  807. xlnx_dp_update_irq(s);
  808. break;
  809. case DP_INT_EN:
  810. s->core_registers[DP_INT_MASK] &= ~value;
  811. xlnx_dp_update_irq(s);
  812. break;
  813. case DP_INT_DS:
  814. s->core_registers[DP_INT_MASK] |= value;
  815. xlnx_dp_update_irq(s);
  816. break;
  817. default:
  818. assert(offset <= (0x504C >> 2));
  819. s->core_registers[offset] = value;
  820. break;
  821. }
  822. }
  823. static const MemoryRegionOps dp_ops = {
  824. .read = xlnx_dp_read,
  825. .write = xlnx_dp_write,
  826. .endianness = DEVICE_NATIVE_ENDIAN,
  827. .valid = {
  828. .min_access_size = 4,
  829. .max_access_size = 4,
  830. },
  831. .impl = {
  832. .min_access_size = 4,
  833. .max_access_size = 4,
  834. },
  835. };
  836. /*
  837. * This is to handle Read/Write to the Video Blender.
  838. */
  839. static void xlnx_dp_vblend_write(void *opaque, hwaddr offset,
  840. uint64_t value, unsigned size)
  841. {
  842. XlnxDPState *s = XLNX_DP(opaque);
  843. bool alpha_was_enabled;
  844. DPRINTF("vblend: write @0x%" HWADDR_PRIX " = 0x%" PRIX32 "\n", offset,
  845. (uint32_t)value);
  846. offset = offset >> 2;
  847. switch (offset) {
  848. case V_BLEND_BG_CLR_0:
  849. case V_BLEND_BG_CLR_1:
  850. case V_BLEND_BG_CLR_2:
  851. s->vblend_registers[offset] = value & 0x00000FFF;
  852. break;
  853. case V_BLEND_SET_GLOBAL_ALPHA_REG:
  854. /*
  855. * A write to this register can enable or disable blending. Thus we need
  856. * to recreate the surfaces.
  857. */
  858. alpha_was_enabled = xlnx_dp_global_alpha_enabled(s);
  859. s->vblend_registers[offset] = value & 0x000001FF;
  860. if (xlnx_dp_global_alpha_enabled(s) != alpha_was_enabled) {
  861. xlnx_dp_recreate_surface(s);
  862. }
  863. break;
  864. case V_BLEND_OUTPUT_VID_FORMAT:
  865. s->vblend_registers[offset] = value & 0x00000017;
  866. break;
  867. case V_BLEND_LAYER0_CONTROL:
  868. case V_BLEND_LAYER1_CONTROL:
  869. s->vblend_registers[offset] = value & 0x00000103;
  870. break;
  871. case V_BLEND_RGB2YCBCR_COEFF(0):
  872. case V_BLEND_RGB2YCBCR_COEFF(1):
  873. case V_BLEND_RGB2YCBCR_COEFF(2):
  874. case V_BLEND_RGB2YCBCR_COEFF(3):
  875. case V_BLEND_RGB2YCBCR_COEFF(4):
  876. case V_BLEND_RGB2YCBCR_COEFF(5):
  877. case V_BLEND_RGB2YCBCR_COEFF(6):
  878. case V_BLEND_RGB2YCBCR_COEFF(7):
  879. case V_BLEND_RGB2YCBCR_COEFF(8):
  880. case V_BLEND_IN1CSC_COEFF(0):
  881. case V_BLEND_IN1CSC_COEFF(1):
  882. case V_BLEND_IN1CSC_COEFF(2):
  883. case V_BLEND_IN1CSC_COEFF(3):
  884. case V_BLEND_IN1CSC_COEFF(4):
  885. case V_BLEND_IN1CSC_COEFF(5):
  886. case V_BLEND_IN1CSC_COEFF(6):
  887. case V_BLEND_IN1CSC_COEFF(7):
  888. case V_BLEND_IN1CSC_COEFF(8):
  889. case V_BLEND_IN2CSC_COEFF(0):
  890. case V_BLEND_IN2CSC_COEFF(1):
  891. case V_BLEND_IN2CSC_COEFF(2):
  892. case V_BLEND_IN2CSC_COEFF(3):
  893. case V_BLEND_IN2CSC_COEFF(4):
  894. case V_BLEND_IN2CSC_COEFF(5):
  895. case V_BLEND_IN2CSC_COEFF(6):
  896. case V_BLEND_IN2CSC_COEFF(7):
  897. case V_BLEND_IN2CSC_COEFF(8):
  898. s->vblend_registers[offset] = value & 0x0000FFFF;
  899. break;
  900. case V_BLEND_LUMA_IN1CSC_OFFSET:
  901. case V_BLEND_CR_IN1CSC_OFFSET:
  902. case V_BLEND_CB_IN1CSC_OFFSET:
  903. case V_BLEND_LUMA_IN2CSC_OFFSET:
  904. case V_BLEND_CR_IN2CSC_OFFSET:
  905. case V_BLEND_CB_IN2CSC_OFFSET:
  906. case V_BLEND_LUMA_OUTCSC_OFFSET:
  907. case V_BLEND_CR_OUTCSC_OFFSET:
  908. case V_BLEND_CB_OUTCSC_OFFSET:
  909. s->vblend_registers[offset] = value & 0x3FFF7FFF;
  910. break;
  911. case V_BLEND_CHROMA_KEY_ENABLE:
  912. s->vblend_registers[offset] = value & 0x00000003;
  913. break;
  914. case V_BLEND_CHROMA_KEY_COMP1:
  915. case V_BLEND_CHROMA_KEY_COMP2:
  916. case V_BLEND_CHROMA_KEY_COMP3:
  917. s->vblend_registers[offset] = value & 0x0FFF0FFF;
  918. break;
  919. default:
  920. s->vblend_registers[offset] = value;
  921. break;
  922. }
  923. }
  924. static uint64_t xlnx_dp_vblend_read(void *opaque, hwaddr offset,
  925. unsigned size)
  926. {
  927. XlnxDPState *s = XLNX_DP(opaque);
  928. DPRINTF("vblend: read @0x%" HWADDR_PRIX " = 0x%" PRIX32 "\n", offset,
  929. s->vblend_registers[offset >> 2]);
  930. return s->vblend_registers[offset >> 2];
  931. }
  932. static const MemoryRegionOps vblend_ops = {
  933. .read = xlnx_dp_vblend_read,
  934. .write = xlnx_dp_vblend_write,
  935. .endianness = DEVICE_NATIVE_ENDIAN,
  936. .valid = {
  937. .min_access_size = 4,
  938. .max_access_size = 4,
  939. },
  940. .impl = {
  941. .min_access_size = 4,
  942. .max_access_size = 4,
  943. },
  944. };
  945. /*
  946. * This is to handle Read/Write to the Audio Video buffer manager.
  947. */
  948. static void xlnx_dp_avbufm_write(void *opaque, hwaddr offset, uint64_t value,
  949. unsigned size)
  950. {
  951. XlnxDPState *s = XLNX_DP(opaque);
  952. DPRINTF("avbufm: write @0x%" HWADDR_PRIX " = 0x%" PRIX32 "\n", offset,
  953. (uint32_t)value);
  954. offset = offset >> 2;
  955. switch (offset) {
  956. case AV_BUF_FORMAT:
  957. s->avbufm_registers[offset] = value & 0x00000FFF;
  958. xlnx_dp_change_graphic_fmt(s);
  959. break;
  960. case AV_CHBUF0:
  961. case AV_CHBUF1:
  962. case AV_CHBUF2:
  963. case AV_CHBUF3:
  964. case AV_CHBUF4:
  965. case AV_CHBUF5:
  966. s->avbufm_registers[offset] = value & 0x0000007F;
  967. break;
  968. case AV_BUF_OUTPUT_AUDIO_VIDEO_SELECT:
  969. s->avbufm_registers[offset] = value & 0x0000007F;
  970. break;
  971. case AV_BUF_DITHER_CONFIG:
  972. s->avbufm_registers[offset] = value & 0x000007FF;
  973. break;
  974. case AV_BUF_DITHER_CONFIG_MAX:
  975. case AV_BUF_DITHER_CONFIG_MIN:
  976. s->avbufm_registers[offset] = value & 0x00000FFF;
  977. break;
  978. case AV_BUF_PATTERN_GEN_SELECT:
  979. s->avbufm_registers[offset] = value & 0xFFFFFF03;
  980. break;
  981. case AV_BUF_AUD_VID_CLK_SOURCE:
  982. s->avbufm_registers[offset] = value & 0x00000007;
  983. break;
  984. case AV_BUF_SRST_REG:
  985. s->avbufm_registers[offset] = value & 0x00000002;
  986. break;
  987. case AV_BUF_AUDIO_CH_CONFIG:
  988. s->avbufm_registers[offset] = value & 0x00000003;
  989. break;
  990. case AV_BUF_GRAPHICS_COMP_SCALE_FACTOR(0):
  991. case AV_BUF_GRAPHICS_COMP_SCALE_FACTOR(1):
  992. case AV_BUF_GRAPHICS_COMP_SCALE_FACTOR(2):
  993. case AV_BUF_VIDEO_COMP_SCALE_FACTOR(0):
  994. case AV_BUF_VIDEO_COMP_SCALE_FACTOR(1):
  995. case AV_BUF_VIDEO_COMP_SCALE_FACTOR(2):
  996. s->avbufm_registers[offset] = value & 0x0000FFFF;
  997. break;
  998. case AV_BUF_LIVE_VIDEO_COMP_SF(0):
  999. case AV_BUF_LIVE_VIDEO_COMP_SF(1):
  1000. case AV_BUF_LIVE_VIDEO_COMP_SF(2):
  1001. case AV_BUF_LIVE_VID_CONFIG:
  1002. case AV_BUF_LIVE_GFX_COMP_SF(0):
  1003. case AV_BUF_LIVE_GFX_COMP_SF(1):
  1004. case AV_BUF_LIVE_GFX_COMP_SF(2):
  1005. case AV_BUF_LIVE_GFX_CONFIG:
  1006. case AV_BUF_NON_LIVE_LATENCY:
  1007. case AV_BUF_STC_CONTROL:
  1008. case AV_BUF_STC_INIT_VALUE0:
  1009. case AV_BUF_STC_INIT_VALUE1:
  1010. case AV_BUF_STC_ADJ:
  1011. case AV_BUF_STC_VIDEO_VSYNC_TS_REG0:
  1012. case AV_BUF_STC_VIDEO_VSYNC_TS_REG1:
  1013. case AV_BUF_STC_EXT_VSYNC_TS_REG0:
  1014. case AV_BUF_STC_EXT_VSYNC_TS_REG1:
  1015. case AV_BUF_STC_CUSTOM_EVENT_TS_REG0:
  1016. case AV_BUF_STC_CUSTOM_EVENT_TS_REG1:
  1017. case AV_BUF_STC_CUSTOM_EVENT2_TS_REG0:
  1018. case AV_BUF_STC_CUSTOM_EVENT2_TS_REG1:
  1019. case AV_BUF_STC_SNAPSHOT0:
  1020. case AV_BUF_STC_SNAPSHOT1:
  1021. case AV_BUF_HCOUNT_VCOUNT_INT0:
  1022. case AV_BUF_HCOUNT_VCOUNT_INT1:
  1023. qemu_log_mask(LOG_UNIMP, "avbufm: unimplemented register 0x%04"
  1024. PRIx64 "\n",
  1025. offset << 2);
  1026. break;
  1027. default:
  1028. s->avbufm_registers[offset] = value;
  1029. break;
  1030. }
  1031. }
  1032. static uint64_t xlnx_dp_avbufm_read(void *opaque, hwaddr offset,
  1033. unsigned size)
  1034. {
  1035. XlnxDPState *s = XLNX_DP(opaque);
  1036. offset = offset >> 2;
  1037. return s->avbufm_registers[offset];
  1038. }
  1039. static const MemoryRegionOps avbufm_ops = {
  1040. .read = xlnx_dp_avbufm_read,
  1041. .write = xlnx_dp_avbufm_write,
  1042. .endianness = DEVICE_NATIVE_ENDIAN,
  1043. .valid = {
  1044. .min_access_size = 4,
  1045. .max_access_size = 4,
  1046. },
  1047. .impl = {
  1048. .min_access_size = 4,
  1049. .max_access_size = 4,
  1050. },
  1051. };
  1052. /*
  1053. * This is a global alpha blending using pixman.
  1054. * Both graphic and video planes are multiplied with the global alpha
  1055. * coefficient and added.
  1056. */
  1057. static inline void xlnx_dp_blend_surface(XlnxDPState *s)
  1058. {
  1059. pixman_fixed_t alpha1[] = { pixman_double_to_fixed(1),
  1060. pixman_double_to_fixed(1),
  1061. pixman_double_to_fixed(1.0) };
  1062. pixman_fixed_t alpha2[] = { pixman_double_to_fixed(1),
  1063. pixman_double_to_fixed(1),
  1064. pixman_double_to_fixed(1.0) };
  1065. if ((surface_width(s->g_plane.surface)
  1066. != surface_width(s->v_plane.surface)) ||
  1067. (surface_height(s->g_plane.surface)
  1068. != surface_height(s->v_plane.surface))) {
  1069. return;
  1070. }
  1071. alpha1[2] = pixman_double_to_fixed((double)(xlnx_dp_global_alpha_value(s))
  1072. / 256.0);
  1073. alpha2[2] = pixman_double_to_fixed((255.0
  1074. - (double)xlnx_dp_global_alpha_value(s))
  1075. / 256.0);
  1076. pixman_image_set_filter(s->g_plane.surface->image,
  1077. PIXMAN_FILTER_CONVOLUTION, alpha1, 3);
  1078. pixman_image_composite(PIXMAN_OP_SRC, s->g_plane.surface->image, 0,
  1079. s->bout_plane.surface->image, 0, 0, 0, 0, 0, 0,
  1080. surface_width(s->g_plane.surface),
  1081. surface_height(s->g_plane.surface));
  1082. pixman_image_set_filter(s->v_plane.surface->image,
  1083. PIXMAN_FILTER_CONVOLUTION, alpha2, 3);
  1084. pixman_image_composite(PIXMAN_OP_ADD, s->v_plane.surface->image, 0,
  1085. s->bout_plane.surface->image, 0, 0, 0, 0, 0, 0,
  1086. surface_width(s->g_plane.surface),
  1087. surface_height(s->g_plane.surface));
  1088. }
  1089. static void xlnx_dp_update_display(void *opaque)
  1090. {
  1091. XlnxDPState *s = XLNX_DP(opaque);
  1092. if ((s->core_registers[DP_TRANSMITTER_ENABLE] & 0x01) == 0) {
  1093. return;
  1094. }
  1095. xlnx_dpdma_trigger_vsync_irq(s->dpdma);
  1096. /*
  1097. * Trigger the DMA channel.
  1098. */
  1099. if (!xlnx_dpdma_start_operation(s->dpdma, 3, false)) {
  1100. /*
  1101. * An error occurred don't do anything with the data..
  1102. * Trigger an underflow interrupt.
  1103. */
  1104. s->core_registers[DP_INT_STATUS] |= (1 << 21);
  1105. xlnx_dp_update_irq(s);
  1106. return;
  1107. }
  1108. if (xlnx_dp_global_alpha_enabled(s)) {
  1109. if (!xlnx_dpdma_start_operation(s->dpdma, 0, false)) {
  1110. s->core_registers[DP_INT_STATUS] |= (1 << 21);
  1111. xlnx_dp_update_irq(s);
  1112. return;
  1113. }
  1114. xlnx_dp_blend_surface(s);
  1115. }
  1116. /*
  1117. * XXX: We might want to update only what changed.
  1118. */
  1119. dpy_gfx_update_full(s->console);
  1120. }
  1121. static const GraphicHwOps xlnx_dp_gfx_ops = {
  1122. .gfx_update = xlnx_dp_update_display,
  1123. };
  1124. static void xlnx_dp_init(Object *obj)
  1125. {
  1126. SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
  1127. XlnxDPState *s = XLNX_DP(obj);
  1128. memory_region_init(&s->container, obj, TYPE_XLNX_DP, DP_CONTAINER_SIZE);
  1129. memory_region_init_io(&s->core_iomem, obj, &dp_ops, s, TYPE_XLNX_DP
  1130. ".core", sizeof(s->core_registers));
  1131. memory_region_add_subregion(&s->container, DP_CORE_REG_OFFSET,
  1132. &s->core_iomem);
  1133. memory_region_init_io(&s->vblend_iomem, obj, &vblend_ops, s, TYPE_XLNX_DP
  1134. ".v_blend", sizeof(s->vblend_registers));
  1135. memory_region_add_subregion(&s->container, DP_VBLEND_REG_OFFSET,
  1136. &s->vblend_iomem);
  1137. memory_region_init_io(&s->avbufm_iomem, obj, &avbufm_ops, s, TYPE_XLNX_DP
  1138. ".av_buffer_manager", sizeof(s->avbufm_registers));
  1139. memory_region_add_subregion(&s->container, DP_AVBUF_REG_OFFSET,
  1140. &s->avbufm_iomem);
  1141. memory_region_init_io(&s->audio_iomem, obj, &audio_ops, s, TYPE_XLNX_DP
  1142. ".audio", sizeof(s->audio_registers));
  1143. memory_region_add_subregion(&s->container, 0xC000, &s->audio_iomem);
  1144. sysbus_init_mmio(sbd, &s->container);
  1145. sysbus_init_irq(sbd, &s->irq);
  1146. object_property_add_link(obj, "dpdma", TYPE_XLNX_DPDMA,
  1147. (Object **) &s->dpdma,
  1148. xlnx_dp_set_dpdma,
  1149. OBJ_PROP_LINK_STRONG);
  1150. /*
  1151. * Initialize AUX Bus.
  1152. */
  1153. s->aux_bus = aux_bus_init(DEVICE(obj), "aux");
  1154. /*
  1155. * Initialize DPCD and EDID..
  1156. */
  1157. s->dpcd = DPCD(qdev_new("dpcd"));
  1158. object_property_add_child(OBJECT(s), "dpcd", OBJECT(s->dpcd));
  1159. s->edid = I2CDDC(qdev_new("i2c-ddc"));
  1160. i2c_slave_set_address(I2C_SLAVE(s->edid), 0x50);
  1161. object_property_add_child(OBJECT(s), "edid", OBJECT(s->edid));
  1162. fifo8_create(&s->rx_fifo, 16);
  1163. fifo8_create(&s->tx_fifo, 16);
  1164. }
  1165. static void xlnx_dp_finalize(Object *obj)
  1166. {
  1167. XlnxDPState *s = XLNX_DP(obj);
  1168. fifo8_destroy(&s->tx_fifo);
  1169. fifo8_destroy(&s->rx_fifo);
  1170. }
  1171. static void vblank_hit(void *opaque)
  1172. {
  1173. XlnxDPState *s = XLNX_DP(opaque);
  1174. s->core_registers[DP_INT_STATUS] |= DP_INT_VBLNK_START;
  1175. xlnx_dp_update_irq(s);
  1176. }
  1177. static void xlnx_dp_realize(DeviceState *dev, Error **errp)
  1178. {
  1179. XlnxDPState *s = XLNX_DP(dev);
  1180. DisplaySurface *surface;
  1181. struct audsettings as;
  1182. aux_bus_realize(s->aux_bus);
  1183. qdev_realize(DEVICE(s->dpcd), BUS(s->aux_bus), &error_fatal);
  1184. aux_map_slave(AUX_SLAVE(s->dpcd), 0x0000);
  1185. qdev_realize_and_unref(DEVICE(s->edid), BUS(aux_get_i2c_bus(s->aux_bus)),
  1186. &error_fatal);
  1187. s->console = graphic_console_init(dev, 0, &xlnx_dp_gfx_ops, s);
  1188. surface = qemu_console_surface(s->console);
  1189. xlnx_dpdma_set_host_data_location(s->dpdma, DP_GRAPHIC_DMA_CHANNEL,
  1190. surface_data(surface));
  1191. as.freq = 44100;
  1192. as.nchannels = 2;
  1193. as.fmt = AUDIO_FORMAT_S16;
  1194. as.endianness = 0;
  1195. AUD_register_card("xlnx_dp.audio", &s->aud_card);
  1196. s->amixer_output_stream = AUD_open_out(&s->aud_card,
  1197. s->amixer_output_stream,
  1198. "xlnx_dp.audio.out",
  1199. s,
  1200. xlnx_dp_audio_callback,
  1201. &as);
  1202. AUD_set_volume_out(s->amixer_output_stream, 0, 255, 255);
  1203. xlnx_dp_audio_activate(s);
  1204. s->vblank = ptimer_init(vblank_hit, s, DP_VBLANK_PTIMER_POLICY);
  1205. ptimer_transaction_begin(s->vblank);
  1206. ptimer_set_freq(s->vblank, 30);
  1207. ptimer_transaction_commit(s->vblank);
  1208. }
  1209. static void xlnx_dp_reset(DeviceState *dev)
  1210. {
  1211. XlnxDPState *s = XLNX_DP(dev);
  1212. memset(s->core_registers, 0, sizeof(s->core_registers));
  1213. s->core_registers[DP_VERSION_REGISTER] = 0x04010000;
  1214. s->core_registers[DP_CORE_ID] = 0x01020000;
  1215. s->core_registers[DP_REPLY_STATUS] = 0x00000010;
  1216. s->core_registers[DP_MSA_TRANSFER_UNIT_SIZE] = 0x00000040;
  1217. s->core_registers[DP_INIT_WAIT] = 0x00000020;
  1218. s->core_registers[DP_PHY_RESET] = 0x00010003;
  1219. s->core_registers[DP_INT_MASK] = 0xFFFFF03F;
  1220. s->core_registers[DP_PHY_STATUS] = 0x00000043;
  1221. s->core_registers[DP_INTERRUPT_SIGNAL_STATE] = 0x00000001;
  1222. s->vblend_registers[V_BLEND_RGB2YCBCR_COEFF(0)] = 0x00001000;
  1223. s->vblend_registers[V_BLEND_RGB2YCBCR_COEFF(4)] = 0x00001000;
  1224. s->vblend_registers[V_BLEND_RGB2YCBCR_COEFF(8)] = 0x00001000;
  1225. s->vblend_registers[V_BLEND_IN1CSC_COEFF(0)] = 0x00001000;
  1226. s->vblend_registers[V_BLEND_IN1CSC_COEFF(4)] = 0x00001000;
  1227. s->vblend_registers[V_BLEND_IN1CSC_COEFF(8)] = 0x00001000;
  1228. s->vblend_registers[V_BLEND_IN2CSC_COEFF(0)] = 0x00001000;
  1229. s->vblend_registers[V_BLEND_IN2CSC_COEFF(4)] = 0x00001000;
  1230. s->vblend_registers[V_BLEND_IN2CSC_COEFF(8)] = 0x00001000;
  1231. s->avbufm_registers[AV_BUF_NON_LIVE_LATENCY] = 0x00000180;
  1232. s->avbufm_registers[AV_BUF_OUTPUT_AUDIO_VIDEO_SELECT] = 0x00000008;
  1233. s->avbufm_registers[AV_BUF_DITHER_CONFIG_MAX] = 0x00000FFF;
  1234. s->avbufm_registers[AV_BUF_GRAPHICS_COMP_SCALE_FACTOR(0)] = 0x00010101;
  1235. s->avbufm_registers[AV_BUF_GRAPHICS_COMP_SCALE_FACTOR(1)] = 0x00010101;
  1236. s->avbufm_registers[AV_BUF_GRAPHICS_COMP_SCALE_FACTOR(2)] = 0x00010101;
  1237. s->avbufm_registers[AV_BUF_VIDEO_COMP_SCALE_FACTOR(0)] = 0x00010101;
  1238. s->avbufm_registers[AV_BUF_VIDEO_COMP_SCALE_FACTOR(1)] = 0x00010101;
  1239. s->avbufm_registers[AV_BUF_VIDEO_COMP_SCALE_FACTOR(2)] = 0x00010101;
  1240. s->avbufm_registers[AV_BUF_LIVE_VIDEO_COMP_SF(0)] = 0x00010101;
  1241. s->avbufm_registers[AV_BUF_LIVE_VIDEO_COMP_SF(1)] = 0x00010101;
  1242. s->avbufm_registers[AV_BUF_LIVE_VIDEO_COMP_SF(2)] = 0x00010101;
  1243. s->avbufm_registers[AV_BUF_LIVE_GFX_COMP_SF(0)] = 0x00010101;
  1244. s->avbufm_registers[AV_BUF_LIVE_GFX_COMP_SF(1)] = 0x00010101;
  1245. s->avbufm_registers[AV_BUF_LIVE_GFX_COMP_SF(2)] = 0x00010101;
  1246. memset(s->audio_registers, 0, sizeof(s->audio_registers));
  1247. s->byte_left = 0;
  1248. xlnx_dp_aux_clear_rx_fifo(s);
  1249. xlnx_dp_change_graphic_fmt(s);
  1250. xlnx_dp_update_irq(s);
  1251. }
  1252. static void xlnx_dp_class_init(ObjectClass *oc, void *data)
  1253. {
  1254. DeviceClass *dc = DEVICE_CLASS(oc);
  1255. dc->realize = xlnx_dp_realize;
  1256. dc->vmsd = &vmstate_dp;
  1257. dc->reset = xlnx_dp_reset;
  1258. }
  1259. static const TypeInfo xlnx_dp_info = {
  1260. .name = TYPE_XLNX_DP,
  1261. .parent = TYPE_SYS_BUS_DEVICE,
  1262. .instance_size = sizeof(XlnxDPState),
  1263. .instance_init = xlnx_dp_init,
  1264. .instance_finalize = xlnx_dp_finalize,
  1265. .class_init = xlnx_dp_class_init,
  1266. };
  1267. static void xlnx_dp_register_types(void)
  1268. {
  1269. type_register_static(&xlnx_dp_info);
  1270. }
  1271. type_init(xlnx_dp_register_types)