2
0

xlnx_dp.c 46 KB

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