ast_mode.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431
  1. /*
  2. * Copyright 2012 Red Hat Inc.
  3. * Parts based on xf86-video-ast
  4. * Copyright (c) 2005 ASPEED Technology Inc.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sub license, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  18. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  19. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  20. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * The above copyright notice and this permission notice (including the
  23. * next paragraph) shall be included in all copies or substantial portions
  24. * of the Software.
  25. *
  26. */
  27. /*
  28. * Authors: Dave Airlie <airlied@redhat.com>
  29. */
  30. #include <linux/delay.h>
  31. #include <linux/export.h>
  32. #include <linux/pci.h>
  33. #include <drm/drm_atomic.h>
  34. #include <drm/drm_atomic_helper.h>
  35. #include <drm/drm_crtc.h>
  36. #include <drm/drm_damage_helper.h>
  37. #include <drm/drm_format_helper.h>
  38. #include <drm/drm_fourcc.h>
  39. #include <drm/drm_gem_atomic_helper.h>
  40. #include <drm/drm_gem_framebuffer_helper.h>
  41. #include <drm/drm_gem_shmem_helper.h>
  42. #include <drm/drm_managed.h>
  43. #include <drm/drm_panic.h>
  44. #include <drm/drm_probe_helper.h>
  45. #include "ast_drv.h"
  46. #include "ast_tables.h"
  47. #define AST_LUT_SIZE 256
  48. static inline void ast_load_palette_index(struct ast_device *ast,
  49. u8 index, u8 red, u8 green,
  50. u8 blue)
  51. {
  52. ast_io_write8(ast, AST_IO_VGADWR, index);
  53. ast_io_read8(ast, AST_IO_VGASRI);
  54. ast_io_write8(ast, AST_IO_VGAPDR, red);
  55. ast_io_read8(ast, AST_IO_VGASRI);
  56. ast_io_write8(ast, AST_IO_VGAPDR, green);
  57. ast_io_read8(ast, AST_IO_VGASRI);
  58. ast_io_write8(ast, AST_IO_VGAPDR, blue);
  59. ast_io_read8(ast, AST_IO_VGASRI);
  60. }
  61. static void ast_crtc_set_gamma_linear(struct ast_device *ast,
  62. const struct drm_format_info *format)
  63. {
  64. int i;
  65. switch (format->format) {
  66. case DRM_FORMAT_C8: /* In this case, gamma table is used as color palette */
  67. case DRM_FORMAT_RGB565:
  68. case DRM_FORMAT_XRGB8888:
  69. for (i = 0; i < AST_LUT_SIZE; i++)
  70. ast_load_palette_index(ast, i, i, i, i);
  71. break;
  72. default:
  73. drm_warn_once(&ast->base, "Unsupported format %p4cc for gamma correction\n",
  74. &format->format);
  75. break;
  76. }
  77. }
  78. static void ast_crtc_set_gamma(struct ast_device *ast,
  79. const struct drm_format_info *format,
  80. struct drm_color_lut *lut)
  81. {
  82. int i;
  83. switch (format->format) {
  84. case DRM_FORMAT_C8: /* In this case, gamma table is used as color palette */
  85. case DRM_FORMAT_RGB565:
  86. case DRM_FORMAT_XRGB8888:
  87. for (i = 0; i < AST_LUT_SIZE; i++)
  88. ast_load_palette_index(ast, i,
  89. lut[i].red >> 8,
  90. lut[i].green >> 8,
  91. lut[i].blue >> 8);
  92. break;
  93. default:
  94. drm_warn_once(&ast->base, "Unsupported format %p4cc for gamma correction\n",
  95. &format->format);
  96. break;
  97. }
  98. }
  99. static bool ast_get_vbios_mode_info(const struct drm_format_info *format,
  100. const struct drm_display_mode *mode,
  101. struct drm_display_mode *adjusted_mode,
  102. struct ast_vbios_mode_info *vbios_mode)
  103. {
  104. u32 refresh_rate_index = 0, refresh_rate;
  105. const struct ast_vbios_enhtable *best = NULL;
  106. u32 hborder, vborder;
  107. bool check_sync;
  108. switch (format->cpp[0] * 8) {
  109. case 8:
  110. vbios_mode->std_table = &vbios_stdtable[VGAModeIndex];
  111. break;
  112. case 16:
  113. vbios_mode->std_table = &vbios_stdtable[HiCModeIndex];
  114. break;
  115. case 24:
  116. case 32:
  117. vbios_mode->std_table = &vbios_stdtable[TrueCModeIndex];
  118. break;
  119. default:
  120. return false;
  121. }
  122. switch (mode->hdisplay) {
  123. case 640:
  124. vbios_mode->enh_table = &res_640x480[refresh_rate_index];
  125. break;
  126. case 800:
  127. vbios_mode->enh_table = &res_800x600[refresh_rate_index];
  128. break;
  129. case 1024:
  130. vbios_mode->enh_table = &res_1024x768[refresh_rate_index];
  131. break;
  132. case 1152:
  133. vbios_mode->enh_table = &res_1152x864[refresh_rate_index];
  134. break;
  135. case 1280:
  136. if (mode->vdisplay == 800)
  137. vbios_mode->enh_table = &res_1280x800[refresh_rate_index];
  138. else
  139. vbios_mode->enh_table = &res_1280x1024[refresh_rate_index];
  140. break;
  141. case 1360:
  142. vbios_mode->enh_table = &res_1360x768[refresh_rate_index];
  143. break;
  144. case 1440:
  145. vbios_mode->enh_table = &res_1440x900[refresh_rate_index];
  146. break;
  147. case 1600:
  148. if (mode->vdisplay == 900)
  149. vbios_mode->enh_table = &res_1600x900[refresh_rate_index];
  150. else
  151. vbios_mode->enh_table = &res_1600x1200[refresh_rate_index];
  152. break;
  153. case 1680:
  154. vbios_mode->enh_table = &res_1680x1050[refresh_rate_index];
  155. break;
  156. case 1920:
  157. if (mode->vdisplay == 1080)
  158. vbios_mode->enh_table = &res_1920x1080[refresh_rate_index];
  159. else
  160. vbios_mode->enh_table = &res_1920x1200[refresh_rate_index];
  161. break;
  162. default:
  163. return false;
  164. }
  165. refresh_rate = drm_mode_vrefresh(mode);
  166. check_sync = vbios_mode->enh_table->flags & WideScreenMode;
  167. while (1) {
  168. const struct ast_vbios_enhtable *loop = vbios_mode->enh_table;
  169. while (loop->refresh_rate != 0xff) {
  170. if ((check_sync) &&
  171. (((mode->flags & DRM_MODE_FLAG_NVSYNC) &&
  172. (loop->flags & PVSync)) ||
  173. ((mode->flags & DRM_MODE_FLAG_PVSYNC) &&
  174. (loop->flags & NVSync)) ||
  175. ((mode->flags & DRM_MODE_FLAG_NHSYNC) &&
  176. (loop->flags & PHSync)) ||
  177. ((mode->flags & DRM_MODE_FLAG_PHSYNC) &&
  178. (loop->flags & NHSync)))) {
  179. loop++;
  180. continue;
  181. }
  182. if (loop->refresh_rate <= refresh_rate
  183. && (!best || loop->refresh_rate > best->refresh_rate))
  184. best = loop;
  185. loop++;
  186. }
  187. if (best || !check_sync)
  188. break;
  189. check_sync = 0;
  190. }
  191. if (best)
  192. vbios_mode->enh_table = best;
  193. hborder = (vbios_mode->enh_table->flags & HBorder) ? 8 : 0;
  194. vborder = (vbios_mode->enh_table->flags & VBorder) ? 8 : 0;
  195. adjusted_mode->crtc_hdisplay = vbios_mode->enh_table->hde;
  196. adjusted_mode->crtc_htotal = vbios_mode->enh_table->ht;
  197. adjusted_mode->crtc_hblank_start = vbios_mode->enh_table->hde + hborder;
  198. adjusted_mode->crtc_hblank_end = vbios_mode->enh_table->ht - hborder;
  199. adjusted_mode->crtc_hsync_start = vbios_mode->enh_table->hde + hborder +
  200. vbios_mode->enh_table->hfp;
  201. adjusted_mode->crtc_hsync_end = (vbios_mode->enh_table->hde + hborder +
  202. vbios_mode->enh_table->hfp +
  203. vbios_mode->enh_table->hsync);
  204. adjusted_mode->crtc_vdisplay = vbios_mode->enh_table->vde;
  205. adjusted_mode->crtc_vtotal = vbios_mode->enh_table->vt;
  206. adjusted_mode->crtc_vblank_start = vbios_mode->enh_table->vde + vborder;
  207. adjusted_mode->crtc_vblank_end = vbios_mode->enh_table->vt - vborder;
  208. adjusted_mode->crtc_vsync_start = vbios_mode->enh_table->vde + vborder +
  209. vbios_mode->enh_table->vfp;
  210. adjusted_mode->crtc_vsync_end = (vbios_mode->enh_table->vde + vborder +
  211. vbios_mode->enh_table->vfp +
  212. vbios_mode->enh_table->vsync);
  213. return true;
  214. }
  215. static void ast_set_vbios_color_reg(struct ast_device *ast,
  216. const struct drm_format_info *format,
  217. const struct ast_vbios_mode_info *vbios_mode)
  218. {
  219. u32 color_index;
  220. switch (format->cpp[0]) {
  221. case 1:
  222. color_index = VGAModeIndex - 1;
  223. break;
  224. case 2:
  225. color_index = HiCModeIndex;
  226. break;
  227. case 3:
  228. case 4:
  229. color_index = TrueCModeIndex;
  230. break;
  231. default:
  232. return;
  233. }
  234. ast_set_index_reg(ast, AST_IO_VGACRI, 0x8c, (u8)((color_index & 0x0f) << 4));
  235. ast_set_index_reg(ast, AST_IO_VGACRI, 0x91, 0x00);
  236. if (vbios_mode->enh_table->flags & NewModeInfo) {
  237. ast_set_index_reg(ast, AST_IO_VGACRI, 0x91, 0xa8);
  238. ast_set_index_reg(ast, AST_IO_VGACRI, 0x92, format->cpp[0] * 8);
  239. }
  240. }
  241. static void ast_set_vbios_mode_reg(struct ast_device *ast,
  242. const struct drm_display_mode *adjusted_mode,
  243. const struct ast_vbios_mode_info *vbios_mode)
  244. {
  245. u32 refresh_rate_index, mode_id;
  246. refresh_rate_index = vbios_mode->enh_table->refresh_rate_index;
  247. mode_id = vbios_mode->enh_table->mode_id;
  248. ast_set_index_reg(ast, AST_IO_VGACRI, 0x8d, refresh_rate_index & 0xff);
  249. ast_set_index_reg(ast, AST_IO_VGACRI, 0x8e, mode_id & 0xff);
  250. ast_set_index_reg(ast, AST_IO_VGACRI, 0x91, 0x00);
  251. if (vbios_mode->enh_table->flags & NewModeInfo) {
  252. ast_set_index_reg(ast, AST_IO_VGACRI, 0x91, 0xa8);
  253. ast_set_index_reg(ast, AST_IO_VGACRI, 0x93, adjusted_mode->clock / 1000);
  254. ast_set_index_reg(ast, AST_IO_VGACRI, 0x94, adjusted_mode->crtc_hdisplay);
  255. ast_set_index_reg(ast, AST_IO_VGACRI, 0x95, adjusted_mode->crtc_hdisplay >> 8);
  256. ast_set_index_reg(ast, AST_IO_VGACRI, 0x96, adjusted_mode->crtc_vdisplay);
  257. ast_set_index_reg(ast, AST_IO_VGACRI, 0x97, adjusted_mode->crtc_vdisplay >> 8);
  258. }
  259. }
  260. static void ast_set_std_reg(struct ast_device *ast,
  261. struct drm_display_mode *mode,
  262. struct ast_vbios_mode_info *vbios_mode)
  263. {
  264. const struct ast_vbios_stdtable *stdtable;
  265. u32 i;
  266. u8 jreg;
  267. stdtable = vbios_mode->std_table;
  268. jreg = stdtable->misc;
  269. ast_io_write8(ast, AST_IO_VGAMR_W, jreg);
  270. /* Set SEQ; except Screen Disable field */
  271. ast_set_index_reg(ast, AST_IO_VGASRI, 0x00, 0x03);
  272. ast_set_index_reg_mask(ast, AST_IO_VGASRI, 0x01, 0x20, stdtable->seq[0]);
  273. for (i = 1; i < 4; i++) {
  274. jreg = stdtable->seq[i];
  275. ast_set_index_reg(ast, AST_IO_VGASRI, (i + 1), jreg);
  276. }
  277. /* Set CRTC; except base address and offset */
  278. ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x11, 0x7f, 0x00);
  279. for (i = 0; i < 12; i++)
  280. ast_set_index_reg(ast, AST_IO_VGACRI, i, stdtable->crtc[i]);
  281. for (i = 14; i < 19; i++)
  282. ast_set_index_reg(ast, AST_IO_VGACRI, i, stdtable->crtc[i]);
  283. for (i = 20; i < 25; i++)
  284. ast_set_index_reg(ast, AST_IO_VGACRI, i, stdtable->crtc[i]);
  285. /* set AR */
  286. jreg = ast_io_read8(ast, AST_IO_VGAIR1_R);
  287. for (i = 0; i < 20; i++) {
  288. jreg = stdtable->ar[i];
  289. ast_io_write8(ast, AST_IO_VGAARI_W, (u8)i);
  290. ast_io_write8(ast, AST_IO_VGAARI_W, jreg);
  291. }
  292. ast_io_write8(ast, AST_IO_VGAARI_W, 0x14);
  293. ast_io_write8(ast, AST_IO_VGAARI_W, 0x00);
  294. jreg = ast_io_read8(ast, AST_IO_VGAIR1_R);
  295. ast_io_write8(ast, AST_IO_VGAARI_W, 0x20);
  296. /* Set GR */
  297. for (i = 0; i < 9; i++)
  298. ast_set_index_reg(ast, AST_IO_VGAGRI, i, stdtable->gr[i]);
  299. }
  300. static void ast_set_crtc_reg(struct ast_device *ast,
  301. struct drm_display_mode *mode,
  302. struct ast_vbios_mode_info *vbios_mode)
  303. {
  304. u8 jreg05 = 0, jreg07 = 0, jreg09 = 0, jregAC = 0, jregAD = 0, jregAE = 0;
  305. u16 temp, precache = 0;
  306. if ((IS_AST_GEN6(ast) || IS_AST_GEN7(ast)) &&
  307. (vbios_mode->enh_table->flags & AST2500PreCatchCRT))
  308. precache = 40;
  309. ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x11, 0x7f, 0x00);
  310. temp = (mode->crtc_htotal >> 3) - 5;
  311. if (temp & 0x100)
  312. jregAC |= 0x01; /* HT D[8] */
  313. ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x00, 0x00, temp);
  314. temp = (mode->crtc_hdisplay >> 3) - 1;
  315. if (temp & 0x100)
  316. jregAC |= 0x04; /* HDE D[8] */
  317. ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x01, 0x00, temp);
  318. temp = (mode->crtc_hblank_start >> 3) - 1;
  319. if (temp & 0x100)
  320. jregAC |= 0x10; /* HBS D[8] */
  321. ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x02, 0x00, temp);
  322. temp = ((mode->crtc_hblank_end >> 3) - 1) & 0x7f;
  323. if (temp & 0x20)
  324. jreg05 |= 0x80; /* HBE D[5] */
  325. if (temp & 0x40)
  326. jregAD |= 0x01; /* HBE D[5] */
  327. ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x03, 0xE0, (temp & 0x1f));
  328. temp = ((mode->crtc_hsync_start-precache) >> 3) - 1;
  329. if (temp & 0x100)
  330. jregAC |= 0x40; /* HRS D[5] */
  331. ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x04, 0x00, temp);
  332. temp = (((mode->crtc_hsync_end-precache) >> 3) - 1) & 0x3f;
  333. if (temp & 0x20)
  334. jregAD |= 0x04; /* HRE D[5] */
  335. ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x05, 0x60, (u8)((temp & 0x1f) | jreg05));
  336. ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xAC, 0x00, jregAC);
  337. ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xAD, 0x00, jregAD);
  338. // Workaround for HSync Time non octave pixels (1920x1080@60Hz HSync 44 pixels);
  339. if (IS_AST_GEN7(ast) && (mode->crtc_vdisplay == 1080))
  340. ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xFC, 0xFD, 0x02);
  341. else
  342. ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xFC, 0xFD, 0x00);
  343. /* vert timings */
  344. temp = (mode->crtc_vtotal) - 2;
  345. if (temp & 0x100)
  346. jreg07 |= 0x01;
  347. if (temp & 0x200)
  348. jreg07 |= 0x20;
  349. if (temp & 0x400)
  350. jregAE |= 0x01;
  351. ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x06, 0x00, temp);
  352. temp = (mode->crtc_vsync_start) - 1;
  353. if (temp & 0x100)
  354. jreg07 |= 0x04;
  355. if (temp & 0x200)
  356. jreg07 |= 0x80;
  357. if (temp & 0x400)
  358. jregAE |= 0x08;
  359. ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x10, 0x00, temp);
  360. temp = (mode->crtc_vsync_end - 1) & 0x3f;
  361. if (temp & 0x10)
  362. jregAE |= 0x20;
  363. if (temp & 0x20)
  364. jregAE |= 0x40;
  365. ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x11, 0x70, temp & 0xf);
  366. temp = mode->crtc_vdisplay - 1;
  367. if (temp & 0x100)
  368. jreg07 |= 0x02;
  369. if (temp & 0x200)
  370. jreg07 |= 0x40;
  371. if (temp & 0x400)
  372. jregAE |= 0x02;
  373. ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x12, 0x00, temp);
  374. temp = mode->crtc_vblank_start - 1;
  375. if (temp & 0x100)
  376. jreg07 |= 0x08;
  377. if (temp & 0x200)
  378. jreg09 |= 0x20;
  379. if (temp & 0x400)
  380. jregAE |= 0x04;
  381. ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x15, 0x00, temp);
  382. temp = mode->crtc_vblank_end - 1;
  383. if (temp & 0x100)
  384. jregAE |= 0x10;
  385. ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x16, 0x00, temp);
  386. ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x07, 0x00, jreg07);
  387. ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x09, 0xdf, jreg09);
  388. ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xAE, 0x00, (jregAE | 0x80));
  389. if (precache)
  390. ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xb6, 0x3f, 0x80);
  391. else
  392. ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xb6, 0x3f, 0x00);
  393. ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x11, 0x7f, 0x80);
  394. }
  395. static void ast_set_offset_reg(struct ast_device *ast,
  396. struct drm_framebuffer *fb)
  397. {
  398. u16 offset;
  399. offset = fb->pitches[0] >> 3;
  400. ast_set_index_reg(ast, AST_IO_VGACRI, 0x13, (offset & 0xff));
  401. ast_set_index_reg(ast, AST_IO_VGACRI, 0xb0, (offset >> 8) & 0x3f);
  402. }
  403. static void ast_set_dclk_reg(struct ast_device *ast,
  404. struct drm_display_mode *mode,
  405. struct ast_vbios_mode_info *vbios_mode)
  406. {
  407. const struct ast_vbios_dclk_info *clk_info;
  408. if (IS_AST_GEN6(ast) || IS_AST_GEN7(ast))
  409. clk_info = &dclk_table_ast2500[vbios_mode->enh_table->dclk_index];
  410. else
  411. clk_info = &dclk_table[vbios_mode->enh_table->dclk_index];
  412. ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xc0, 0x00, clk_info->param1);
  413. ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xc1, 0x00, clk_info->param2);
  414. ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xbb, 0x0f,
  415. (clk_info->param3 & 0xc0) |
  416. ((clk_info->param3 & 0x3) << 4));
  417. }
  418. static void ast_set_color_reg(struct ast_device *ast,
  419. const struct drm_format_info *format)
  420. {
  421. u8 jregA0 = 0, jregA3 = 0, jregA8 = 0;
  422. switch (format->cpp[0] * 8) {
  423. case 8:
  424. jregA0 = 0x70;
  425. jregA3 = 0x01;
  426. jregA8 = 0x00;
  427. break;
  428. case 15:
  429. case 16:
  430. jregA0 = 0x70;
  431. jregA3 = 0x04;
  432. jregA8 = 0x02;
  433. break;
  434. case 32:
  435. jregA0 = 0x70;
  436. jregA3 = 0x08;
  437. jregA8 = 0x02;
  438. break;
  439. }
  440. ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xa0, 0x8f, jregA0);
  441. ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xa3, 0xf0, jregA3);
  442. ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xa8, 0xfd, jregA8);
  443. }
  444. static void ast_set_crtthd_reg(struct ast_device *ast)
  445. {
  446. /* Set Threshold */
  447. if (IS_AST_GEN7(ast)) {
  448. ast_set_index_reg(ast, AST_IO_VGACRI, 0xa7, 0xe0);
  449. ast_set_index_reg(ast, AST_IO_VGACRI, 0xa6, 0xa0);
  450. } else if (IS_AST_GEN6(ast) || IS_AST_GEN5(ast) || IS_AST_GEN4(ast)) {
  451. ast_set_index_reg(ast, AST_IO_VGACRI, 0xa7, 0x78);
  452. ast_set_index_reg(ast, AST_IO_VGACRI, 0xa6, 0x60);
  453. } else if (IS_AST_GEN3(ast) || IS_AST_GEN2(ast)) {
  454. ast_set_index_reg(ast, AST_IO_VGACRI, 0xa7, 0x3f);
  455. ast_set_index_reg(ast, AST_IO_VGACRI, 0xa6, 0x2f);
  456. } else {
  457. ast_set_index_reg(ast, AST_IO_VGACRI, 0xa7, 0x2f);
  458. ast_set_index_reg(ast, AST_IO_VGACRI, 0xa6, 0x1f);
  459. }
  460. }
  461. static void ast_set_sync_reg(struct ast_device *ast,
  462. struct drm_display_mode *mode,
  463. struct ast_vbios_mode_info *vbios_mode)
  464. {
  465. u8 jreg;
  466. jreg = ast_io_read8(ast, AST_IO_VGAMR_R);
  467. jreg &= ~0xC0;
  468. if (vbios_mode->enh_table->flags & NVSync)
  469. jreg |= 0x80;
  470. if (vbios_mode->enh_table->flags & NHSync)
  471. jreg |= 0x40;
  472. ast_io_write8(ast, AST_IO_VGAMR_W, jreg);
  473. }
  474. static void ast_set_start_address_crt1(struct ast_device *ast,
  475. unsigned int offset)
  476. {
  477. u32 addr;
  478. addr = offset >> 2;
  479. ast_set_index_reg(ast, AST_IO_VGACRI, 0x0d, (u8)(addr & 0xff));
  480. ast_set_index_reg(ast, AST_IO_VGACRI, 0x0c, (u8)((addr >> 8) & 0xff));
  481. ast_set_index_reg(ast, AST_IO_VGACRI, 0xaf, (u8)((addr >> 16) & 0xff));
  482. }
  483. static void ast_wait_for_vretrace(struct ast_device *ast)
  484. {
  485. unsigned long timeout = jiffies + HZ;
  486. u8 vgair1;
  487. do {
  488. vgair1 = ast_io_read8(ast, AST_IO_VGAIR1_R);
  489. } while (!(vgair1 & AST_IO_VGAIR1_VREFRESH) && time_before(jiffies, timeout));
  490. }
  491. /*
  492. * Planes
  493. */
  494. static int ast_plane_init(struct drm_device *dev, struct ast_plane *ast_plane,
  495. void __iomem *vaddr, u64 offset, unsigned long size,
  496. uint32_t possible_crtcs,
  497. const struct drm_plane_funcs *funcs,
  498. const uint32_t *formats, unsigned int format_count,
  499. const uint64_t *format_modifiers,
  500. enum drm_plane_type type)
  501. {
  502. struct drm_plane *plane = &ast_plane->base;
  503. ast_plane->vaddr = vaddr;
  504. ast_plane->offset = offset;
  505. ast_plane->size = size;
  506. return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
  507. formats, format_count, format_modifiers,
  508. type, NULL);
  509. }
  510. /*
  511. * Primary plane
  512. */
  513. static const uint32_t ast_primary_plane_formats[] = {
  514. DRM_FORMAT_XRGB8888,
  515. DRM_FORMAT_RGB565,
  516. DRM_FORMAT_C8,
  517. };
  518. static int ast_primary_plane_helper_atomic_check(struct drm_plane *plane,
  519. struct drm_atomic_state *state)
  520. {
  521. struct drm_device *dev = plane->dev;
  522. struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state, plane);
  523. struct drm_crtc_state *new_crtc_state = NULL;
  524. struct ast_crtc_state *new_ast_crtc_state;
  525. int ret;
  526. if (new_plane_state->crtc)
  527. new_crtc_state = drm_atomic_get_new_crtc_state(state, new_plane_state->crtc);
  528. ret = drm_atomic_helper_check_plane_state(new_plane_state, new_crtc_state,
  529. DRM_PLANE_NO_SCALING,
  530. DRM_PLANE_NO_SCALING,
  531. false, true);
  532. if (ret) {
  533. return ret;
  534. } else if (!new_plane_state->visible) {
  535. if (drm_WARN_ON(dev, new_plane_state->crtc)) /* cannot legally happen */
  536. return -EINVAL;
  537. else
  538. return 0;
  539. }
  540. new_ast_crtc_state = to_ast_crtc_state(new_crtc_state);
  541. new_ast_crtc_state->format = new_plane_state->fb->format;
  542. return 0;
  543. }
  544. static void ast_handle_damage(struct ast_plane *ast_plane, struct iosys_map *src,
  545. struct drm_framebuffer *fb,
  546. const struct drm_rect *clip)
  547. {
  548. struct iosys_map dst = IOSYS_MAP_INIT_VADDR_IOMEM(ast_plane->vaddr);
  549. iosys_map_incr(&dst, drm_fb_clip_offset(fb->pitches[0], fb->format, clip));
  550. drm_fb_memcpy(&dst, fb->pitches, src, fb, clip);
  551. }
  552. static void ast_primary_plane_helper_atomic_update(struct drm_plane *plane,
  553. struct drm_atomic_state *state)
  554. {
  555. struct drm_device *dev = plane->dev;
  556. struct ast_device *ast = to_ast_device(dev);
  557. struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane);
  558. struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
  559. struct drm_framebuffer *fb = plane_state->fb;
  560. struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(state, plane);
  561. struct drm_framebuffer *old_fb = old_plane_state->fb;
  562. struct ast_plane *ast_plane = to_ast_plane(plane);
  563. struct drm_crtc *crtc = plane_state->crtc;
  564. struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
  565. struct drm_rect damage;
  566. struct drm_atomic_helper_damage_iter iter;
  567. if (!old_fb || (fb->format != old_fb->format) || crtc_state->mode_changed) {
  568. struct ast_crtc_state *ast_crtc_state = to_ast_crtc_state(crtc_state);
  569. struct ast_vbios_mode_info *vbios_mode_info = &ast_crtc_state->vbios_mode_info;
  570. ast_set_color_reg(ast, fb->format);
  571. ast_set_vbios_color_reg(ast, fb->format, vbios_mode_info);
  572. }
  573. drm_atomic_helper_damage_iter_init(&iter, old_plane_state, plane_state);
  574. drm_atomic_for_each_plane_damage(&iter, &damage) {
  575. ast_handle_damage(ast_plane, shadow_plane_state->data, fb, &damage);
  576. }
  577. /*
  578. * Some BMCs stop scanning out the video signal after the driver
  579. * reprogrammed the offset. This stalls display output for several
  580. * seconds and makes the display unusable. Therefore only update
  581. * the offset if it changes.
  582. */
  583. if (!old_fb || old_fb->pitches[0] != fb->pitches[0])
  584. ast_set_offset_reg(ast, fb);
  585. }
  586. static void ast_primary_plane_helper_atomic_enable(struct drm_plane *plane,
  587. struct drm_atomic_state *state)
  588. {
  589. struct ast_device *ast = to_ast_device(plane->dev);
  590. struct ast_plane *ast_plane = to_ast_plane(plane);
  591. /*
  592. * Some BMCs stop scanning out the video signal after the driver
  593. * reprogrammed the scanout address. This stalls display
  594. * output for several seconds and makes the display unusable.
  595. * Therefore only reprogram the address after enabling the plane.
  596. */
  597. ast_set_start_address_crt1(ast, (u32)ast_plane->offset);
  598. }
  599. static void ast_primary_plane_helper_atomic_disable(struct drm_plane *plane,
  600. struct drm_atomic_state *state)
  601. {
  602. /*
  603. * Keep this empty function to avoid calling
  604. * atomic_update when disabling the plane.
  605. */
  606. }
  607. static int ast_primary_plane_helper_get_scanout_buffer(struct drm_plane *plane,
  608. struct drm_scanout_buffer *sb)
  609. {
  610. struct ast_plane *ast_plane = to_ast_plane(plane);
  611. if (plane->state && plane->state->fb && ast_plane->vaddr) {
  612. sb->format = plane->state->fb->format;
  613. sb->width = plane->state->fb->width;
  614. sb->height = plane->state->fb->height;
  615. sb->pitch[0] = plane->state->fb->pitches[0];
  616. iosys_map_set_vaddr_iomem(&sb->map[0], ast_plane->vaddr);
  617. return 0;
  618. }
  619. return -ENODEV;
  620. }
  621. static const struct drm_plane_helper_funcs ast_primary_plane_helper_funcs = {
  622. DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,
  623. .atomic_check = ast_primary_plane_helper_atomic_check,
  624. .atomic_update = ast_primary_plane_helper_atomic_update,
  625. .atomic_enable = ast_primary_plane_helper_atomic_enable,
  626. .atomic_disable = ast_primary_plane_helper_atomic_disable,
  627. .get_scanout_buffer = ast_primary_plane_helper_get_scanout_buffer,
  628. };
  629. static const struct drm_plane_funcs ast_primary_plane_funcs = {
  630. .update_plane = drm_atomic_helper_update_plane,
  631. .disable_plane = drm_atomic_helper_disable_plane,
  632. .destroy = drm_plane_cleanup,
  633. DRM_GEM_SHADOW_PLANE_FUNCS,
  634. };
  635. static int ast_primary_plane_init(struct ast_device *ast)
  636. {
  637. struct drm_device *dev = &ast->base;
  638. struct ast_plane *ast_primary_plane = &ast->primary_plane;
  639. struct drm_plane *primary_plane = &ast_primary_plane->base;
  640. void __iomem *vaddr = ast->vram;
  641. u64 offset = 0; /* with shmem, the primary plane is always at offset 0 */
  642. unsigned long cursor_size = roundup(AST_HWC_SIZE + AST_HWC_SIGNATURE_SIZE, PAGE_SIZE);
  643. unsigned long size = ast->vram_fb_available - cursor_size;
  644. int ret;
  645. ret = ast_plane_init(dev, ast_primary_plane, vaddr, offset, size,
  646. 0x01, &ast_primary_plane_funcs,
  647. ast_primary_plane_formats, ARRAY_SIZE(ast_primary_plane_formats),
  648. NULL, DRM_PLANE_TYPE_PRIMARY);
  649. if (ret) {
  650. drm_err(dev, "ast_plane_init() failed: %d\n", ret);
  651. return ret;
  652. }
  653. drm_plane_helper_add(primary_plane, &ast_primary_plane_helper_funcs);
  654. drm_plane_enable_fb_damage_clips(primary_plane);
  655. return 0;
  656. }
  657. /*
  658. * Cursor plane
  659. */
  660. static void ast_update_cursor_image(u8 __iomem *dst, const u8 *src, int width, int height)
  661. {
  662. union {
  663. u32 ul;
  664. u8 b[4];
  665. } srcdata32[2], data32;
  666. union {
  667. u16 us;
  668. u8 b[2];
  669. } data16;
  670. u32 csum = 0;
  671. s32 alpha_dst_delta, last_alpha_dst_delta;
  672. u8 __iomem *dstxor;
  673. const u8 *srcxor;
  674. int i, j;
  675. u32 per_pixel_copy, two_pixel_copy;
  676. alpha_dst_delta = AST_MAX_HWC_WIDTH << 1;
  677. last_alpha_dst_delta = alpha_dst_delta - (width << 1);
  678. srcxor = src;
  679. dstxor = (u8 *)dst + last_alpha_dst_delta + (AST_MAX_HWC_HEIGHT - height) * alpha_dst_delta;
  680. per_pixel_copy = width & 1;
  681. two_pixel_copy = width >> 1;
  682. for (j = 0; j < height; j++) {
  683. for (i = 0; i < two_pixel_copy; i++) {
  684. srcdata32[0].ul = *((u32 *)srcxor) & 0xf0f0f0f0;
  685. srcdata32[1].ul = *((u32 *)(srcxor + 4)) & 0xf0f0f0f0;
  686. data32.b[0] = srcdata32[0].b[1] | (srcdata32[0].b[0] >> 4);
  687. data32.b[1] = srcdata32[0].b[3] | (srcdata32[0].b[2] >> 4);
  688. data32.b[2] = srcdata32[1].b[1] | (srcdata32[1].b[0] >> 4);
  689. data32.b[3] = srcdata32[1].b[3] | (srcdata32[1].b[2] >> 4);
  690. writel(data32.ul, dstxor);
  691. csum += data32.ul;
  692. dstxor += 4;
  693. srcxor += 8;
  694. }
  695. for (i = 0; i < per_pixel_copy; i++) {
  696. srcdata32[0].ul = *((u32 *)srcxor) & 0xf0f0f0f0;
  697. data16.b[0] = srcdata32[0].b[1] | (srcdata32[0].b[0] >> 4);
  698. data16.b[1] = srcdata32[0].b[3] | (srcdata32[0].b[2] >> 4);
  699. writew(data16.us, dstxor);
  700. csum += (u32)data16.us;
  701. dstxor += 2;
  702. srcxor += 4;
  703. }
  704. dstxor += last_alpha_dst_delta;
  705. }
  706. /* write checksum + signature */
  707. dst += AST_HWC_SIZE;
  708. writel(csum, dst);
  709. writel(width, dst + AST_HWC_SIGNATURE_SizeX);
  710. writel(height, dst + AST_HWC_SIGNATURE_SizeY);
  711. writel(0, dst + AST_HWC_SIGNATURE_HOTSPOTX);
  712. writel(0, dst + AST_HWC_SIGNATURE_HOTSPOTY);
  713. }
  714. static void ast_set_cursor_base(struct ast_device *ast, u64 address)
  715. {
  716. u8 addr0 = (address >> 3) & 0xff;
  717. u8 addr1 = (address >> 11) & 0xff;
  718. u8 addr2 = (address >> 19) & 0xff;
  719. ast_set_index_reg(ast, AST_IO_VGACRI, 0xc8, addr0);
  720. ast_set_index_reg(ast, AST_IO_VGACRI, 0xc9, addr1);
  721. ast_set_index_reg(ast, AST_IO_VGACRI, 0xca, addr2);
  722. }
  723. static void ast_set_cursor_location(struct ast_device *ast, u16 x, u16 y,
  724. u8 x_offset, u8 y_offset)
  725. {
  726. u8 x0 = (x & 0x00ff);
  727. u8 x1 = (x & 0x0f00) >> 8;
  728. u8 y0 = (y & 0x00ff);
  729. u8 y1 = (y & 0x0700) >> 8;
  730. ast_set_index_reg(ast, AST_IO_VGACRI, 0xc2, x_offset);
  731. ast_set_index_reg(ast, AST_IO_VGACRI, 0xc3, y_offset);
  732. ast_set_index_reg(ast, AST_IO_VGACRI, 0xc4, x0);
  733. ast_set_index_reg(ast, AST_IO_VGACRI, 0xc5, x1);
  734. ast_set_index_reg(ast, AST_IO_VGACRI, 0xc6, y0);
  735. ast_set_index_reg(ast, AST_IO_VGACRI, 0xc7, y1);
  736. }
  737. static void ast_set_cursor_enabled(struct ast_device *ast, bool enabled)
  738. {
  739. static const u8 mask = (u8)~(AST_IO_VGACRCB_HWC_16BPP |
  740. AST_IO_VGACRCB_HWC_ENABLED);
  741. u8 vgacrcb = AST_IO_VGACRCB_HWC_16BPP;
  742. if (enabled)
  743. vgacrcb |= AST_IO_VGACRCB_HWC_ENABLED;
  744. ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xcb, mask, vgacrcb);
  745. }
  746. static const uint32_t ast_cursor_plane_formats[] = {
  747. DRM_FORMAT_ARGB8888,
  748. };
  749. static int ast_cursor_plane_helper_atomic_check(struct drm_plane *plane,
  750. struct drm_atomic_state *state)
  751. {
  752. struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state, plane);
  753. struct drm_framebuffer *new_fb = new_plane_state->fb;
  754. struct drm_crtc_state *new_crtc_state = NULL;
  755. int ret;
  756. if (new_plane_state->crtc)
  757. new_crtc_state = drm_atomic_get_new_crtc_state(state, new_plane_state->crtc);
  758. ret = drm_atomic_helper_check_plane_state(new_plane_state, new_crtc_state,
  759. DRM_PLANE_NO_SCALING,
  760. DRM_PLANE_NO_SCALING,
  761. true, true);
  762. if (ret || !new_plane_state->visible)
  763. return ret;
  764. if (new_fb->width > AST_MAX_HWC_WIDTH || new_fb->height > AST_MAX_HWC_HEIGHT)
  765. return -EINVAL;
  766. return 0;
  767. }
  768. static void ast_cursor_plane_helper_atomic_update(struct drm_plane *plane,
  769. struct drm_atomic_state *state)
  770. {
  771. struct ast_plane *ast_plane = to_ast_plane(plane);
  772. struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane);
  773. struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
  774. struct drm_framebuffer *fb = plane_state->fb;
  775. struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(state, plane);
  776. struct ast_device *ast = to_ast_device(plane->dev);
  777. struct iosys_map src_map = shadow_plane_state->data[0];
  778. struct drm_rect damage;
  779. const u8 *src = src_map.vaddr; /* TODO: Use mapping abstraction properly */
  780. u64 dst_off = ast_plane->offset;
  781. u8 __iomem *dst = ast_plane->vaddr; /* TODO: Use mapping abstraction properly */
  782. u8 __iomem *sig = dst + AST_HWC_SIZE; /* TODO: Use mapping abstraction properly */
  783. unsigned int offset_x, offset_y;
  784. u16 x, y;
  785. u8 x_offset, y_offset;
  786. /*
  787. * Do data transfer to hardware buffer and point the scanout
  788. * engine to the offset.
  789. */
  790. if (drm_atomic_helper_damage_merged(old_plane_state, plane_state, &damage)) {
  791. ast_update_cursor_image(dst, src, fb->width, fb->height);
  792. ast_set_cursor_base(ast, dst_off);
  793. }
  794. /*
  795. * Update location in HWC signature and registers.
  796. */
  797. writel(plane_state->crtc_x, sig + AST_HWC_SIGNATURE_X);
  798. writel(plane_state->crtc_y, sig + AST_HWC_SIGNATURE_Y);
  799. offset_x = AST_MAX_HWC_WIDTH - fb->width;
  800. offset_y = AST_MAX_HWC_HEIGHT - fb->height;
  801. if (plane_state->crtc_x < 0) {
  802. x_offset = (-plane_state->crtc_x) + offset_x;
  803. x = 0;
  804. } else {
  805. x_offset = offset_x;
  806. x = plane_state->crtc_x;
  807. }
  808. if (plane_state->crtc_y < 0) {
  809. y_offset = (-plane_state->crtc_y) + offset_y;
  810. y = 0;
  811. } else {
  812. y_offset = offset_y;
  813. y = plane_state->crtc_y;
  814. }
  815. ast_set_cursor_location(ast, x, y, x_offset, y_offset);
  816. /* Dummy write to enable HWC and make the HW pick-up the changes. */
  817. ast_set_cursor_enabled(ast, true);
  818. }
  819. static void ast_cursor_plane_helper_atomic_disable(struct drm_plane *plane,
  820. struct drm_atomic_state *state)
  821. {
  822. struct ast_device *ast = to_ast_device(plane->dev);
  823. ast_set_cursor_enabled(ast, false);
  824. }
  825. static const struct drm_plane_helper_funcs ast_cursor_plane_helper_funcs = {
  826. DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,
  827. .atomic_check = ast_cursor_plane_helper_atomic_check,
  828. .atomic_update = ast_cursor_plane_helper_atomic_update,
  829. .atomic_disable = ast_cursor_plane_helper_atomic_disable,
  830. };
  831. static const struct drm_plane_funcs ast_cursor_plane_funcs = {
  832. .update_plane = drm_atomic_helper_update_plane,
  833. .disable_plane = drm_atomic_helper_disable_plane,
  834. .destroy = drm_plane_cleanup,
  835. DRM_GEM_SHADOW_PLANE_FUNCS,
  836. };
  837. static int ast_cursor_plane_init(struct ast_device *ast)
  838. {
  839. struct drm_device *dev = &ast->base;
  840. struct ast_plane *ast_cursor_plane = &ast->cursor_plane;
  841. struct drm_plane *cursor_plane = &ast_cursor_plane->base;
  842. size_t size;
  843. void __iomem *vaddr;
  844. u64 offset;
  845. int ret;
  846. /*
  847. * Allocate backing storage for cursors. The BOs are permanently
  848. * pinned to the top end of the VRAM.
  849. */
  850. size = roundup(AST_HWC_SIZE + AST_HWC_SIGNATURE_SIZE, PAGE_SIZE);
  851. if (ast->vram_fb_available < size)
  852. return -ENOMEM;
  853. vaddr = ast->vram + ast->vram_fb_available - size;
  854. offset = ast->vram_fb_available - size;
  855. ret = ast_plane_init(dev, ast_cursor_plane, vaddr, offset, size,
  856. 0x01, &ast_cursor_plane_funcs,
  857. ast_cursor_plane_formats, ARRAY_SIZE(ast_cursor_plane_formats),
  858. NULL, DRM_PLANE_TYPE_CURSOR);
  859. if (ret) {
  860. drm_err(dev, "ast_plane_init() failed: %d\n", ret);
  861. return ret;
  862. }
  863. drm_plane_helper_add(cursor_plane, &ast_cursor_plane_helper_funcs);
  864. drm_plane_enable_fb_damage_clips(cursor_plane);
  865. ast->vram_fb_available -= size;
  866. return 0;
  867. }
  868. /*
  869. * CRTC
  870. */
  871. static enum drm_mode_status
  872. ast_crtc_helper_mode_valid(struct drm_crtc *crtc, const struct drm_display_mode *mode)
  873. {
  874. struct ast_device *ast = to_ast_device(crtc->dev);
  875. enum drm_mode_status status;
  876. uint32_t jtemp;
  877. if (ast->support_wide_screen) {
  878. if ((mode->hdisplay == 1680) && (mode->vdisplay == 1050))
  879. return MODE_OK;
  880. if ((mode->hdisplay == 1280) && (mode->vdisplay == 800))
  881. return MODE_OK;
  882. if ((mode->hdisplay == 1440) && (mode->vdisplay == 900))
  883. return MODE_OK;
  884. if ((mode->hdisplay == 1360) && (mode->vdisplay == 768))
  885. return MODE_OK;
  886. if ((mode->hdisplay == 1600) && (mode->vdisplay == 900))
  887. return MODE_OK;
  888. if ((mode->hdisplay == 1152) && (mode->vdisplay == 864))
  889. return MODE_OK;
  890. if ((ast->chip == AST2100) || // GEN2, but not AST1100 (?)
  891. (ast->chip == AST2200) || // GEN3, but not AST2150 (?)
  892. IS_AST_GEN4(ast) || IS_AST_GEN5(ast) ||
  893. IS_AST_GEN6(ast) || IS_AST_GEN7(ast)) {
  894. if ((mode->hdisplay == 1920) && (mode->vdisplay == 1080))
  895. return MODE_OK;
  896. if ((mode->hdisplay == 1920) && (mode->vdisplay == 1200)) {
  897. jtemp = ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0xd1, 0xff);
  898. if (jtemp & 0x01)
  899. return MODE_NOMODE;
  900. else
  901. return MODE_OK;
  902. }
  903. }
  904. }
  905. status = MODE_NOMODE;
  906. switch (mode->hdisplay) {
  907. case 640:
  908. if (mode->vdisplay == 480)
  909. status = MODE_OK;
  910. break;
  911. case 800:
  912. if (mode->vdisplay == 600)
  913. status = MODE_OK;
  914. break;
  915. case 1024:
  916. if (mode->vdisplay == 768)
  917. status = MODE_OK;
  918. break;
  919. case 1152:
  920. if (mode->vdisplay == 864)
  921. status = MODE_OK;
  922. break;
  923. case 1280:
  924. if (mode->vdisplay == 1024)
  925. status = MODE_OK;
  926. break;
  927. case 1600:
  928. if (mode->vdisplay == 1200)
  929. status = MODE_OK;
  930. break;
  931. default:
  932. break;
  933. }
  934. return status;
  935. }
  936. static void ast_crtc_helper_mode_set_nofb(struct drm_crtc *crtc)
  937. {
  938. struct drm_device *dev = crtc->dev;
  939. struct ast_device *ast = to_ast_device(dev);
  940. struct drm_crtc_state *crtc_state = crtc->state;
  941. struct ast_crtc_state *ast_crtc_state = to_ast_crtc_state(crtc_state);
  942. struct ast_vbios_mode_info *vbios_mode_info =
  943. &ast_crtc_state->vbios_mode_info;
  944. struct drm_display_mode *adjusted_mode = &crtc_state->adjusted_mode;
  945. /*
  946. * Ensure that no scanout takes place before reprogramming mode
  947. * and format registers.
  948. *
  949. * TODO: Get vblank interrupts working and remove this line.
  950. */
  951. ast_wait_for_vretrace(ast);
  952. ast_set_vbios_mode_reg(ast, adjusted_mode, vbios_mode_info);
  953. ast_set_index_reg(ast, AST_IO_VGACRI, 0xa1, 0x06);
  954. ast_set_std_reg(ast, adjusted_mode, vbios_mode_info);
  955. ast_set_crtc_reg(ast, adjusted_mode, vbios_mode_info);
  956. ast_set_dclk_reg(ast, adjusted_mode, vbios_mode_info);
  957. ast_set_crtthd_reg(ast);
  958. ast_set_sync_reg(ast, adjusted_mode, vbios_mode_info);
  959. }
  960. static int ast_crtc_helper_atomic_check(struct drm_crtc *crtc,
  961. struct drm_atomic_state *state)
  962. {
  963. struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
  964. struct drm_crtc_state *old_crtc_state = drm_atomic_get_old_crtc_state(state, crtc);
  965. struct ast_crtc_state *old_ast_crtc_state = to_ast_crtc_state(old_crtc_state);
  966. struct drm_device *dev = crtc->dev;
  967. struct ast_crtc_state *ast_state;
  968. const struct drm_format_info *format;
  969. bool succ;
  970. int ret;
  971. if (!crtc_state->enable)
  972. return 0;
  973. ret = drm_atomic_helper_check_crtc_primary_plane(crtc_state);
  974. if (ret)
  975. return ret;
  976. ast_state = to_ast_crtc_state(crtc_state);
  977. format = ast_state->format;
  978. if (drm_WARN_ON_ONCE(dev, !format))
  979. return -EINVAL; /* BUG: We didn't set format in primary check(). */
  980. /*
  981. * The gamma LUT has to be reloaded after changing the primary
  982. * plane's color format.
  983. */
  984. if (old_ast_crtc_state->format != format)
  985. crtc_state->color_mgmt_changed = true;
  986. if (crtc_state->color_mgmt_changed && crtc_state->gamma_lut) {
  987. if (crtc_state->gamma_lut->length !=
  988. AST_LUT_SIZE * sizeof(struct drm_color_lut)) {
  989. drm_err(dev, "Wrong size for gamma_lut %zu\n",
  990. crtc_state->gamma_lut->length);
  991. return -EINVAL;
  992. }
  993. }
  994. succ = ast_get_vbios_mode_info(format, &crtc_state->mode,
  995. &crtc_state->adjusted_mode,
  996. &ast_state->vbios_mode_info);
  997. if (!succ)
  998. return -EINVAL;
  999. return 0;
  1000. }
  1001. static void
  1002. ast_crtc_helper_atomic_flush(struct drm_crtc *crtc,
  1003. struct drm_atomic_state *state)
  1004. {
  1005. struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state,
  1006. crtc);
  1007. struct drm_device *dev = crtc->dev;
  1008. struct ast_device *ast = to_ast_device(dev);
  1009. struct ast_crtc_state *ast_crtc_state = to_ast_crtc_state(crtc_state);
  1010. /*
  1011. * The gamma LUT has to be reloaded after changing the primary
  1012. * plane's color format.
  1013. */
  1014. if (crtc_state->enable && crtc_state->color_mgmt_changed) {
  1015. if (crtc_state->gamma_lut)
  1016. ast_crtc_set_gamma(ast,
  1017. ast_crtc_state->format,
  1018. crtc_state->gamma_lut->data);
  1019. else
  1020. ast_crtc_set_gamma_linear(ast, ast_crtc_state->format);
  1021. }
  1022. }
  1023. static void ast_crtc_helper_atomic_enable(struct drm_crtc *crtc, struct drm_atomic_state *state)
  1024. {
  1025. struct ast_device *ast = to_ast_device(crtc->dev);
  1026. ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xb6, 0xfc, 0x00);
  1027. ast_set_index_reg_mask(ast, AST_IO_VGASRI, 0x01, 0xdf, 0x00);
  1028. }
  1029. static void ast_crtc_helper_atomic_disable(struct drm_crtc *crtc, struct drm_atomic_state *state)
  1030. {
  1031. struct drm_crtc_state *old_crtc_state = drm_atomic_get_old_crtc_state(state, crtc);
  1032. struct ast_device *ast = to_ast_device(crtc->dev);
  1033. u8 vgacrb6;
  1034. ast_set_index_reg_mask(ast, AST_IO_VGASRI, 0x01, 0xdf, AST_IO_VGASR1_SD);
  1035. vgacrb6 = AST_IO_VGACRB6_VSYNC_OFF |
  1036. AST_IO_VGACRB6_HSYNC_OFF;
  1037. ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xb6, 0xfc, vgacrb6);
  1038. /*
  1039. * HW cursors require the underlying primary plane and CRTC to
  1040. * display a valid mode and image. This is not the case during
  1041. * full modeset operations. So we temporarily disable any active
  1042. * plane, including the HW cursor. Each plane's atomic_update()
  1043. * helper will re-enable it if necessary.
  1044. *
  1045. * We only do this during *full* modesets. It does not affect
  1046. * simple pageflips on the planes.
  1047. */
  1048. drm_atomic_helper_disable_planes_on_crtc(old_crtc_state, false);
  1049. }
  1050. static const struct drm_crtc_helper_funcs ast_crtc_helper_funcs = {
  1051. .mode_valid = ast_crtc_helper_mode_valid,
  1052. .mode_set_nofb = ast_crtc_helper_mode_set_nofb,
  1053. .atomic_check = ast_crtc_helper_atomic_check,
  1054. .atomic_flush = ast_crtc_helper_atomic_flush,
  1055. .atomic_enable = ast_crtc_helper_atomic_enable,
  1056. .atomic_disable = ast_crtc_helper_atomic_disable,
  1057. };
  1058. static void ast_crtc_reset(struct drm_crtc *crtc)
  1059. {
  1060. struct ast_crtc_state *ast_state =
  1061. kzalloc(sizeof(*ast_state), GFP_KERNEL);
  1062. if (crtc->state)
  1063. crtc->funcs->atomic_destroy_state(crtc, crtc->state);
  1064. if (ast_state)
  1065. __drm_atomic_helper_crtc_reset(crtc, &ast_state->base);
  1066. else
  1067. __drm_atomic_helper_crtc_reset(crtc, NULL);
  1068. }
  1069. static struct drm_crtc_state *
  1070. ast_crtc_atomic_duplicate_state(struct drm_crtc *crtc)
  1071. {
  1072. struct ast_crtc_state *new_ast_state, *ast_state;
  1073. struct drm_device *dev = crtc->dev;
  1074. if (drm_WARN_ON(dev, !crtc->state))
  1075. return NULL;
  1076. new_ast_state = kmalloc(sizeof(*new_ast_state), GFP_KERNEL);
  1077. if (!new_ast_state)
  1078. return NULL;
  1079. __drm_atomic_helper_crtc_duplicate_state(crtc, &new_ast_state->base);
  1080. ast_state = to_ast_crtc_state(crtc->state);
  1081. new_ast_state->format = ast_state->format;
  1082. memcpy(&new_ast_state->vbios_mode_info, &ast_state->vbios_mode_info,
  1083. sizeof(new_ast_state->vbios_mode_info));
  1084. return &new_ast_state->base;
  1085. }
  1086. static void ast_crtc_atomic_destroy_state(struct drm_crtc *crtc,
  1087. struct drm_crtc_state *state)
  1088. {
  1089. struct ast_crtc_state *ast_state = to_ast_crtc_state(state);
  1090. __drm_atomic_helper_crtc_destroy_state(&ast_state->base);
  1091. kfree(ast_state);
  1092. }
  1093. static const struct drm_crtc_funcs ast_crtc_funcs = {
  1094. .reset = ast_crtc_reset,
  1095. .destroy = drm_crtc_cleanup,
  1096. .set_config = drm_atomic_helper_set_config,
  1097. .page_flip = drm_atomic_helper_page_flip,
  1098. .atomic_duplicate_state = ast_crtc_atomic_duplicate_state,
  1099. .atomic_destroy_state = ast_crtc_atomic_destroy_state,
  1100. };
  1101. static int ast_crtc_init(struct drm_device *dev)
  1102. {
  1103. struct ast_device *ast = to_ast_device(dev);
  1104. struct drm_crtc *crtc = &ast->crtc;
  1105. int ret;
  1106. ret = drm_crtc_init_with_planes(dev, crtc, &ast->primary_plane.base,
  1107. &ast->cursor_plane.base, &ast_crtc_funcs,
  1108. NULL);
  1109. if (ret)
  1110. return ret;
  1111. drm_mode_crtc_set_gamma_size(crtc, AST_LUT_SIZE);
  1112. drm_crtc_enable_color_mgmt(crtc, 0, false, AST_LUT_SIZE);
  1113. drm_crtc_helper_add(crtc, &ast_crtc_helper_funcs);
  1114. return 0;
  1115. }
  1116. /*
  1117. * Mode config
  1118. */
  1119. static void ast_mode_config_helper_atomic_commit_tail(struct drm_atomic_state *state)
  1120. {
  1121. struct ast_device *ast = to_ast_device(state->dev);
  1122. /*
  1123. * Concurrent operations could possibly trigger a call to
  1124. * drm_connector_helper_funcs.get_modes by reading the display
  1125. * modes. Protect access to registers by acquiring the modeset
  1126. * lock.
  1127. */
  1128. mutex_lock(&ast->modeset_lock);
  1129. drm_atomic_helper_commit_tail(state);
  1130. mutex_unlock(&ast->modeset_lock);
  1131. }
  1132. static const struct drm_mode_config_helper_funcs ast_mode_config_helper_funcs = {
  1133. .atomic_commit_tail = ast_mode_config_helper_atomic_commit_tail,
  1134. };
  1135. static enum drm_mode_status ast_mode_config_mode_valid(struct drm_device *dev,
  1136. const struct drm_display_mode *mode)
  1137. {
  1138. static const unsigned long max_bpp = 4; /* DRM_FORMAT_XRGB8888 */
  1139. struct ast_device *ast = to_ast_device(dev);
  1140. unsigned long fbsize, fbpages, max_fbpages;
  1141. max_fbpages = (ast->vram_fb_available) >> PAGE_SHIFT;
  1142. fbsize = mode->hdisplay * mode->vdisplay * max_bpp;
  1143. fbpages = DIV_ROUND_UP(fbsize, PAGE_SIZE);
  1144. if (fbpages > max_fbpages)
  1145. return MODE_MEM;
  1146. return MODE_OK;
  1147. }
  1148. static const struct drm_mode_config_funcs ast_mode_config_funcs = {
  1149. .fb_create = drm_gem_fb_create_with_dirty,
  1150. .mode_valid = ast_mode_config_mode_valid,
  1151. .atomic_check = drm_atomic_helper_check,
  1152. .atomic_commit = drm_atomic_helper_commit,
  1153. };
  1154. int ast_mode_config_init(struct ast_device *ast)
  1155. {
  1156. struct drm_device *dev = &ast->base;
  1157. int ret;
  1158. ret = drmm_mutex_init(dev, &ast->modeset_lock);
  1159. if (ret)
  1160. return ret;
  1161. ret = drmm_mode_config_init(dev);
  1162. if (ret)
  1163. return ret;
  1164. dev->mode_config.funcs = &ast_mode_config_funcs;
  1165. dev->mode_config.min_width = 0;
  1166. dev->mode_config.min_height = 0;
  1167. dev->mode_config.preferred_depth = 24;
  1168. if (ast->chip == AST2100 || // GEN2, but not AST1100 (?)
  1169. ast->chip == AST2200 || // GEN3, but not AST2150 (?)
  1170. IS_AST_GEN7(ast) ||
  1171. IS_AST_GEN6(ast) ||
  1172. IS_AST_GEN5(ast) ||
  1173. IS_AST_GEN4(ast)) {
  1174. dev->mode_config.max_width = 1920;
  1175. dev->mode_config.max_height = 2048;
  1176. } else {
  1177. dev->mode_config.max_width = 1600;
  1178. dev->mode_config.max_height = 1200;
  1179. }
  1180. dev->mode_config.helper_private = &ast_mode_config_helper_funcs;
  1181. ret = ast_primary_plane_init(ast);
  1182. if (ret)
  1183. return ret;
  1184. ret = ast_cursor_plane_init(ast);
  1185. if (ret)
  1186. return ret;
  1187. ast_crtc_init(dev);
  1188. if (ast->tx_chip_types & AST_TX_NONE_BIT) {
  1189. ret = ast_vga_output_init(ast);
  1190. if (ret)
  1191. return ret;
  1192. }
  1193. if (ast->tx_chip_types & AST_TX_SIL164_BIT) {
  1194. ret = ast_sil164_output_init(ast);
  1195. if (ret)
  1196. return ret;
  1197. }
  1198. if (ast->tx_chip_types & AST_TX_DP501_BIT) {
  1199. ret = ast_dp501_output_init(ast);
  1200. if (ret)
  1201. return ret;
  1202. }
  1203. if (ast->tx_chip_types & AST_TX_ASTDP_BIT) {
  1204. ret = ast_astdp_output_init(ast);
  1205. if (ret)
  1206. return ret;
  1207. }
  1208. drm_mode_config_reset(dev);
  1209. ret = drmm_kms_helper_poll_init(dev);
  1210. if (ret)
  1211. return ret;
  1212. return 0;
  1213. }