2
0

xlnx_dp.c 48 KB

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