radeon_display.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194
  1. /*
  2. * Copyright 2007-8 Advanced Micro Devices, Inc.
  3. * Copyright 2008 Red Hat Inc.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  19. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  20. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. * OTHER DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors: Dave Airlie
  24. * Alex Deucher
  25. */
  26. #include "drmP.h"
  27. #include "radeon_drm.h"
  28. #include "radeon.h"
  29. #include "atom.h"
  30. #include <asm/div64.h>
  31. #include "drm_crtc_helper.h"
  32. #include "drm_edid.h"
  33. static int radeon_ddc_dump(struct drm_connector *connector);
  34. static void avivo_crtc_load_lut(struct drm_crtc *crtc)
  35. {
  36. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  37. struct drm_device *dev = crtc->dev;
  38. struct radeon_device *rdev = dev->dev_private;
  39. int i;
  40. DRM_DEBUG_KMS("%d\n", radeon_crtc->crtc_id);
  41. WREG32(AVIVO_DC_LUTA_CONTROL + radeon_crtc->crtc_offset, 0);
  42. WREG32(AVIVO_DC_LUTA_BLACK_OFFSET_BLUE + radeon_crtc->crtc_offset, 0);
  43. WREG32(AVIVO_DC_LUTA_BLACK_OFFSET_GREEN + radeon_crtc->crtc_offset, 0);
  44. WREG32(AVIVO_DC_LUTA_BLACK_OFFSET_RED + radeon_crtc->crtc_offset, 0);
  45. WREG32(AVIVO_DC_LUTA_WHITE_OFFSET_BLUE + radeon_crtc->crtc_offset, 0xffff);
  46. WREG32(AVIVO_DC_LUTA_WHITE_OFFSET_GREEN + radeon_crtc->crtc_offset, 0xffff);
  47. WREG32(AVIVO_DC_LUTA_WHITE_OFFSET_RED + radeon_crtc->crtc_offset, 0xffff);
  48. WREG32(AVIVO_DC_LUT_RW_SELECT, radeon_crtc->crtc_id);
  49. WREG32(AVIVO_DC_LUT_RW_MODE, 0);
  50. WREG32(AVIVO_DC_LUT_WRITE_EN_MASK, 0x0000003f);
  51. WREG8(AVIVO_DC_LUT_RW_INDEX, 0);
  52. for (i = 0; i < 256; i++) {
  53. WREG32(AVIVO_DC_LUT_30_COLOR,
  54. (radeon_crtc->lut_r[i] << 20) |
  55. (radeon_crtc->lut_g[i] << 10) |
  56. (radeon_crtc->lut_b[i] << 0));
  57. }
  58. WREG32(AVIVO_D1GRPH_LUT_SEL + radeon_crtc->crtc_offset, radeon_crtc->crtc_id);
  59. }
  60. static void evergreen_crtc_load_lut(struct drm_crtc *crtc)
  61. {
  62. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  63. struct drm_device *dev = crtc->dev;
  64. struct radeon_device *rdev = dev->dev_private;
  65. int i;
  66. DRM_DEBUG_KMS("%d\n", radeon_crtc->crtc_id);
  67. WREG32(EVERGREEN_DC_LUT_CONTROL + radeon_crtc->crtc_offset, 0);
  68. WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_BLUE + radeon_crtc->crtc_offset, 0);
  69. WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_GREEN + radeon_crtc->crtc_offset, 0);
  70. WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_RED + radeon_crtc->crtc_offset, 0);
  71. WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_BLUE + radeon_crtc->crtc_offset, 0xffff);
  72. WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_GREEN + radeon_crtc->crtc_offset, 0xffff);
  73. WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_RED + radeon_crtc->crtc_offset, 0xffff);
  74. WREG32(EVERGREEN_DC_LUT_RW_MODE + radeon_crtc->crtc_offset, 0);
  75. WREG32(EVERGREEN_DC_LUT_WRITE_EN_MASK + radeon_crtc->crtc_offset, 0x00000007);
  76. WREG32(EVERGREEN_DC_LUT_RW_INDEX + radeon_crtc->crtc_offset, 0);
  77. for (i = 0; i < 256; i++) {
  78. WREG32(EVERGREEN_DC_LUT_30_COLOR + radeon_crtc->crtc_offset,
  79. (radeon_crtc->lut_r[i] << 20) |
  80. (radeon_crtc->lut_g[i] << 10) |
  81. (radeon_crtc->lut_b[i] << 0));
  82. }
  83. }
  84. static void legacy_crtc_load_lut(struct drm_crtc *crtc)
  85. {
  86. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  87. struct drm_device *dev = crtc->dev;
  88. struct radeon_device *rdev = dev->dev_private;
  89. int i;
  90. uint32_t dac2_cntl;
  91. dac2_cntl = RREG32(RADEON_DAC_CNTL2);
  92. if (radeon_crtc->crtc_id == 0)
  93. dac2_cntl &= (uint32_t)~RADEON_DAC2_PALETTE_ACC_CTL;
  94. else
  95. dac2_cntl |= RADEON_DAC2_PALETTE_ACC_CTL;
  96. WREG32(RADEON_DAC_CNTL2, dac2_cntl);
  97. WREG8(RADEON_PALETTE_INDEX, 0);
  98. for (i = 0; i < 256; i++) {
  99. WREG32(RADEON_PALETTE_30_DATA,
  100. (radeon_crtc->lut_r[i] << 20) |
  101. (radeon_crtc->lut_g[i] << 10) |
  102. (radeon_crtc->lut_b[i] << 0));
  103. }
  104. }
  105. void radeon_crtc_load_lut(struct drm_crtc *crtc)
  106. {
  107. struct drm_device *dev = crtc->dev;
  108. struct radeon_device *rdev = dev->dev_private;
  109. if (!crtc->enabled)
  110. return;
  111. if (ASIC_IS_DCE4(rdev))
  112. evergreen_crtc_load_lut(crtc);
  113. else if (ASIC_IS_AVIVO(rdev))
  114. avivo_crtc_load_lut(crtc);
  115. else
  116. legacy_crtc_load_lut(crtc);
  117. }
  118. /** Sets the color ramps on behalf of fbcon */
  119. void radeon_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  120. u16 blue, int regno)
  121. {
  122. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  123. radeon_crtc->lut_r[regno] = red >> 6;
  124. radeon_crtc->lut_g[regno] = green >> 6;
  125. radeon_crtc->lut_b[regno] = blue >> 6;
  126. }
  127. /** Gets the color ramps on behalf of fbcon */
  128. void radeon_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
  129. u16 *blue, int regno)
  130. {
  131. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  132. *red = radeon_crtc->lut_r[regno] << 6;
  133. *green = radeon_crtc->lut_g[regno] << 6;
  134. *blue = radeon_crtc->lut_b[regno] << 6;
  135. }
  136. static void radeon_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  137. u16 *blue, uint32_t start, uint32_t size)
  138. {
  139. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  140. int end = (start + size > 256) ? 256 : start + size, i;
  141. /* userspace palettes are always correct as is */
  142. for (i = start; i < end; i++) {
  143. radeon_crtc->lut_r[i] = red[i] >> 6;
  144. radeon_crtc->lut_g[i] = green[i] >> 6;
  145. radeon_crtc->lut_b[i] = blue[i] >> 6;
  146. }
  147. radeon_crtc_load_lut(crtc);
  148. }
  149. static void radeon_crtc_destroy(struct drm_crtc *crtc)
  150. {
  151. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  152. drm_crtc_cleanup(crtc);
  153. kfree(radeon_crtc);
  154. }
  155. static const struct drm_crtc_funcs radeon_crtc_funcs = {
  156. .cursor_set = radeon_crtc_cursor_set,
  157. .cursor_move = radeon_crtc_cursor_move,
  158. .gamma_set = radeon_crtc_gamma_set,
  159. .set_config = drm_crtc_helper_set_config,
  160. .destroy = radeon_crtc_destroy,
  161. };
  162. static void radeon_crtc_init(struct drm_device *dev, int index)
  163. {
  164. struct radeon_device *rdev = dev->dev_private;
  165. struct radeon_crtc *radeon_crtc;
  166. int i;
  167. radeon_crtc = kzalloc(sizeof(struct radeon_crtc) + (RADEONFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
  168. if (radeon_crtc == NULL)
  169. return;
  170. drm_crtc_init(dev, &radeon_crtc->base, &radeon_crtc_funcs);
  171. drm_mode_crtc_set_gamma_size(&radeon_crtc->base, 256);
  172. radeon_crtc->crtc_id = index;
  173. rdev->mode_info.crtcs[index] = radeon_crtc;
  174. #if 0
  175. radeon_crtc->mode_set.crtc = &radeon_crtc->base;
  176. radeon_crtc->mode_set.connectors = (struct drm_connector **)(radeon_crtc + 1);
  177. radeon_crtc->mode_set.num_connectors = 0;
  178. #endif
  179. for (i = 0; i < 256; i++) {
  180. radeon_crtc->lut_r[i] = i << 2;
  181. radeon_crtc->lut_g[i] = i << 2;
  182. radeon_crtc->lut_b[i] = i << 2;
  183. }
  184. if (rdev->is_atom_bios && (ASIC_IS_AVIVO(rdev) || radeon_r4xx_atom))
  185. radeon_atombios_init_crtc(dev, radeon_crtc);
  186. else
  187. radeon_legacy_init_crtc(dev, radeon_crtc);
  188. }
  189. static const char *encoder_names[34] = {
  190. "NONE",
  191. "INTERNAL_LVDS",
  192. "INTERNAL_TMDS1",
  193. "INTERNAL_TMDS2",
  194. "INTERNAL_DAC1",
  195. "INTERNAL_DAC2",
  196. "INTERNAL_SDVOA",
  197. "INTERNAL_SDVOB",
  198. "SI170B",
  199. "CH7303",
  200. "CH7301",
  201. "INTERNAL_DVO1",
  202. "EXTERNAL_SDVOA",
  203. "EXTERNAL_SDVOB",
  204. "TITFP513",
  205. "INTERNAL_LVTM1",
  206. "VT1623",
  207. "HDMI_SI1930",
  208. "HDMI_INTERNAL",
  209. "INTERNAL_KLDSCP_TMDS1",
  210. "INTERNAL_KLDSCP_DVO1",
  211. "INTERNAL_KLDSCP_DAC1",
  212. "INTERNAL_KLDSCP_DAC2",
  213. "SI178",
  214. "MVPU_FPGA",
  215. "INTERNAL_DDI",
  216. "VT1625",
  217. "HDMI_SI1932",
  218. "DP_AN9801",
  219. "DP_DP501",
  220. "INTERNAL_UNIPHY",
  221. "INTERNAL_KLDSCP_LVTMA",
  222. "INTERNAL_UNIPHY1",
  223. "INTERNAL_UNIPHY2",
  224. };
  225. static const char *connector_names[15] = {
  226. "Unknown",
  227. "VGA",
  228. "DVI-I",
  229. "DVI-D",
  230. "DVI-A",
  231. "Composite",
  232. "S-video",
  233. "LVDS",
  234. "Component",
  235. "DIN",
  236. "DisplayPort",
  237. "HDMI-A",
  238. "HDMI-B",
  239. "TV",
  240. "eDP",
  241. };
  242. static const char *hpd_names[6] = {
  243. "HPD1",
  244. "HPD2",
  245. "HPD3",
  246. "HPD4",
  247. "HPD5",
  248. "HPD6",
  249. };
  250. static void radeon_print_display_setup(struct drm_device *dev)
  251. {
  252. struct drm_connector *connector;
  253. struct radeon_connector *radeon_connector;
  254. struct drm_encoder *encoder;
  255. struct radeon_encoder *radeon_encoder;
  256. uint32_t devices;
  257. int i = 0;
  258. DRM_INFO("Radeon Display Connectors\n");
  259. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  260. radeon_connector = to_radeon_connector(connector);
  261. DRM_INFO("Connector %d:\n", i);
  262. DRM_INFO(" %s\n", connector_names[connector->connector_type]);
  263. if (radeon_connector->hpd.hpd != RADEON_HPD_NONE)
  264. DRM_INFO(" %s\n", hpd_names[radeon_connector->hpd.hpd]);
  265. if (radeon_connector->ddc_bus) {
  266. DRM_INFO(" DDC: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
  267. radeon_connector->ddc_bus->rec.mask_clk_reg,
  268. radeon_connector->ddc_bus->rec.mask_data_reg,
  269. radeon_connector->ddc_bus->rec.a_clk_reg,
  270. radeon_connector->ddc_bus->rec.a_data_reg,
  271. radeon_connector->ddc_bus->rec.en_clk_reg,
  272. radeon_connector->ddc_bus->rec.en_data_reg,
  273. radeon_connector->ddc_bus->rec.y_clk_reg,
  274. radeon_connector->ddc_bus->rec.y_data_reg);
  275. if (radeon_connector->router_bus)
  276. DRM_INFO(" DDC Router 0x%x/0x%x\n",
  277. radeon_connector->router.mux_control_pin,
  278. radeon_connector->router.mux_state);
  279. } else {
  280. if (connector->connector_type == DRM_MODE_CONNECTOR_VGA ||
  281. connector->connector_type == DRM_MODE_CONNECTOR_DVII ||
  282. connector->connector_type == DRM_MODE_CONNECTOR_DVID ||
  283. connector->connector_type == DRM_MODE_CONNECTOR_DVIA ||
  284. connector->connector_type == DRM_MODE_CONNECTOR_HDMIA ||
  285. connector->connector_type == DRM_MODE_CONNECTOR_HDMIB)
  286. DRM_INFO(" DDC: no ddc bus - possible BIOS bug - please report to xorg-driver-ati@lists.x.org\n");
  287. }
  288. DRM_INFO(" Encoders:\n");
  289. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  290. radeon_encoder = to_radeon_encoder(encoder);
  291. devices = radeon_encoder->devices & radeon_connector->devices;
  292. if (devices) {
  293. if (devices & ATOM_DEVICE_CRT1_SUPPORT)
  294. DRM_INFO(" CRT1: %s\n", encoder_names[radeon_encoder->encoder_id]);
  295. if (devices & ATOM_DEVICE_CRT2_SUPPORT)
  296. DRM_INFO(" CRT2: %s\n", encoder_names[radeon_encoder->encoder_id]);
  297. if (devices & ATOM_DEVICE_LCD1_SUPPORT)
  298. DRM_INFO(" LCD1: %s\n", encoder_names[radeon_encoder->encoder_id]);
  299. if (devices & ATOM_DEVICE_DFP1_SUPPORT)
  300. DRM_INFO(" DFP1: %s\n", encoder_names[radeon_encoder->encoder_id]);
  301. if (devices & ATOM_DEVICE_DFP2_SUPPORT)
  302. DRM_INFO(" DFP2: %s\n", encoder_names[radeon_encoder->encoder_id]);
  303. if (devices & ATOM_DEVICE_DFP3_SUPPORT)
  304. DRM_INFO(" DFP3: %s\n", encoder_names[radeon_encoder->encoder_id]);
  305. if (devices & ATOM_DEVICE_DFP4_SUPPORT)
  306. DRM_INFO(" DFP4: %s\n", encoder_names[radeon_encoder->encoder_id]);
  307. if (devices & ATOM_DEVICE_DFP5_SUPPORT)
  308. DRM_INFO(" DFP5: %s\n", encoder_names[radeon_encoder->encoder_id]);
  309. if (devices & ATOM_DEVICE_TV1_SUPPORT)
  310. DRM_INFO(" TV1: %s\n", encoder_names[radeon_encoder->encoder_id]);
  311. if (devices & ATOM_DEVICE_CV_SUPPORT)
  312. DRM_INFO(" CV: %s\n", encoder_names[radeon_encoder->encoder_id]);
  313. }
  314. }
  315. i++;
  316. }
  317. }
  318. static bool radeon_setup_enc_conn(struct drm_device *dev)
  319. {
  320. struct radeon_device *rdev = dev->dev_private;
  321. struct drm_connector *drm_connector;
  322. bool ret = false;
  323. if (rdev->bios) {
  324. if (rdev->is_atom_bios) {
  325. ret = radeon_get_atom_connector_info_from_supported_devices_table(dev);
  326. if (ret == false)
  327. ret = radeon_get_atom_connector_info_from_object_table(dev);
  328. } else {
  329. ret = radeon_get_legacy_connector_info_from_bios(dev);
  330. if (ret == false)
  331. ret = radeon_get_legacy_connector_info_from_table(dev);
  332. }
  333. } else {
  334. if (!ASIC_IS_AVIVO(rdev))
  335. ret = radeon_get_legacy_connector_info_from_table(dev);
  336. }
  337. if (ret) {
  338. radeon_setup_encoder_clones(dev);
  339. radeon_print_display_setup(dev);
  340. list_for_each_entry(drm_connector, &dev->mode_config.connector_list, head)
  341. radeon_ddc_dump(drm_connector);
  342. }
  343. return ret;
  344. }
  345. int radeon_ddc_get_modes(struct radeon_connector *radeon_connector)
  346. {
  347. struct drm_device *dev = radeon_connector->base.dev;
  348. struct radeon_device *rdev = dev->dev_private;
  349. int ret = 0;
  350. /* on hw with routers, select right port */
  351. if (radeon_connector->router.valid)
  352. radeon_router_select_port(radeon_connector);
  353. if ((radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_DisplayPort) ||
  354. (radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_eDP)) {
  355. struct radeon_connector_atom_dig *dig = radeon_connector->con_priv;
  356. if ((dig->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT ||
  357. dig->dp_sink_type == CONNECTOR_OBJECT_ID_eDP) && dig->dp_i2c_bus)
  358. radeon_connector->edid = drm_get_edid(&radeon_connector->base, &dig->dp_i2c_bus->adapter);
  359. }
  360. if (!radeon_connector->ddc_bus)
  361. return -1;
  362. if (!radeon_connector->edid) {
  363. radeon_connector->edid = drm_get_edid(&radeon_connector->base, &radeon_connector->ddc_bus->adapter);
  364. }
  365. /* some servers provide a hardcoded edid in rom for KVMs */
  366. if (!radeon_connector->edid)
  367. radeon_connector->edid = radeon_combios_get_hardcoded_edid(rdev);
  368. if (radeon_connector->edid) {
  369. drm_mode_connector_update_edid_property(&radeon_connector->base, radeon_connector->edid);
  370. ret = drm_add_edid_modes(&radeon_connector->base, radeon_connector->edid);
  371. return ret;
  372. }
  373. drm_mode_connector_update_edid_property(&radeon_connector->base, NULL);
  374. return 0;
  375. }
  376. static int radeon_ddc_dump(struct drm_connector *connector)
  377. {
  378. struct edid *edid;
  379. struct radeon_connector *radeon_connector = to_radeon_connector(connector);
  380. int ret = 0;
  381. /* on hw with routers, select right port */
  382. if (radeon_connector->router.valid)
  383. radeon_router_select_port(radeon_connector);
  384. if (!radeon_connector->ddc_bus)
  385. return -1;
  386. edid = drm_get_edid(connector, &radeon_connector->ddc_bus->adapter);
  387. if (edid) {
  388. kfree(edid);
  389. }
  390. return ret;
  391. }
  392. static inline uint32_t radeon_div(uint64_t n, uint32_t d)
  393. {
  394. uint64_t mod;
  395. n += d / 2;
  396. mod = do_div(n, d);
  397. return n;
  398. }
  399. static void radeon_compute_pll_legacy(struct radeon_pll *pll,
  400. uint64_t freq,
  401. uint32_t *dot_clock_p,
  402. uint32_t *fb_div_p,
  403. uint32_t *frac_fb_div_p,
  404. uint32_t *ref_div_p,
  405. uint32_t *post_div_p)
  406. {
  407. uint32_t min_ref_div = pll->min_ref_div;
  408. uint32_t max_ref_div = pll->max_ref_div;
  409. uint32_t min_post_div = pll->min_post_div;
  410. uint32_t max_post_div = pll->max_post_div;
  411. uint32_t min_fractional_feed_div = 0;
  412. uint32_t max_fractional_feed_div = 0;
  413. uint32_t best_vco = pll->best_vco;
  414. uint32_t best_post_div = 1;
  415. uint32_t best_ref_div = 1;
  416. uint32_t best_feedback_div = 1;
  417. uint32_t best_frac_feedback_div = 0;
  418. uint32_t best_freq = -1;
  419. uint32_t best_error = 0xffffffff;
  420. uint32_t best_vco_diff = 1;
  421. uint32_t post_div;
  422. u32 pll_out_min, pll_out_max;
  423. DRM_DEBUG_KMS("PLL freq %llu %u %u\n", freq, pll->min_ref_div, pll->max_ref_div);
  424. freq = freq * 1000;
  425. if (pll->flags & RADEON_PLL_IS_LCD) {
  426. pll_out_min = pll->lcd_pll_out_min;
  427. pll_out_max = pll->lcd_pll_out_max;
  428. } else {
  429. pll_out_min = pll->pll_out_min;
  430. pll_out_max = pll->pll_out_max;
  431. }
  432. if (pll->flags & RADEON_PLL_USE_REF_DIV)
  433. min_ref_div = max_ref_div = pll->reference_div;
  434. else {
  435. while (min_ref_div < max_ref_div-1) {
  436. uint32_t mid = (min_ref_div + max_ref_div) / 2;
  437. uint32_t pll_in = pll->reference_freq / mid;
  438. if (pll_in < pll->pll_in_min)
  439. max_ref_div = mid;
  440. else if (pll_in > pll->pll_in_max)
  441. min_ref_div = mid;
  442. else
  443. break;
  444. }
  445. }
  446. if (pll->flags & RADEON_PLL_USE_POST_DIV)
  447. min_post_div = max_post_div = pll->post_div;
  448. if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
  449. min_fractional_feed_div = pll->min_frac_feedback_div;
  450. max_fractional_feed_div = pll->max_frac_feedback_div;
  451. }
  452. for (post_div = min_post_div; post_div <= max_post_div; ++post_div) {
  453. uint32_t ref_div;
  454. if ((pll->flags & RADEON_PLL_NO_ODD_POST_DIV) && (post_div & 1))
  455. continue;
  456. /* legacy radeons only have a few post_divs */
  457. if (pll->flags & RADEON_PLL_LEGACY) {
  458. if ((post_div == 5) ||
  459. (post_div == 7) ||
  460. (post_div == 9) ||
  461. (post_div == 10) ||
  462. (post_div == 11) ||
  463. (post_div == 13) ||
  464. (post_div == 14) ||
  465. (post_div == 15))
  466. continue;
  467. }
  468. for (ref_div = min_ref_div; ref_div <= max_ref_div; ++ref_div) {
  469. uint32_t feedback_div, current_freq = 0, error, vco_diff;
  470. uint32_t pll_in = pll->reference_freq / ref_div;
  471. uint32_t min_feed_div = pll->min_feedback_div;
  472. uint32_t max_feed_div = pll->max_feedback_div + 1;
  473. if (pll_in < pll->pll_in_min || pll_in > pll->pll_in_max)
  474. continue;
  475. while (min_feed_div < max_feed_div) {
  476. uint32_t vco;
  477. uint32_t min_frac_feed_div = min_fractional_feed_div;
  478. uint32_t max_frac_feed_div = max_fractional_feed_div + 1;
  479. uint32_t frac_feedback_div;
  480. uint64_t tmp;
  481. feedback_div = (min_feed_div + max_feed_div) / 2;
  482. tmp = (uint64_t)pll->reference_freq * feedback_div;
  483. vco = radeon_div(tmp, ref_div);
  484. if (vco < pll_out_min) {
  485. min_feed_div = feedback_div + 1;
  486. continue;
  487. } else if (vco > pll_out_max) {
  488. max_feed_div = feedback_div;
  489. continue;
  490. }
  491. while (min_frac_feed_div < max_frac_feed_div) {
  492. frac_feedback_div = (min_frac_feed_div + max_frac_feed_div) / 2;
  493. tmp = (uint64_t)pll->reference_freq * 10000 * feedback_div;
  494. tmp += (uint64_t)pll->reference_freq * 1000 * frac_feedback_div;
  495. current_freq = radeon_div(tmp, ref_div * post_div);
  496. if (pll->flags & RADEON_PLL_PREFER_CLOSEST_LOWER) {
  497. if (freq < current_freq)
  498. error = 0xffffffff;
  499. else
  500. error = freq - current_freq;
  501. } else
  502. error = abs(current_freq - freq);
  503. vco_diff = abs(vco - best_vco);
  504. if ((best_vco == 0 && error < best_error) ||
  505. (best_vco != 0 &&
  506. ((best_error > 100 && error < best_error - 100) ||
  507. (abs(error - best_error) < 100 && vco_diff < best_vco_diff)))) {
  508. best_post_div = post_div;
  509. best_ref_div = ref_div;
  510. best_feedback_div = feedback_div;
  511. best_frac_feedback_div = frac_feedback_div;
  512. best_freq = current_freq;
  513. best_error = error;
  514. best_vco_diff = vco_diff;
  515. } else if (current_freq == freq) {
  516. if (best_freq == -1) {
  517. best_post_div = post_div;
  518. best_ref_div = ref_div;
  519. best_feedback_div = feedback_div;
  520. best_frac_feedback_div = frac_feedback_div;
  521. best_freq = current_freq;
  522. best_error = error;
  523. best_vco_diff = vco_diff;
  524. } else if (((pll->flags & RADEON_PLL_PREFER_LOW_REF_DIV) && (ref_div < best_ref_div)) ||
  525. ((pll->flags & RADEON_PLL_PREFER_HIGH_REF_DIV) && (ref_div > best_ref_div)) ||
  526. ((pll->flags & RADEON_PLL_PREFER_LOW_FB_DIV) && (feedback_div < best_feedback_div)) ||
  527. ((pll->flags & RADEON_PLL_PREFER_HIGH_FB_DIV) && (feedback_div > best_feedback_div)) ||
  528. ((pll->flags & RADEON_PLL_PREFER_LOW_POST_DIV) && (post_div < best_post_div)) ||
  529. ((pll->flags & RADEON_PLL_PREFER_HIGH_POST_DIV) && (post_div > best_post_div))) {
  530. best_post_div = post_div;
  531. best_ref_div = ref_div;
  532. best_feedback_div = feedback_div;
  533. best_frac_feedback_div = frac_feedback_div;
  534. best_freq = current_freq;
  535. best_error = error;
  536. best_vco_diff = vco_diff;
  537. }
  538. }
  539. if (current_freq < freq)
  540. min_frac_feed_div = frac_feedback_div + 1;
  541. else
  542. max_frac_feed_div = frac_feedback_div;
  543. }
  544. if (current_freq < freq)
  545. min_feed_div = feedback_div + 1;
  546. else
  547. max_feed_div = feedback_div;
  548. }
  549. }
  550. }
  551. *dot_clock_p = best_freq / 10000;
  552. *fb_div_p = best_feedback_div;
  553. *frac_fb_div_p = best_frac_feedback_div;
  554. *ref_div_p = best_ref_div;
  555. *post_div_p = best_post_div;
  556. }
  557. static bool
  558. calc_fb_div(struct radeon_pll *pll,
  559. uint32_t freq,
  560. uint32_t post_div,
  561. uint32_t ref_div,
  562. uint32_t *fb_div,
  563. uint32_t *fb_div_frac)
  564. {
  565. fixed20_12 feedback_divider, a, b;
  566. u32 vco_freq;
  567. vco_freq = freq * post_div;
  568. /* feedback_divider = vco_freq * ref_div / pll->reference_freq; */
  569. a.full = dfixed_const(pll->reference_freq);
  570. feedback_divider.full = dfixed_const(vco_freq);
  571. feedback_divider.full = dfixed_div(feedback_divider, a);
  572. a.full = dfixed_const(ref_div);
  573. feedback_divider.full = dfixed_mul(feedback_divider, a);
  574. if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
  575. /* feedback_divider = floor((feedback_divider * 10.0) + 0.5) * 0.1; */
  576. a.full = dfixed_const(10);
  577. feedback_divider.full = dfixed_mul(feedback_divider, a);
  578. feedback_divider.full += dfixed_const_half(0);
  579. feedback_divider.full = dfixed_floor(feedback_divider);
  580. feedback_divider.full = dfixed_div(feedback_divider, a);
  581. /* *fb_div = floor(feedback_divider); */
  582. a.full = dfixed_floor(feedback_divider);
  583. *fb_div = dfixed_trunc(a);
  584. /* *fb_div_frac = fmod(feedback_divider, 1.0) * 10.0; */
  585. a.full = dfixed_const(10);
  586. b.full = dfixed_mul(feedback_divider, a);
  587. feedback_divider.full = dfixed_floor(feedback_divider);
  588. feedback_divider.full = dfixed_mul(feedback_divider, a);
  589. feedback_divider.full = b.full - feedback_divider.full;
  590. *fb_div_frac = dfixed_trunc(feedback_divider);
  591. } else {
  592. /* *fb_div = floor(feedback_divider + 0.5); */
  593. feedback_divider.full += dfixed_const_half(0);
  594. feedback_divider.full = dfixed_floor(feedback_divider);
  595. *fb_div = dfixed_trunc(feedback_divider);
  596. *fb_div_frac = 0;
  597. }
  598. if (((*fb_div) < pll->min_feedback_div) || ((*fb_div) > pll->max_feedback_div))
  599. return false;
  600. else
  601. return true;
  602. }
  603. static bool
  604. calc_fb_ref_div(struct radeon_pll *pll,
  605. uint32_t freq,
  606. uint32_t post_div,
  607. uint32_t *fb_div,
  608. uint32_t *fb_div_frac,
  609. uint32_t *ref_div)
  610. {
  611. fixed20_12 ffreq, max_error, error, pll_out, a;
  612. u32 vco;
  613. u32 pll_out_min, pll_out_max;
  614. if (pll->flags & RADEON_PLL_IS_LCD) {
  615. pll_out_min = pll->lcd_pll_out_min;
  616. pll_out_max = pll->lcd_pll_out_max;
  617. } else {
  618. pll_out_min = pll->pll_out_min;
  619. pll_out_max = pll->pll_out_max;
  620. }
  621. ffreq.full = dfixed_const(freq);
  622. /* max_error = ffreq * 0.0025; */
  623. a.full = dfixed_const(400);
  624. max_error.full = dfixed_div(ffreq, a);
  625. for ((*ref_div) = pll->min_ref_div; (*ref_div) < pll->max_ref_div; ++(*ref_div)) {
  626. if (calc_fb_div(pll, freq, post_div, (*ref_div), fb_div, fb_div_frac)) {
  627. vco = pll->reference_freq * (((*fb_div) * 10) + (*fb_div_frac));
  628. vco = vco / ((*ref_div) * 10);
  629. if ((vco < pll_out_min) || (vco > pll_out_max))
  630. continue;
  631. /* pll_out = vco / post_div; */
  632. a.full = dfixed_const(post_div);
  633. pll_out.full = dfixed_const(vco);
  634. pll_out.full = dfixed_div(pll_out, a);
  635. if (pll_out.full >= ffreq.full) {
  636. error.full = pll_out.full - ffreq.full;
  637. if (error.full <= max_error.full)
  638. return true;
  639. }
  640. }
  641. }
  642. return false;
  643. }
  644. static void radeon_compute_pll_new(struct radeon_pll *pll,
  645. uint64_t freq,
  646. uint32_t *dot_clock_p,
  647. uint32_t *fb_div_p,
  648. uint32_t *frac_fb_div_p,
  649. uint32_t *ref_div_p,
  650. uint32_t *post_div_p)
  651. {
  652. u32 fb_div = 0, fb_div_frac = 0, post_div = 0, ref_div = 0;
  653. u32 best_freq = 0, vco_frequency;
  654. u32 pll_out_min, pll_out_max;
  655. if (pll->flags & RADEON_PLL_IS_LCD) {
  656. pll_out_min = pll->lcd_pll_out_min;
  657. pll_out_max = pll->lcd_pll_out_max;
  658. } else {
  659. pll_out_min = pll->pll_out_min;
  660. pll_out_max = pll->pll_out_max;
  661. }
  662. /* freq = freq / 10; */
  663. do_div(freq, 10);
  664. if (pll->flags & RADEON_PLL_USE_POST_DIV) {
  665. post_div = pll->post_div;
  666. if ((post_div < pll->min_post_div) || (post_div > pll->max_post_div))
  667. goto done;
  668. vco_frequency = freq * post_div;
  669. if ((vco_frequency < pll_out_min) || (vco_frequency > pll_out_max))
  670. goto done;
  671. if (pll->flags & RADEON_PLL_USE_REF_DIV) {
  672. ref_div = pll->reference_div;
  673. if ((ref_div < pll->min_ref_div) || (ref_div > pll->max_ref_div))
  674. goto done;
  675. if (!calc_fb_div(pll, freq, post_div, ref_div, &fb_div, &fb_div_frac))
  676. goto done;
  677. }
  678. } else {
  679. for (post_div = pll->max_post_div; post_div >= pll->min_post_div; --post_div) {
  680. if (pll->flags & RADEON_PLL_LEGACY) {
  681. if ((post_div == 5) ||
  682. (post_div == 7) ||
  683. (post_div == 9) ||
  684. (post_div == 10) ||
  685. (post_div == 11))
  686. continue;
  687. }
  688. if ((pll->flags & RADEON_PLL_NO_ODD_POST_DIV) && (post_div & 1))
  689. continue;
  690. vco_frequency = freq * post_div;
  691. if ((vco_frequency < pll_out_min) || (vco_frequency > pll_out_max))
  692. continue;
  693. if (pll->flags & RADEON_PLL_USE_REF_DIV) {
  694. ref_div = pll->reference_div;
  695. if ((ref_div < pll->min_ref_div) || (ref_div > pll->max_ref_div))
  696. goto done;
  697. if (calc_fb_div(pll, freq, post_div, ref_div, &fb_div, &fb_div_frac))
  698. break;
  699. } else {
  700. if (calc_fb_ref_div(pll, freq, post_div, &fb_div, &fb_div_frac, &ref_div))
  701. break;
  702. }
  703. }
  704. }
  705. best_freq = pll->reference_freq * 10 * fb_div;
  706. best_freq += pll->reference_freq * fb_div_frac;
  707. best_freq = best_freq / (ref_div * post_div);
  708. done:
  709. if (best_freq == 0)
  710. DRM_ERROR("Couldn't find valid PLL dividers\n");
  711. *dot_clock_p = best_freq / 10;
  712. *fb_div_p = fb_div;
  713. *frac_fb_div_p = fb_div_frac;
  714. *ref_div_p = ref_div;
  715. *post_div_p = post_div;
  716. DRM_DEBUG_KMS("%u %d.%d, %d, %d\n", *dot_clock_p, *fb_div_p, *frac_fb_div_p, *ref_div_p, *post_div_p);
  717. }
  718. void radeon_compute_pll(struct radeon_pll *pll,
  719. uint64_t freq,
  720. uint32_t *dot_clock_p,
  721. uint32_t *fb_div_p,
  722. uint32_t *frac_fb_div_p,
  723. uint32_t *ref_div_p,
  724. uint32_t *post_div_p)
  725. {
  726. switch (pll->algo) {
  727. case PLL_ALGO_NEW:
  728. radeon_compute_pll_new(pll, freq, dot_clock_p, fb_div_p,
  729. frac_fb_div_p, ref_div_p, post_div_p);
  730. break;
  731. case PLL_ALGO_LEGACY:
  732. default:
  733. radeon_compute_pll_legacy(pll, freq, dot_clock_p, fb_div_p,
  734. frac_fb_div_p, ref_div_p, post_div_p);
  735. break;
  736. }
  737. }
  738. static void radeon_user_framebuffer_destroy(struct drm_framebuffer *fb)
  739. {
  740. struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
  741. if (radeon_fb->obj)
  742. drm_gem_object_unreference_unlocked(radeon_fb->obj);
  743. drm_framebuffer_cleanup(fb);
  744. kfree(radeon_fb);
  745. }
  746. static int radeon_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  747. struct drm_file *file_priv,
  748. unsigned int *handle)
  749. {
  750. struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
  751. return drm_gem_handle_create(file_priv, radeon_fb->obj, handle);
  752. }
  753. static const struct drm_framebuffer_funcs radeon_fb_funcs = {
  754. .destroy = radeon_user_framebuffer_destroy,
  755. .create_handle = radeon_user_framebuffer_create_handle,
  756. };
  757. void
  758. radeon_framebuffer_init(struct drm_device *dev,
  759. struct radeon_framebuffer *rfb,
  760. struct drm_mode_fb_cmd *mode_cmd,
  761. struct drm_gem_object *obj)
  762. {
  763. rfb->obj = obj;
  764. drm_framebuffer_init(dev, &rfb->base, &radeon_fb_funcs);
  765. drm_helper_mode_fill_fb_struct(&rfb->base, mode_cmd);
  766. }
  767. static struct drm_framebuffer *
  768. radeon_user_framebuffer_create(struct drm_device *dev,
  769. struct drm_file *file_priv,
  770. struct drm_mode_fb_cmd *mode_cmd)
  771. {
  772. struct drm_gem_object *obj;
  773. struct radeon_framebuffer *radeon_fb;
  774. obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handle);
  775. if (obj == NULL) {
  776. dev_err(&dev->pdev->dev, "No GEM object associated to handle 0x%08X, "
  777. "can't create framebuffer\n", mode_cmd->handle);
  778. return ERR_PTR(-ENOENT);
  779. }
  780. radeon_fb = kzalloc(sizeof(*radeon_fb), GFP_KERNEL);
  781. if (radeon_fb == NULL)
  782. return ERR_PTR(-ENOMEM);
  783. radeon_framebuffer_init(dev, radeon_fb, mode_cmd, obj);
  784. return &radeon_fb->base;
  785. }
  786. static void radeon_output_poll_changed(struct drm_device *dev)
  787. {
  788. struct radeon_device *rdev = dev->dev_private;
  789. radeon_fb_output_poll_changed(rdev);
  790. }
  791. static const struct drm_mode_config_funcs radeon_mode_funcs = {
  792. .fb_create = radeon_user_framebuffer_create,
  793. .output_poll_changed = radeon_output_poll_changed
  794. };
  795. struct drm_prop_enum_list {
  796. int type;
  797. char *name;
  798. };
  799. static struct drm_prop_enum_list radeon_tmds_pll_enum_list[] =
  800. { { 0, "driver" },
  801. { 1, "bios" },
  802. };
  803. static struct drm_prop_enum_list radeon_tv_std_enum_list[] =
  804. { { TV_STD_NTSC, "ntsc" },
  805. { TV_STD_PAL, "pal" },
  806. { TV_STD_PAL_M, "pal-m" },
  807. { TV_STD_PAL_60, "pal-60" },
  808. { TV_STD_NTSC_J, "ntsc-j" },
  809. { TV_STD_SCART_PAL, "scart-pal" },
  810. { TV_STD_PAL_CN, "pal-cn" },
  811. { TV_STD_SECAM, "secam" },
  812. };
  813. static struct drm_prop_enum_list radeon_underscan_enum_list[] =
  814. { { UNDERSCAN_OFF, "off" },
  815. { UNDERSCAN_ON, "on" },
  816. { UNDERSCAN_AUTO, "auto" },
  817. };
  818. static int radeon_modeset_create_props(struct radeon_device *rdev)
  819. {
  820. int i, sz;
  821. if (rdev->is_atom_bios) {
  822. rdev->mode_info.coherent_mode_property =
  823. drm_property_create(rdev->ddev,
  824. DRM_MODE_PROP_RANGE,
  825. "coherent", 2);
  826. if (!rdev->mode_info.coherent_mode_property)
  827. return -ENOMEM;
  828. rdev->mode_info.coherent_mode_property->values[0] = 0;
  829. rdev->mode_info.coherent_mode_property->values[1] = 1;
  830. }
  831. if (!ASIC_IS_AVIVO(rdev)) {
  832. sz = ARRAY_SIZE(radeon_tmds_pll_enum_list);
  833. rdev->mode_info.tmds_pll_property =
  834. drm_property_create(rdev->ddev,
  835. DRM_MODE_PROP_ENUM,
  836. "tmds_pll", sz);
  837. for (i = 0; i < sz; i++) {
  838. drm_property_add_enum(rdev->mode_info.tmds_pll_property,
  839. i,
  840. radeon_tmds_pll_enum_list[i].type,
  841. radeon_tmds_pll_enum_list[i].name);
  842. }
  843. }
  844. rdev->mode_info.load_detect_property =
  845. drm_property_create(rdev->ddev,
  846. DRM_MODE_PROP_RANGE,
  847. "load detection", 2);
  848. if (!rdev->mode_info.load_detect_property)
  849. return -ENOMEM;
  850. rdev->mode_info.load_detect_property->values[0] = 0;
  851. rdev->mode_info.load_detect_property->values[1] = 1;
  852. drm_mode_create_scaling_mode_property(rdev->ddev);
  853. sz = ARRAY_SIZE(radeon_tv_std_enum_list);
  854. rdev->mode_info.tv_std_property =
  855. drm_property_create(rdev->ddev,
  856. DRM_MODE_PROP_ENUM,
  857. "tv standard", sz);
  858. for (i = 0; i < sz; i++) {
  859. drm_property_add_enum(rdev->mode_info.tv_std_property,
  860. i,
  861. radeon_tv_std_enum_list[i].type,
  862. radeon_tv_std_enum_list[i].name);
  863. }
  864. sz = ARRAY_SIZE(radeon_underscan_enum_list);
  865. rdev->mode_info.underscan_property =
  866. drm_property_create(rdev->ddev,
  867. DRM_MODE_PROP_ENUM,
  868. "underscan", sz);
  869. for (i = 0; i < sz; i++) {
  870. drm_property_add_enum(rdev->mode_info.underscan_property,
  871. i,
  872. radeon_underscan_enum_list[i].type,
  873. radeon_underscan_enum_list[i].name);
  874. }
  875. return 0;
  876. }
  877. void radeon_update_display_priority(struct radeon_device *rdev)
  878. {
  879. /* adjustment options for the display watermarks */
  880. if ((radeon_disp_priority == 0) || (radeon_disp_priority > 2)) {
  881. /* set display priority to high for r3xx, rv515 chips
  882. * this avoids flickering due to underflow to the
  883. * display controllers during heavy acceleration.
  884. * Don't force high on rs4xx igp chips as it seems to
  885. * affect the sound card. See kernel bug 15982.
  886. */
  887. if ((ASIC_IS_R300(rdev) || (rdev->family == CHIP_RV515)) &&
  888. !(rdev->flags & RADEON_IS_IGP))
  889. rdev->disp_priority = 2;
  890. else
  891. rdev->disp_priority = 0;
  892. } else
  893. rdev->disp_priority = radeon_disp_priority;
  894. }
  895. int radeon_modeset_init(struct radeon_device *rdev)
  896. {
  897. int i;
  898. int ret;
  899. drm_mode_config_init(rdev->ddev);
  900. rdev->mode_info.mode_config_initialized = true;
  901. rdev->ddev->mode_config.funcs = (void *)&radeon_mode_funcs;
  902. if (ASIC_IS_AVIVO(rdev)) {
  903. rdev->ddev->mode_config.max_width = 8192;
  904. rdev->ddev->mode_config.max_height = 8192;
  905. } else {
  906. rdev->ddev->mode_config.max_width = 4096;
  907. rdev->ddev->mode_config.max_height = 4096;
  908. }
  909. rdev->ddev->mode_config.fb_base = rdev->mc.aper_base;
  910. ret = radeon_modeset_create_props(rdev);
  911. if (ret) {
  912. return ret;
  913. }
  914. /* init i2c buses */
  915. radeon_i2c_init(rdev);
  916. /* check combios for a valid hardcoded EDID - Sun servers */
  917. if (!rdev->is_atom_bios) {
  918. /* check for hardcoded EDID in BIOS */
  919. radeon_combios_check_hardcoded_edid(rdev);
  920. }
  921. /* allocate crtcs */
  922. for (i = 0; i < rdev->num_crtc; i++) {
  923. radeon_crtc_init(rdev->ddev, i);
  924. }
  925. /* okay we should have all the bios connectors */
  926. ret = radeon_setup_enc_conn(rdev->ddev);
  927. if (!ret) {
  928. return ret;
  929. }
  930. /* initialize hpd */
  931. radeon_hpd_init(rdev);
  932. /* Initialize power management */
  933. radeon_pm_init(rdev);
  934. radeon_fbdev_init(rdev);
  935. drm_kms_helper_poll_init(rdev->ddev);
  936. return 0;
  937. }
  938. void radeon_modeset_fini(struct radeon_device *rdev)
  939. {
  940. radeon_fbdev_fini(rdev);
  941. kfree(rdev->mode_info.bios_hardcoded_edid);
  942. radeon_pm_fini(rdev);
  943. if (rdev->mode_info.mode_config_initialized) {
  944. drm_kms_helper_poll_fini(rdev->ddev);
  945. radeon_hpd_fini(rdev);
  946. drm_mode_config_cleanup(rdev->ddev);
  947. rdev->mode_info.mode_config_initialized = false;
  948. }
  949. /* free i2c buses */
  950. radeon_i2c_fini(rdev);
  951. }
  952. static bool is_hdtv_mode(struct drm_display_mode *mode)
  953. {
  954. /* try and guess if this is a tv or a monitor */
  955. if ((mode->vdisplay == 480 && mode->hdisplay == 720) || /* 480p */
  956. (mode->vdisplay == 576) || /* 576p */
  957. (mode->vdisplay == 720) || /* 720p */
  958. (mode->vdisplay == 1080)) /* 1080p */
  959. return true;
  960. else
  961. return false;
  962. }
  963. bool radeon_crtc_scaling_mode_fixup(struct drm_crtc *crtc,
  964. struct drm_display_mode *mode,
  965. struct drm_display_mode *adjusted_mode)
  966. {
  967. struct drm_device *dev = crtc->dev;
  968. struct radeon_device *rdev = dev->dev_private;
  969. struct drm_encoder *encoder;
  970. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  971. struct radeon_encoder *radeon_encoder;
  972. struct drm_connector *connector;
  973. struct radeon_connector *radeon_connector;
  974. bool first = true;
  975. u32 src_v = 1, dst_v = 1;
  976. u32 src_h = 1, dst_h = 1;
  977. radeon_crtc->h_border = 0;
  978. radeon_crtc->v_border = 0;
  979. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  980. if (encoder->crtc != crtc)
  981. continue;
  982. radeon_encoder = to_radeon_encoder(encoder);
  983. connector = radeon_get_connector_for_encoder(encoder);
  984. radeon_connector = to_radeon_connector(connector);
  985. if (first) {
  986. /* set scaling */
  987. if (radeon_encoder->rmx_type == RMX_OFF)
  988. radeon_crtc->rmx_type = RMX_OFF;
  989. else if (mode->hdisplay < radeon_encoder->native_mode.hdisplay ||
  990. mode->vdisplay < radeon_encoder->native_mode.vdisplay)
  991. radeon_crtc->rmx_type = radeon_encoder->rmx_type;
  992. else
  993. radeon_crtc->rmx_type = RMX_OFF;
  994. /* copy native mode */
  995. memcpy(&radeon_crtc->native_mode,
  996. &radeon_encoder->native_mode,
  997. sizeof(struct drm_display_mode));
  998. src_v = crtc->mode.vdisplay;
  999. dst_v = radeon_crtc->native_mode.vdisplay;
  1000. src_h = crtc->mode.hdisplay;
  1001. dst_h = radeon_crtc->native_mode.hdisplay;
  1002. /* fix up for overscan on hdmi */
  1003. if (ASIC_IS_AVIVO(rdev) &&
  1004. (!(mode->flags & DRM_MODE_FLAG_INTERLACE)) &&
  1005. ((radeon_encoder->underscan_type == UNDERSCAN_ON) ||
  1006. ((radeon_encoder->underscan_type == UNDERSCAN_AUTO) &&
  1007. drm_detect_hdmi_monitor(radeon_connector->edid) &&
  1008. is_hdtv_mode(mode)))) {
  1009. radeon_crtc->h_border = (mode->hdisplay >> 5) + 16;
  1010. radeon_crtc->v_border = (mode->vdisplay >> 5) + 16;
  1011. radeon_crtc->rmx_type = RMX_FULL;
  1012. src_v = crtc->mode.vdisplay;
  1013. dst_v = crtc->mode.vdisplay - (radeon_crtc->v_border * 2);
  1014. src_h = crtc->mode.hdisplay;
  1015. dst_h = crtc->mode.hdisplay - (radeon_crtc->h_border * 2);
  1016. }
  1017. first = false;
  1018. } else {
  1019. if (radeon_crtc->rmx_type != radeon_encoder->rmx_type) {
  1020. /* WARNING: Right now this can't happen but
  1021. * in the future we need to check that scaling
  1022. * are consistent across different encoder
  1023. * (ie all encoder can work with the same
  1024. * scaling).
  1025. */
  1026. DRM_ERROR("Scaling not consistent across encoder.\n");
  1027. return false;
  1028. }
  1029. }
  1030. }
  1031. if (radeon_crtc->rmx_type != RMX_OFF) {
  1032. fixed20_12 a, b;
  1033. a.full = dfixed_const(src_v);
  1034. b.full = dfixed_const(dst_v);
  1035. radeon_crtc->vsc.full = dfixed_div(a, b);
  1036. a.full = dfixed_const(src_h);
  1037. b.full = dfixed_const(dst_h);
  1038. radeon_crtc->hsc.full = dfixed_div(a, b);
  1039. } else {
  1040. radeon_crtc->vsc.full = dfixed_const(1);
  1041. radeon_crtc->hsc.full = dfixed_const(1);
  1042. }
  1043. return true;
  1044. }