xlnx_dp.c 47 KB

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